Setting the presence mqtt topic is now optional, if not set it will generate an appropriate value automatically

This commit is contained in:
2023-01-06 04:42:42 +01:00
parent cbcbd05613
commit 27a63b1a79
4 changed files with 50 additions and 28 deletions

View File

@@ -44,8 +44,9 @@ async fn main() {
info!("Starting automation_rs...");
// Configure MQTT
let mut mqttoptions = MqttOptions::new("rust-test", config.mqtt.host, config.mqtt.port);
mqttoptions.set_credentials(config.mqtt.username, config.mqtt.password);
let mqtt = config.mqtt.clone();
let mut mqttoptions = MqttOptions::new("rust-test", mqtt.host, mqtt.port);
mqttoptions.set_credentials(mqtt.username, mqtt.password);
mqttoptions.set_keep_alive(Duration::from_secs(5));
mqttoptions.set_transport(Transport::tls_with_default_config());
@@ -57,6 +58,18 @@ async fn main() {
let devices = Arc::new(RwLock::new(Devices::new()));
mqtt.add_listener(Arc::downgrade(&devices));
// Turn the config into actual devices and add them
config.devices.clone()
.into_iter()
.map(|(identifier, device_config)| {
// This can technically block, but this only happens during start-up, so should not be
// a problem
device_config.into(identifier, &config, client.clone())
})
.for_each(|device| {
devices.write().unwrap().add_device(device);
});
// Setup presence system
let mut presence = Presence::new(config.presence, client.clone());
// Register devices as presence listener
@@ -82,18 +95,6 @@ async fn main() {
// Start mqtt, this spawns a seperate async task
mqtt.start();
// Turn the config into actual devices and add them
config.devices
.into_iter()
.map(|(identifier, device_config)| {
// This can technically block, but this only happens during start-up, so should not be
// a problem
device_config.into(identifier, client.clone())
})
.for_each(|device| {
devices.write().unwrap().add_device(device);
});
// Create google home fullfillment route
let fullfillment = Router::new()
.route("/google_home", post(async move |user: User, Json(payload): Json<Request>| {