use serde::{Deserialize, Serialize};
use crate::config::LightSettings;
use crate::device::LightCapabilities;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LightCommand {
Power(bool),
BrightnessPercent(u8),
TemperatureKelvin(u16),
BrightnessNative(u16),
}
#[must_use]
pub fn commands_for_light_settings(
settings: LightSettings,
capabilities: LightCapabilities,
) -> Vec<LightCommand> {
let mut commands = Vec::new();
if capabilities.power {
commands.push(LightCommand::Power(settings.enabled));
}
if capabilities.brightness.is_some() {
commands.push(LightCommand::BrightnessPercent(settings.brightness_percent));
}
if capabilities.temperature.is_some()
&& let Some(kelvin) = settings.temperature_kelvin
{
commands.push(LightCommand::TemperatureKelvin(kelvin));
}
commands
}
#[cfg(test)]
mod tests {
use super::{LightCommand, commands_for_light_settings};
use crate::config::LightSettings;
use crate::device::{LightCapabilities, LightValueRange, LightValueUnit};
#[test]
fn light_settings_expand_only_to_advertised_controls() {
let Ok(brightness) = LightValueRange::new(0, 100, 1, LightValueUnit::Percent) else {
panic!("valid brightness fixture");
};
let settings = LightSettings::new(false, 37, Some(4600));
let commands = commands_for_light_settings(
settings,
LightCapabilities {
brightness: Some(brightness),
..LightCapabilities::default()
},
);
assert_eq!(commands, vec![LightCommand::BrightnessPercent(37)]);
}
}