openlogi_core/hid/
light.rs1use serde::{Deserialize, Serialize};
7
8use crate::config::LightSettings;
9use crate::device::LightCapabilities;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
13pub enum LightCommand {
14 Power(bool),
16 BrightnessPercent(u8),
18 TemperatureKelvin(u16),
20 BrightnessNative(u16),
24}
25
26#[must_use]
30pub fn commands_for_light_settings(
31 settings: LightSettings,
32 capabilities: LightCapabilities,
33) -> Vec<LightCommand> {
34 let mut commands = Vec::new();
35 if capabilities.power {
36 commands.push(LightCommand::Power(settings.enabled));
37 }
38 if capabilities.brightness.is_some() {
39 commands.push(LightCommand::BrightnessPercent(settings.brightness_percent));
40 }
41 if capabilities.temperature.is_some()
42 && let Some(kelvin) = settings.temperature_kelvin
43 {
44 commands.push(LightCommand::TemperatureKelvin(kelvin));
45 }
46 commands
47}
48
49#[cfg(test)]
50mod tests {
51 use super::{LightCommand, commands_for_light_settings};
52 use crate::config::LightSettings;
53 use crate::device::{LightCapabilities, LightValueRange, LightValueUnit};
54
55 #[test]
56 fn light_settings_expand_only_to_advertised_controls() {
57 let Ok(brightness) = LightValueRange::new(0, 100, 1, LightValueUnit::Percent) else {
58 panic!("valid brightness fixture");
59 };
60 let settings = LightSettings::new(false, 37, Some(4600));
61 let commands = commands_for_light_settings(
62 settings,
63 LightCapabilities {
64 brightness: Some(brightness),
65 ..LightCapabilities::default()
66 },
67 );
68
69 assert_eq!(commands, vec![LightCommand::BrightnessPercent(37)]);
70 }
71}