Skip to content

Commit

Permalink
HA messages are retained now
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRichterHuber committed May 7, 2024
1 parent 4c2168f commit f9bb620
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ pub fn read_configuration() -> AppConfig {
if config
.mqtt
.as_ref()
.and_then(|c| Some(c.password.is_none() && c.password_file.is_some()))
.map(|c| c.password.is_none() && c.password_file.is_some())
.unwrap_or(false)
{
let mqttconfig = config.mqtt.unwrap();
let file = mqttconfig.password_file.as_ref().unwrap();

let filepw = std::fs::read_to_string(&file);
let filepw = std::fs::read_to_string(file);

config = match filepw {
Ok(pw) => AppConfig {
Expand Down Expand Up @@ -155,5 +155,5 @@ pub fn read_configuration() -> AppConfig {
};
}

return config;
config
}
12 changes: 6 additions & 6 deletions src/homeassistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub async fn publish_homeassistant_device_discovery_messages(

let topic_temperature = format!(
"homeassistant/sensor/thermobeacon/{}_temperature/config",
device.mac.replace(":", "_")
device.mac.replace(':', "_")
);
let device_id = MQTTDiscoveryDevice {
identifiers: vec![device.mac.clone()],
Expand Down Expand Up @@ -68,7 +68,7 @@ pub async fn publish_homeassistant_device_discovery_messages(

let topic_humidity = format!(
"homeassistant/sensor/thermobeacon/{}_humidity/config",
device.mac.replace(":", "_")
device.mac.replace(':', "_")
);
let payload_humidity = MQTTDiscovery {
device_class: "humidity".to_string(),
Expand All @@ -81,7 +81,7 @@ pub async fn publish_homeassistant_device_discovery_messages(

let topic_battery = format!(
"homeassistant/sensor/thermobeacon/{}_battery/config",
device.mac.replace(":", "_")
device.mac.replace(':', "_")
);
let payload_battery = MQTTDiscovery {
device_class: "battery".to_string(),
Expand All @@ -98,7 +98,7 @@ pub async fn publish_homeassistant_device_discovery_messages(
topic_temperature,
serde_json::to_string(&payload_temperature).unwrap()
);
cli.publish(mqtt::Message::new(
cli.publish(mqtt::Message::new_retained(
topic_temperature,
serde_json::to_string(&payload_temperature).unwrap(),
1,
Expand All @@ -111,7 +111,7 @@ pub async fn publish_homeassistant_device_discovery_messages(
topic_humidity,
serde_json::to_string(&payload_humidity).unwrap()
);
cli.publish(mqtt::Message::new(
cli.publish(mqtt::Message::new_retained(
topic_humidity,
serde_json::to_string(&payload_humidity).unwrap(),
1,
Expand All @@ -124,7 +124,7 @@ pub async fn publish_homeassistant_device_discovery_messages(
topic_battery,
serde_json::to_string(&payload_battery).unwrap()
);
cli.publish(mqtt::Message::new(
cli.publish(mqtt::Message::new_retained(
topic_battery,
serde_json::to_string(&payload_battery).unwrap(),
1,
Expand Down
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Message {
name: String,
}

// Tries to connect to the MQTT server using the given MqttConfig
/// Tries to connect to the MQTT server using the given MqttConfig
pub async fn connect_to_mqtt(
mqtt_config: &MqttConfig,
) -> Result<AsyncClient, Box<dyn Error + Send + Sync>> {
Expand Down Expand Up @@ -100,7 +100,7 @@ async fn collect_and_print_results(

/// Collects all results and sends them to the given MQTT client
async fn collect_and_send_results(
cli: &AsyncClient,
client: &AsyncClient,
devices: &[AppDevice],
manager: &Manager,
seconds_to_scan: u64,
Expand Down Expand Up @@ -148,7 +148,7 @@ async fn collect_and_send_results(
} else {
mqtt::Message::new_retained(topic, payload, qos)
};
cli.publish(msg).await?;
client.publish(msg).await?;
}

Ok(())
Expand All @@ -158,11 +158,11 @@ async fn collect_and_send_results(
async fn job(
config: &AppConfig,
manager: &Manager,
client: Option<AsyncClient>,
client: &Option<AsyncClient>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match client {
Some(c) => {
collect_and_send_results(&c, &config.devices, manager, config.seconds_to_scan).await?;
collect_and_send_results(c, &config.devices, manager, config.seconds_to_scan).await?;
}
None => {
warn!("No valid mqtt configuration found. Results are just printed to the console");
Expand Down Expand Up @@ -201,7 +201,7 @@ async fn run_scheduled(
// Sleep until the next run
tokio::time::sleep_until(instant).await;
// Finally execute run
match job(&config, &manager, client.clone()).await {
match job(&config, &manager, &client).await {
Ok(()) => {
set_health_status(HealthStatus::Ok);
debug!("Run was successful");
Expand Down Expand Up @@ -270,7 +270,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.unwrap();
} else {
info!("No cron descriptor found -> job is executed just once!");
match job(&config, &manager, client).await {
match job(&config, &manager, &client).await {
Ok(()) => {
set_health_status(HealthStatus::Ok);
debug!("Run was successful");
Expand Down

0 comments on commit f9bb620

Please sign in to comment.