1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use crate::{topic::Topic, validation::CustomValidation};
use hass_mqtt_discovery_macros::entity_document;
use std::borrow::Cow;
#[entity_document]
#[entity(extend_json(schema = "json"))]
#[entity(validate(ColorModeWithoutSupportedColorModes))]
pub struct Light<'a> {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub brightness: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub brightness_scale: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub color_mode: Option<bool>,
#[serde(borrow)]
pub command_topic: Topic<'a>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub effect: Option<bool>,
#[serde(borrow, default, skip_serializing_if = "<[Cow<str>]>::is_empty")]
pub effect_list: Cow<'a, [Cow<'a, str>]>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flash_time_long: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flash_time_short: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_mireds: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_mireds: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub optimistic: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub retain: Option<bool>,
#[serde(borrow, default, skip_serializing_if = "Option::is_none")]
pub state_topic: Option<Topic<'a>>,
#[serde(borrow, default, skip_serializing_if = "<[ColorMode]>::is_empty")]
pub supported_color_modes: Cow<'a, [ColorMode]>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub white_value_scale: Option<u8>,
}
impl<'a> CustomValidation for Light<'a> {
type Invalidity = LightInvalidity;
fn additional_validation(
&self,
context: semval::context::Context<Self::Invalidity>,
) -> semval::context::Context<Self::Invalidity> {
context.invalidate_if(
self.color_mode == Some(true) && self.supported_color_modes.is_empty(),
LightInvalidity::ColorModeWithoutSupportedColorModes,
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum ColorMode {
#[serde(rename = "onoff")]
OnOff,
#[serde(rename = "brightness")]
Brightness,
#[serde(rename = "color_temp")]
ColorTemp,
#[serde(rename = "hs")]
Hs,
#[serde(rename = "xy")]
Xy,
#[serde(rename = "rgb")]
Rgb,
#[serde(rename = "rgbw")]
Rgbw,
#[serde(rename = "rgbww")]
Rgbww,
#[serde(rename = "white")]
White,
}