diff --git a/driver/kasa/src/impl_protocol.rs b/driver/kasa/src/impl_protocol.rs index 6bb9105..4977a98 100644 --- a/driver/kasa/src/impl_protocol.rs +++ b/driver/kasa/src/impl_protocol.rs @@ -1,14 +1,14 @@ use std::convert::Infallible; use palette::{encoding::Srgb, Hsv, IntoColor}; -use protocol::light::{GetState, Kelvin, SetState, TurnToColor, TurnToTemperature}; +use protocol::light::{GetState, SetState, TurnToColor, TurnToTemperature}; use snafu::{ResultExt, Snafu}; use crate::{ connection::{HandleError, LB130USHandle}, messages::{ - Angle, Hsb, LightState, Off, On, Percentage, SetLightHsv, SetLightLastOn, SetLightOff, - SetLightStateArgs, SetLightTo, + Angle, Hsb, LightState, Off, On, Percentage, SetLightHsv, SetLightKelvin, SetLightLastOn, + SetLightOff, SetLightStateArgs, SetLightTo, }, }; @@ -64,11 +64,33 @@ impl SetState for LB130USHandle { } } -impl TurnToTemperature for LB130USHandle { - type Error = Infallible; // TODO +#[derive(Debug, Snafu)] +#[snafu(module)] +pub enum TurnToTemperatureError { + HandleError { source: HandleError }, +} - async fn turn_to_temperature(&mut self, temperature: Kelvin) -> Result<(), Self::Error> { - todo!() +impl TurnToTemperature for LB130USHandle { + type Error = TurnToTemperatureError; + + async fn turn_to_temperature( + &mut self, + temperature: protocol::light::Kelvin, + ) -> Result<(), Self::Error> { + // TODO: re-evaluate saturating + let color_temp = crate::messages::Kelvin::new_saturating(temperature.get()); + + self.set_light_state(SetLightStateArgs { + to: SetLightTo::Kelvin(SetLightKelvin { + on_off: On, + color_temp, + }), + transition: None, + }) + .await + .context(turn_to_temperature_error::HandleSnafu)?; + + Ok(()) } } diff --git a/driver/kasa/src/messages.rs b/driver/kasa/src/messages.rs index b8839c6..53a25d1 100644 --- a/driver/kasa/src/messages.rs +++ b/driver/kasa/src/messages.rs @@ -388,13 +388,19 @@ pub struct SetLightHsv { pub hsb: Hsb, } +#[derive(Debug, Clone, Serialize)] +pub struct SetLightKelvin { + pub on_off: On, + pub color_temp: Kelvin, +} + #[derive(Debug, Clone, Serialize)] #[serde(untagged)] pub enum SetLightTo { Off(SetLightOff), LastOn(SetLightLastOn), Hsv(SetLightHsv), - // TODO: kelvin + Kelvin(SetLightKelvin), } #[derive(Debug, Clone, derive_more::From)]