From cf88768c15e9adbb8b9b2716b2f3343a0adbf021 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Mon, 9 Jan 2023 20:27:59 +0100 Subject: [PATCH] Made hue_bridge and ntfy optional --- config/ares.dev.toml | 40 ++++++++++++++++++++++++++++++++++++++++ src/config.rs | 4 ++-- src/main.rs | 22 ++++++++++++++-------- src/ntfy.rs | 4 +++- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/config/ares.dev.toml b/config/ares.dev.toml index 08a0837..f3a3fca 100644 --- a/config/ares.dev.toml +++ b/config/ares.dev.toml @@ -1,4 +1,44 @@ +[openid] +base_url = "https://login.huizinga.dev/api/oidc" + [mqtt] host="olympus.vpn.huizinga.dev" port=8883 username="mqtt" +password="${MQTT_PASSWORD}" + +[ntfy] +topic = "${NTFY_TOPIC}" + +[presence] +topic = "automation_dev/presence/+/#" + +[light_sensor] +topic = "zigbee2mqtt_dev/living/light" +min = 23_000 +max = 25_000 + +[devices.kitchen_kettle] +type = "IkeaOutlet" +name = "Kettle" +room = "Kitchen" +topic = "zigbee2mqtt/kitchen/kettle" +kettle = { timeout = 5 } + +[devices.living_workbench] +type = "IkeaOutlet" +name = "Workbench" +room = "Living Room" +topic = "zigbee2mqtt/living/workbench" + +[devices.living_zeus] +type = "WakeOnLAN" +name = "Zeus" +room = "Living Room" +topic = "automation/appliance/living_room/zeus" +mac_address = "30:9c:23:60:9c:13" + +[devices.hallway_frontdoor] +type = "ContactSensor" +topic = "zigbee2mqtt/hallway/frontdoor" +presence = { timeout = 10 } diff --git a/src/config.rs b/src/config.rs index e34374a..ccc8a8f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -15,10 +15,10 @@ pub struct Config { pub mqtt: MqttConfig, #[serde(default)] pub fullfillment: FullfillmentConfig, - pub ntfy: NtfyConfig, + pub ntfy: Option, pub presence: MqttDeviceConfig, pub light_sensor: LightSensorConfig, - pub hue_bridge: HueBridgeConfig, + pub hue_bridge: Option, #[serde(default)] pub devices: HashMap } diff --git a/src/main.rs b/src/main.rs index 5d36d7e..2d51bc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,20 +76,26 @@ async fn main() { // Register devices as presence listener presence.add_listener(Arc::downgrade(&devices)); - let ntfy = Arc::new(RwLock::new(Ntfy::new(config.ntfy))); - presence.add_listener(Arc::downgrade(&ntfy)); + let mut light_sensor = LightSensor::new(config.light_sensor, client.clone()); + light_sensor.add_listener(Arc::downgrade(&devices)); - let hue_bridge = Arc::new(RwLock::new(HueBridge::new(config.hue_bridge))); - presence.add_listener(Arc::downgrade(&hue_bridge)); + let ntfy; + if let Some(ntfy_config) = config.ntfy { + ntfy = Arc::new(RwLock::new(Ntfy::new(ntfy_config))); + presence.add_listener(Arc::downgrade(&ntfy)); + } + + let hue_bridge; + if let Some(hue_bridge_config) = config.hue_bridge { + hue_bridge = Arc::new(RwLock::new(HueBridge::new(hue_bridge_config))); + presence.add_listener(Arc::downgrade(&hue_bridge)); + light_sensor.add_listener(Arc::downgrade(&hue_bridge)); + } // Register presence as mqtt listener let presence = Arc::new(RwLock::new(presence)); mqtt.add_listener(Arc::downgrade(&presence)); - let mut light_sensor = LightSensor::new(config.light_sensor, client.clone()); - light_sensor.add_listener(Arc::downgrade(&devices)); - light_sensor.add_listener(Arc::downgrade(&hue_bridge)); - let light_sensor = Arc::new(RwLock::new(light_sensor)); mqtt.add_listener(Arc::downgrade(&light_sensor)); diff --git a/src/ntfy.rs b/src/ntfy.rs index de2b36f..e754caa 100644 --- a/src/ntfy.rs +++ b/src/ntfy.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use tracing::{warn, error}; +use tracing::{warn, error, debug}; use serde::Serialize; use serde_repr::*; use pollster::FutureExt as _; @@ -116,6 +116,8 @@ impl OnPresence for Ntfy { .add_action(action) .set_priority(Priority::Low); + debug!("Notifying presence as {presence}"); + // Create the request let res = reqwest::Client::new() .post(self.base_url.clone())