Skip to content

Commit

Permalink
feat(hass): add expire_after property to occupancy sensor
Browse files Browse the repository at this point in the history
defaults to poll rate
  • Loading branch information
maxjoehnk committed Feb 6, 2021
1 parent 848d831 commit 3fbdb50
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/modules/home_assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ impl Module for HomeAssistantModule {
hass_config.name.clone(),
);
let backlight = config.backlight;
let expire_after = config.idle_poll_rate;
async move {
self.announce_occupancy(&hass_config, topic.clone(), device.clone())?;
self.announce_occupancy(&hass_config, topic.clone(), device.clone(), expire_after)?;
if backlight != BacklightProvider::None {
self.announce_backlight(&hass_config, topic, device)?;
}
Expand Down Expand Up @@ -70,6 +71,7 @@ impl HomeAssistantModule {
config: &HomeAssistantConfig,
topic: String,
device: Device,
expire_after: u64,
) -> anyhow::Result<()> {
let config_topic = format!(
"homeassistant/binary_sensor/{}/occupancy/config",
Expand All @@ -83,6 +85,7 @@ impl HomeAssistantModule {
SensorConfig {
device_class: "occupancy".to_string(),
value_template: "{{ value_json.occupancy }}".to_string(),
expire_after: Some(expire_after),
..Default::default()
},
);
Expand Down Expand Up @@ -148,6 +151,8 @@ pub struct SensorConfig {
pub value_template: String,
pub payload_off: bool,
pub payload_on: bool,
/// Defines the number of seconds after the sensor’s state expires, if it’s not updated.
pub expire_after: Option<u64>,
}

impl Default for SensorConfig {
Expand All @@ -157,6 +162,7 @@ impl Default for SensorConfig {
value_template: String::new(),
payload_on: true,
payload_off: false,
expire_after: None,
}
}
}
Expand Down

0 comments on commit 3fbdb50

Please sign in to comment.