Add json incoming for light

This commit is contained in:
Austen Adler 2024-04-13 12:10:34 -04:00
parent d4b3651dbc
commit 2496e03033
2 changed files with 81 additions and 1 deletions

View File

@ -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<usize>,
pub color_mode: Option<ColorMode>,
pub color_temp: Option<usize>,
pub color: Option<IncomingColor>,
pub effect: Option<String>,
pub state: Option<String>,
pub transition: Option<usize>,
}
#[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<usize>,
pub g: Option<usize>,
pub b: Option<usize>,
pub c: Option<usize>,
pub w: Option<usize>,
pub x: Option<f32>,
pub y: Option<f32>,
pub h: Option<f32>,
pub s: Option<f32>,
}
}
pub mod lock {
use super::*;

View File

@ -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::<JsonIncoming>(test_data).unwrap(),
);
assert_eq!(
serde_json::from_str::<Value>(&serde_json::to_string(&expected).unwrap()).unwrap(),
serde_json::from_str::<Value>(test_data).unwrap()
);
}
#[test]
fn light_default() {
let test_data = r#"{