Add battery sensor to discovery
This commit is contained in:
2 files changed
+89
-9
No files matched your search
@@ -27,6 +27,8 @@ pub(crate) enum Discoveries {
|
||||
Reboot,
|
||||
#[serde(alias = "pt")]
|
||||
Pt,
|
||||
#[serde(alias = "battery", alias = "power")]
|
||||
Battery,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -181,6 +183,20 @@ struct DiscoveryButton {
|
||||
payload_press: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct DiscoverySensor {
|
||||
name: String,
|
||||
unique_id: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
icon: Option<String>,
|
||||
device: DiscoveryDevice,
|
||||
availability: DiscoveryAvaliablity,
|
||||
// Button specific
|
||||
state_topic: String,
|
||||
state_class: String,
|
||||
unit_of_measurement: String,
|
||||
}
|
||||
|
||||
/// Enables MQTT discovery for a camera. See docs at https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery
|
||||
pub(crate) async fn enable_discovery(
|
||||
discovery_config: &MqttDiscoveryConfig,
|
||||
@@ -503,6 +519,44 @@ pub(crate) async fn enable_discovery(
|
||||
})?;
|
||||
}
|
||||
}
|
||||
Discoveries::Battery => {
|
||||
let config_data = DiscoverySensor {
|
||||
// Common across all potential features
|
||||
device: device.clone(),
|
||||
availability: availability.clone(),
|
||||
|
||||
// Identifiers
|
||||
name: format!("{} Battery", friendly_name.as_str()),
|
||||
unique_id: format!("neolink_{}_battery", cam_config.name),
|
||||
icon: Some("mdi:battery".to_string()),
|
||||
|
||||
// Camera specific
|
||||
state_topic: format!("neolink/{}/status/battery_level", cam_config.name),
|
||||
state_class: "measurement".to_string(),
|
||||
unit_of_measurement: "%".to_string(),
|
||||
};
|
||||
|
||||
// Each feature needs to be individually registered
|
||||
mqtt_sender
|
||||
.send_message_with_root_topic(
|
||||
&format!(
|
||||
"{}/sensor/{}",
|
||||
discovery_config.topic, &config_data.unique_id
|
||||
),
|
||||
"config",
|
||||
&serde_json::to_string(&config_data).with_context(|| {
|
||||
"Cound not serialise discovery battery config into json"
|
||||
})?,
|
||||
true,
|
||||
)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Failed to publish battery auto-discover data on over MQTT for {}",
|
||||
cam_config.name
|
||||
)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+35
-9
@@ -216,8 +216,8 @@ impl EventCamThread {
|
||||
tx: self.tx.clone(),
|
||||
camera: arc_cam.clone(),
|
||||
};
|
||||
|
||||
let mut battery_thread = BatteryThread {
|
||||
|
||||
let mut battery_thread = BatteryLevelThread {
|
||||
tx: self.tx.clone(),
|
||||
camera: arc_cam.clone(),
|
||||
};
|
||||
@@ -372,10 +372,22 @@ struct SnapThread {
|
||||
|
||||
impl SnapThread {
|
||||
async fn run(&mut self) -> Result<()> {
|
||||
let mut inter = interval(Duration::from_millis(500));
|
||||
let mut tries = 0;
|
||||
let base_duration = Duration::from_millis(500);
|
||||
loop {
|
||||
inter.tick().await;
|
||||
let snapshot = self.camera.get_snapshot().await?;
|
||||
tokio::time::sleep(base_duration.saturating_mul(tries)).await;
|
||||
let snapshot = match self.camera.get_snapshot().await {
|
||||
Ok(info) => {
|
||||
tries = 1;
|
||||
info
|
||||
}
|
||||
Err(neolink_core::Error::UnintelligibleReply { .. }) => {
|
||||
// Try again later
|
||||
tries += 1;
|
||||
continue;
|
||||
}
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
self.tx.send(Messages::Snap(snapshot)).await?;
|
||||
}
|
||||
}
|
||||
@@ -388,11 +400,25 @@ struct BatteryLevelThread {
|
||||
|
||||
impl BatteryLevelThread {
|
||||
async fn run(&mut self) -> Result<()> {
|
||||
let mut inter = interval(Duration::from_millis(500));
|
||||
let mut tries = 0;
|
||||
let base_duration = Duration::from_millis(500);
|
||||
loop {
|
||||
inter.tick().await;
|
||||
let battery = self.camera.battery_info().await?;
|
||||
self.tx.send(Messages::BatteryLevel(battery.battery_percent)).await?;
|
||||
tokio::time::sleep(base_duration.saturating_mul(tries)).await;
|
||||
let battery = match self.camera.battery_info().await {
|
||||
Ok(info) => {
|
||||
tries = 1;
|
||||
info
|
||||
}
|
||||
Err(neolink_core::Error::UnintelligibleReply { .. }) => {
|
||||
// Try again later
|
||||
tries += 1;
|
||||
continue;
|
||||
}
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
self.tx
|
||||
.send(Messages::BatteryLevel(battery.battery_percent))
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user