Skip to content

Commit

Permalink
do a better job at filling out light metadata
Browse files Browse the repository at this point in the history
brightness, rgb and color temp are now conditionally enabled
based on available metadata.

But: if we don't have any http metadata, we'll report no
capabilities at all. There's not much we can do about this
without having some kind of offline device metadata database
to consult.
  • Loading branch information
wez committed Jan 4, 2024
1 parent 8555c52 commit 900c31d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
20 changes: 20 additions & 0 deletions src/platform_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,26 @@ impl HttpDeviceInfo {
pub fn capability_by_instance(&self, instance: &str) -> Option<&DeviceCapability> {
self.capabilities.iter().find(|c| c.instance == instance)
}

pub fn supports_rgb(&self) -> bool {
self.capability_by_instance("colorRgb").is_some()
}

pub fn supports_brightness(&self) -> bool {
self.capability_by_instance("brightness").is_some()
}

pub fn get_color_temperature_range(&self) -> Option<(u32, u32)> {
let cap = self.capability_by_instance("colorTemperatureK")?;

match cap.parameters {
DeviceParameters::Integer {
range: IntegerRange { min, max, .. },
..
} => Some((min, max)),
_ => None,
}
}
}

#[derive(Deserialize, Serialize, Debug, Default, Clone, Copy)]
Expand Down
48 changes: 41 additions & 7 deletions src/service/hass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ pub struct LightConfig {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub effect_list: Vec<String>,

pub min_mireds: Option<u32>,
pub max_mireds: Option<u32>,

pub payload_available: String,
}

Expand All @@ -237,6 +240,39 @@ impl LightConfig {

let effect_list = state.device_list_scenes(device).await?;

let mut supported_color_modes = vec![];
let mut color_mode = false;

if device
.http_device_info
.as_ref()
.map(|info| info.supports_rgb())
.unwrap_or(false)
{
supported_color_modes.push("rgb".to_string());
color_mode = true;
}

let (min_mireds, max_mireds) = if let Some((min, max)) = device
.http_device_info
.as_ref()
.and_then(|info| info.get_color_temperature_range())
{
supported_color_modes.push("color_temp".to_string());
color_mode = true;
// Note that min and max are swapped by the translation
// from kelvin to mired
(Some(kelvin_to_mired(max)), Some(kelvin_to_mired(min)))
} else {
(None, None)
};

let brightness = device
.http_device_info
.as_ref()
.map(|info| info.supports_brightness())
.unwrap_or(false);

Ok(Self {
base: EntityConfig {
availability_topic,
Expand All @@ -251,17 +287,15 @@ impl LightConfig {
schema: "json".to_string(),
command_topic,
state_topic,
supported_color_modes: vec![
"rgb".to_string(),
"color_temp".to_string(),
//"white".to_string(),
],
color_mode: true,
brightness: true,
supported_color_modes,
color_mode,
brightness,
brightness_scale: 100,
effect: true,
effect_list,
payload_available: "online".to_string(),
max_mireds,
min_mireds,
})
}

Expand Down

0 comments on commit 900c31d

Please sign in to comment.