Skip to content

Commit

Permalink
feat(sensor): add state_class=measurement to temp./humid.
Browse files Browse the repository at this point in the history
  • Loading branch information
buschtoens authored and wez committed Apr 29, 2024
1 parent f32ed82 commit 98282e2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/hass_mqtt/sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@ pub struct SensorConfig {

pub state_topic: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub state_class: Option<StateClass>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit_of_measurement: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub json_attributes_topic: Option<String>,
}

#[allow(unused)]
#[derive(Serialize, Clone, Copy, Debug, PartialEq, Eq)]
pub enum StateClass {
#[serde(rename="measurement")]
Measurement,
#[serde(rename="total")]
Total,
#[serde(rename="total_increasing")]
TotalIncreasing
}

impl SensorConfig {
pub async fn publish(&self, state: &StateHandle, client: &HassClient) -> anyhow::Result<()> {
publish_entity_config("sensor", state, client, &self.base, self).await
Expand Down Expand Up @@ -70,6 +83,7 @@ impl GlobalFixedDiagnostic {
icon: None,
},
state_topic: format!("gv2mqtt/sensor/{unique_id}/state"),
state_class: None,
unit_of_measurement: None,
json_attributes_topic: None,
},
Expand Down Expand Up @@ -110,6 +124,12 @@ impl CapabilitySensor {
_ => None,
};

let state_class = match instance.instance.as_str() {
"sensorTemperature" => Some(StateClass::Measurement),
"sensorHumidity" => Some(StateClass::Measurement),
_ => None,
};

let name = match instance.instance.as_str() {
"sensorTemperature" => "Temperature".to_string(),
"sensorHumidity" => "Humidity".to_string(),
Expand All @@ -130,6 +150,7 @@ impl CapabilitySensor {
icon: None,
},
state_topic: format!("gv2mqtt/sensor/{unique_id}/state"),
state_class: state_class,
unit_of_measurement,
json_attributes_topic: None,
},
Expand Down Expand Up @@ -227,6 +248,7 @@ impl DeviceStatusDiagnostic {
icon: None,
},
state_topic: format!("gv2mqtt/sensor/{unique_id}/state"),
state_class: None,
json_attributes_topic: Some(format!("gv2mqtt/sensor/{unique_id}/attributes")),
unit_of_measurement: None,
},
Expand Down

0 comments on commit 98282e2

Please sign in to comment.