diff --git a/automation_devices/src/lib.rs b/automation_devices/src/lib.rs index 826f9dc..612eab5 100644 --- a/automation_devices/src/lib.rs +++ b/automation_devices/src/lib.rs @@ -96,6 +96,26 @@ macro_rules! impl_device { }); } + if impls::impls!($device: google_home::traits::ColorSetting) { + methods.add_async_method("set_color_temperature", |_lua, this, temperature: u32| async move { + (this.deref().cast() as Option<&dyn google_home::traits::ColorSetting>) + .expect("Cast should be valid") + .set_color(google_home::traits::Color {temperature}) + .await + .unwrap(); + + Ok(()) + }); + + methods.add_async_method("color_temperature", |_lua, this, _: ()| async move { + Ok((this.deref().cast() as Option<&dyn google_home::traits::ColorSetting>) + .expect("Cast should be valid") + .color() + .await + .temperature) + }); + } + if impls::impls!($device: google_home::traits::OpenClose) { // TODO: Make discrete_only_open_close and query_only_open_close static, that way we can // add only the supported functions and drop _percet if discrete is true diff --git a/google_home/google_home/src/traits.rs b/google_home/google_home/src/traits.rs index 6ce251b..ca576f0 100644 --- a/google_home/google_home/src/traits.rs +++ b/google_home/google_home/src/traits.rs @@ -1,7 +1,7 @@ #![allow(non_snake_case)] use automation_cast::Cast; use google_home_macro::traits; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use crate::errors::ErrorCode; use crate::Device; @@ -26,6 +26,12 @@ traits! { async fn brightness(&self) -> Result, "action.devices.commands.BrightnessAbsolute" => async fn set_brightness(&self, brightness: u8) -> Result<(), ErrorCode>, }, + "action.devices.traits.ColorSetting" => trait ColorSetting { + color_temperature_range: ColorTemperatureRange, + + async fn color(&self) -> Color, + "action.devices.commands.ColorAbsolute" => async fn set_color(&self, color: Color) -> Result<(), ErrorCode>, + }, "action.devices.traits.Scene" => trait Scene { scene_reversible: Option, @@ -56,6 +62,20 @@ traits! { } } +#[derive(Debug, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ColorTemperatureRange { + pub temperature_min_k: u32, + pub temperature_max_k: u32, +} + +#[derive(Debug, Copy, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Color { + #[serde(rename(serialize = "temperatureK"))] + pub temperature: u32, +} + #[derive(Debug, Serialize)] pub struct SpeedValue { pub speed_synonym: Vec,