From 2496e03033b70ca41dd6a0d9c1d259ccb6c16ac9 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sat, 13 Apr 2024 12:10:34 -0400 Subject: [PATCH] Add json incoming for light --- .../src/integrations.rs | 28 ++++++++++ homeassistant-mqtt-discovery/src/tests.rs | 54 ++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/homeassistant-mqtt-discovery/src/integrations.rs b/homeassistant-mqtt-discovery/src/integrations.rs index 2a445bd..68dd7ac 100644 --- a/homeassistant-mqtt-discovery/src/integrations.rs +++ b/homeassistant-mqtt-discovery/src/integrations.rs @@ -2069,6 +2069,34 @@ pub mod light { Rgbww, White, } + + #[derive(Debug, PartialEq, Clone, Default)] + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] + #[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))] + pub struct JsonIncoming { + pub brightness: Option, + pub color_mode: Option, + pub color_temp: Option, + pub color: Option, + pub effect: Option, + pub state: Option, + pub transition: Option, + } + + #[derive(Debug, PartialEq, Clone, Default)] + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] + #[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))] + pub struct IncomingColor { + pub r: Option, + pub g: Option, + pub b: Option, + pub c: Option, + pub w: Option, + pub x: Option, + pub y: Option, + pub h: Option, + pub s: Option, + } } pub mod lock { use super::*; diff --git a/homeassistant-mqtt-discovery/src/tests.rs b/homeassistant-mqtt-discovery/src/tests.rs index fb7dce3..b8da1f5 100644 --- a/homeassistant-mqtt-discovery/src/tests.rs +++ b/homeassistant-mqtt-discovery/src/tests.rs @@ -1,6 +1,6 @@ use crate::integrations::{ binary_sensor, - light::{self, ColorMode}, + light::{self, ColorMode, IncomingColor, JsonIncoming}, }; use super::*; @@ -28,6 +28,58 @@ fn device() { ); } +#[test] +fn light_json_incoming() { + let test_data = r#"{ + "brightness": 255, + "color_mode": "rgb", + "color_temp": 155, + "color": { + "r": 255, + "g": 180, + "b": 200, + "c": 100, + "w": 50, + "x": 0.406, + "y": 0.301, + "h": 344.0, + "s": 29.412 + }, + "effect": "colorloop", + "state": "ON", + "transition": 2 + }"#; + + let expected = JsonIncoming { + brightness: Some(255), + color_mode: Some(ColorMode::Rgb), + color_temp: Some(155), + color: Some(IncomingColor { + r: Some(255), + g: Some(180), + b: Some(200), + c: Some(100), + w: Some(50), + x: Some(0.406), + y: Some(0.301), + h: Some(344.0), + s: Some(29.412), + }), + effect: Some(String::from("colorloop")), + state: Some(String::from("ON")), + transition: Some(2), + }; + + assert_eq!( + expected, + serde_json::from_str::(test_data).unwrap(), + ); + assert_eq!( + serde_json::from_str::(&serde_json::to_string(&expected).unwrap()).unwrap(), + serde_json::from_str::(test_data).unwrap() + ); +} + #[test] fn light_default() { let test_data = r#"{