Improved how devices are created, ntfy and presence are now treated like any other device
This commit is contained in:
@@ -6,10 +6,9 @@ use google_home::traits::{AvailableSpeeds, FanSpeed, HumiditySetting, OnOff, Spe
|
||||
use google_home::types::Type;
|
||||
use google_home::GoogleHomeDevice;
|
||||
use rumqttc::Publish;
|
||||
use tracing::{debug, error, warn};
|
||||
use tracing::{debug, error, trace, warn};
|
||||
|
||||
use crate::config::{InfoConfig, MqttDeviceConfig};
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::OnMqtt;
|
||||
@@ -26,25 +25,8 @@ pub struct AirFilterConfig {
|
||||
client: WrappedAsyncClient,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for AirFilterConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
let device = AirFilter {
|
||||
identifier: identifier.into(),
|
||||
config: self.clone(),
|
||||
last_known_state: AirFilterState {
|
||||
state: AirFilterFanState::Off,
|
||||
humidity: 0.0,
|
||||
},
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct AirFilter {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: AirFilterConfig,
|
||||
|
||||
@@ -71,9 +53,22 @@ impl AirFilter {
|
||||
}
|
||||
}
|
||||
|
||||
impl AirFilter {
|
||||
async fn create(config: AirFilterConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.info.identifier(), "Setting up AirFilter");
|
||||
Ok(Self {
|
||||
config,
|
||||
last_known_state: AirFilterState {
|
||||
state: AirFilterFanState::Off,
|
||||
humidity: 0.0,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for AirFilter {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.info.identifier()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +82,7 @@ impl OnMqtt for AirFilter {
|
||||
let state = match AirFilterState::try_from(message) {
|
||||
Ok(state) => state,
|
||||
Err(err) => {
|
||||
error!(id = self.identifier, "Failed to parse message: {err}");
|
||||
error!(id = Device::get_id(self), "Failed to parse message: {err}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -96,7 +91,7 @@ impl OnMqtt for AirFilter {
|
||||
return;
|
||||
}
|
||||
|
||||
debug!(id = self.identifier, "Updating state to {state:?}");
|
||||
debug!(id = Device::get_id(self), "Updating state to {state:?}");
|
||||
|
||||
self.last_known_state = state;
|
||||
}
|
||||
@@ -111,7 +106,7 @@ impl GoogleHomeDevice for AirFilter {
|
||||
Name::new(&self.config.info.name)
|
||||
}
|
||||
|
||||
fn get_id(&self) -> &str {
|
||||
fn get_id(&self) -> String {
|
||||
Device::get_id(self)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ use tracing::{debug, error, trace, warn};
|
||||
|
||||
use super::Device;
|
||||
use crate::config::MqttDeviceConfig;
|
||||
use crate::device_manager::{DeviceConfig, WrappedDevice};
|
||||
use crate::device_manager::WrappedDevice;
|
||||
use crate::devices::As;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{OnMqtt, OnPresence};
|
||||
@@ -13,6 +13,7 @@ use crate::messages::{RemoteAction, RemoteMessage};
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct AudioSetupConfig {
|
||||
identifier: String,
|
||||
#[device_config(flatten)]
|
||||
mqtt: MqttDeviceConfig,
|
||||
#[device_config(from_lua)]
|
||||
@@ -21,41 +22,33 @@ pub struct AudioSetupConfig {
|
||||
speakers: WrappedDevice,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for AudioSetupConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
trace!(id = identifier, "Setting up AudioSetup");
|
||||
|
||||
let mixer_id = self.mixer.read().await.get_id().to_owned();
|
||||
if !As::<dyn OnOff>::is(self.mixer.read().await.as_ref()) {
|
||||
return Err(DeviceConfigError::MissingTrait(mixer_id, "OnOff".into()));
|
||||
}
|
||||
|
||||
let speakers_id = self.speakers.read().await.get_id().to_owned();
|
||||
if !As::<dyn OnOff>::is(self.speakers.read().await.as_ref()) {
|
||||
return Err(DeviceConfigError::MissingTrait(speakers_id, "OnOff".into()));
|
||||
}
|
||||
|
||||
let device = AudioSetup {
|
||||
identifier: identifier.into(),
|
||||
config: self.clone(),
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: We need a better way to store the children devices
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct AudioSetup {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: AudioSetupConfig,
|
||||
}
|
||||
|
||||
impl AudioSetup {
|
||||
async fn create(config: AudioSetupConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.identifier, "Setting up AudioSetup");
|
||||
|
||||
let mixer_id = config.mixer.read().await.get_id().to_owned();
|
||||
if !As::<dyn OnOff>::is(config.mixer.read().await.as_ref()) {
|
||||
return Err(DeviceConfigError::MissingTrait(mixer_id, "OnOff".into()));
|
||||
}
|
||||
|
||||
let speakers_id = config.speakers.read().await.get_id().to_owned();
|
||||
if !As::<dyn OnOff>::is(config.speakers.read().await.as_ref()) {
|
||||
return Err(DeviceConfigError::MissingTrait(speakers_id, "OnOff".into()));
|
||||
}
|
||||
|
||||
Ok(AudioSetup { config })
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for AudioSetup {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.identifier.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +62,10 @@ impl OnMqtt for AudioSetup {
|
||||
let action = match RemoteMessage::try_from(message) {
|
||||
Ok(message) => message.action(),
|
||||
Err(err) => {
|
||||
error!(id = self.identifier, "Failed to parse message: {err}");
|
||||
error!(
|
||||
id = self.config.identifier,
|
||||
"Failed to parse message: {err}"
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -118,7 +114,7 @@ impl OnPresence for AudioSetup {
|
||||
) {
|
||||
// Turn off the audio setup when we leave the house
|
||||
if !presence {
|
||||
debug!(id = self.identifier, "Turning devices off");
|
||||
debug!(id = self.config.identifier, "Turning devices off");
|
||||
speakers.set_on(false).await.unwrap();
|
||||
mixer.set_on(false).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use tracing::{debug, error, trace, warn};
|
||||
|
||||
use super::Device;
|
||||
use crate::config::MqttDeviceConfig;
|
||||
use crate::device_manager::{DeviceConfig, WrappedDevice};
|
||||
use crate::device_manager::WrappedDevice;
|
||||
use crate::devices::{As, DEFAULT_PRESENCE};
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{OnMqtt, OnPresence};
|
||||
@@ -51,6 +51,7 @@ pub struct TriggerConfig {
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct ContactSensorConfig {
|
||||
identifier: String,
|
||||
#[device_config(flatten)]
|
||||
mqtt: MqttDeviceConfig,
|
||||
#[device_config(from_lua)]
|
||||
@@ -61,13 +62,22 @@ pub struct ContactSensorConfig {
|
||||
client: WrappedAsyncClient,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for ContactSensorConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
trace!(id = identifier, "Setting up ContactSensor");
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct ContactSensor {
|
||||
#[config]
|
||||
config: ContactSensorConfig,
|
||||
|
||||
overall_presence: bool,
|
||||
is_closed: bool,
|
||||
handle: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl ContactSensor {
|
||||
async fn create(config: ContactSensorConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.identifier, "Setting up ContactSensor");
|
||||
|
||||
// Make sure the devices implement the required traits
|
||||
if let Some(trigger) = &self.trigger {
|
||||
if let Some(trigger) = &config.trigger {
|
||||
for (device, _) in &trigger.devices {
|
||||
let id = device.read().await.get_id().to_owned();
|
||||
if !As::<dyn OnOff>::is(device.read().await.as_ref()) {
|
||||
@@ -81,32 +91,18 @@ impl DeviceConfig for ContactSensorConfig {
|
||||
}
|
||||
}
|
||||
|
||||
let device = ContactSensor {
|
||||
identifier: identifier.into(),
|
||||
config: self.clone(),
|
||||
Ok(Self {
|
||||
config: config.clone(),
|
||||
overall_presence: DEFAULT_PRESENCE,
|
||||
is_closed: true,
|
||||
handle: None,
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct ContactSensor {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: ContactSensorConfig,
|
||||
|
||||
overall_presence: bool,
|
||||
is_closed: bool,
|
||||
handle: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl Device for ContactSensor {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.identifier.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +123,10 @@ impl OnMqtt for ContactSensor {
|
||||
let is_closed = match ContactMessage::try_from(message) {
|
||||
Ok(state) => state.is_closed(),
|
||||
Err(err) => {
|
||||
error!(id = self.identifier, "Failed to parse message: {err}");
|
||||
error!(
|
||||
id = self.config.identifier,
|
||||
"Failed to parse message: {err}"
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -136,7 +135,7 @@ impl OnMqtt for ContactSensor {
|
||||
return;
|
||||
}
|
||||
|
||||
debug!(id = self.identifier, "Updating state to {is_closed}");
|
||||
debug!(id = self.config.identifier, "Updating state to {is_closed}");
|
||||
self.is_closed = is_closed;
|
||||
|
||||
if let Some(trigger) = &mut self.config.trigger {
|
||||
@@ -205,7 +204,7 @@ impl OnMqtt for ContactSensor {
|
||||
} else {
|
||||
// Once the door is closed again we start a timeout for removing the presence
|
||||
let client = self.config.client.clone();
|
||||
let id = self.identifier.clone();
|
||||
let id = self.config.identifier.clone();
|
||||
let timeout = presence.timeout;
|
||||
let topic = presence.mqtt.topic.clone();
|
||||
self.handle = Some(tokio::spawn(async move {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use async_trait::async_trait;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use tracing::warn;
|
||||
use tracing::{trace, warn};
|
||||
|
||||
use crate::config::MqttDeviceConfig;
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{OnDarkness, OnPresence};
|
||||
@@ -12,34 +11,29 @@ use crate::mqtt::WrappedAsyncClient;
|
||||
|
||||
#[derive(Debug, LuaDeviceConfig, Clone)]
|
||||
pub struct DebugBridgeConfig {
|
||||
identifier: String,
|
||||
#[device_config(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
#[device_config(from_lua)]
|
||||
client: WrappedAsyncClient,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for DebugBridgeConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
let device = DebugBridge {
|
||||
identifier: identifier.into(),
|
||||
config: self.clone(),
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct DebugBridge {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: DebugBridgeConfig,
|
||||
}
|
||||
|
||||
impl DebugBridge {
|
||||
async fn create(config: DebugBridgeConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.identifier, "Setting up DebugBridge");
|
||||
Ok(Self { config })
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for DebugBridge {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.identifier.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing::{error, trace, warn};
|
||||
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{OnDarkness, OnPresence};
|
||||
@@ -24,27 +23,15 @@ pub struct FlagIDs {
|
||||
|
||||
#[derive(Debug, LuaDeviceConfig, Clone)]
|
||||
pub struct HueBridgeConfig {
|
||||
pub identifier: String,
|
||||
#[device_config(rename("ip"), with(|ip| SocketAddr::new(ip, 80)))]
|
||||
pub addr: SocketAddr,
|
||||
pub login: String,
|
||||
pub flags: FlagIDs,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for HueBridgeConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
let device = HueBridge {
|
||||
identifier: identifier.into(),
|
||||
config: self.clone(),
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct HueBridge {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: HueBridgeConfig,
|
||||
}
|
||||
@@ -55,6 +42,11 @@ struct FlagMessage {
|
||||
}
|
||||
|
||||
impl HueBridge {
|
||||
async fn create(config: HueBridgeConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.identifier, "Setting up HueBridge");
|
||||
Ok(Self { config })
|
||||
}
|
||||
|
||||
pub async fn set_flag(&self, flag: Flag, value: bool) {
|
||||
let flag_id = match flag {
|
||||
Flag::Presence => self.config.flags.presence,
|
||||
@@ -88,8 +80,8 @@ impl HueBridge {
|
||||
}
|
||||
|
||||
impl Device for HueBridge {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.identifier.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,10 @@ use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use google_home::errors::ErrorCode;
|
||||
use google_home::traits::OnOff;
|
||||
use rumqttc::Publish;
|
||||
use tracing::{debug, error, warn};
|
||||
use tracing::{debug, error, trace, warn};
|
||||
|
||||
use super::Device;
|
||||
use crate::config::MqttDeviceConfig;
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::OnMqtt;
|
||||
use crate::messages::{RemoteAction, RemoteMessage};
|
||||
@@ -19,6 +18,7 @@ use crate::traits::Timeout;
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct HueGroupConfig {
|
||||
identifier: String,
|
||||
#[device_config(rename("ip"), with(|ip| SocketAddr::new(ip, 80)))]
|
||||
pub addr: SocketAddr,
|
||||
pub login: String,
|
||||
@@ -29,27 +29,19 @@ pub struct HueGroupConfig {
|
||||
pub remotes: Vec<MqttDeviceConfig>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for HueGroupConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
let device = HueGroup {
|
||||
identifier: identifier.into(),
|
||||
config: self.clone(),
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct HueGroup {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: HueGroupConfig,
|
||||
}
|
||||
|
||||
// Couple of helper function to get the correct urls
|
||||
impl HueGroup {
|
||||
async fn create(config: HueGroupConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.identifier, "Setting up AudioSetup");
|
||||
Ok(Self { config })
|
||||
}
|
||||
|
||||
fn url_base(&self) -> String {
|
||||
format!("http://{}/api/{}", self.config.addr, self.config.login)
|
||||
}
|
||||
@@ -68,8 +60,8 @@ impl HueGroup {
|
||||
}
|
||||
|
||||
impl Device for HueGroup {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.identifier.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +79,10 @@ impl OnMqtt for HueGroup {
|
||||
let action = match RemoteMessage::try_from(message) {
|
||||
Ok(message) => message.action(),
|
||||
Err(err) => {
|
||||
error!(id = self.identifier, "Failed to parse message: {err}");
|
||||
error!(
|
||||
id = self.config.identifier,
|
||||
"Failed to parse message: {err}"
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -126,10 +121,13 @@ impl OnOff for HueGroup {
|
||||
Ok(res) => {
|
||||
let status = res.status();
|
||||
if !status.is_success() {
|
||||
warn!(id = self.identifier, "Status code is not success: {status}");
|
||||
warn!(
|
||||
id = self.config.identifier,
|
||||
"Status code is not success: {status}"
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(err) => error!(id = self.identifier, "Error: {err}"),
|
||||
Err(err) => error!(id = self.config.identifier, "Error: {err}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -145,13 +143,19 @@ impl OnOff for HueGroup {
|
||||
Ok(res) => {
|
||||
let status = res.status();
|
||||
if !status.is_success() {
|
||||
warn!(id = self.identifier, "Status code is not success: {status}");
|
||||
warn!(
|
||||
id = self.config.identifier,
|
||||
"Status code is not success: {status}"
|
||||
);
|
||||
}
|
||||
|
||||
let on = match res.json::<message::Info>().await {
|
||||
Ok(info) => info.any_on(),
|
||||
Err(err) => {
|
||||
error!(id = self.identifier, "Failed to parse message: {err}");
|
||||
error!(
|
||||
id = self.config.identifier,
|
||||
"Failed to parse message: {err}"
|
||||
);
|
||||
// TODO: Error code
|
||||
return Ok(false);
|
||||
}
|
||||
@@ -159,7 +163,7 @@ impl OnOff for HueGroup {
|
||||
|
||||
return Ok(on);
|
||||
}
|
||||
Err(err) => error!(id = self.identifier, "Error: {err}"),
|
||||
Err(err) => error!(id = self.config.identifier, "Error: {err}"),
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
|
||||
@@ -13,7 +13,6 @@ use tokio::task::JoinHandle;
|
||||
use tracing::{debug, error, trace, warn};
|
||||
|
||||
use crate::config::{InfoConfig, MqttDeviceConfig};
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{OnMqtt, OnPresence};
|
||||
@@ -46,30 +45,8 @@ pub struct IkeaOutletConfig {
|
||||
client: WrappedAsyncClient,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for IkeaOutletConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
trace!(
|
||||
id = identifier,
|
||||
name = self.info.name,
|
||||
room = self.info.room,
|
||||
"Setting up IkeaOutlet"
|
||||
);
|
||||
|
||||
let device = IkeaOutlet {
|
||||
identifier: identifier.into(),
|
||||
config: self.clone(),
|
||||
last_known_state: false,
|
||||
handle: None,
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct IkeaOutlet {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: IkeaOutletConfig,
|
||||
|
||||
@@ -94,9 +71,21 @@ async fn set_on(client: WrappedAsyncClient, topic: &str, on: bool) {
|
||||
.ok();
|
||||
}
|
||||
|
||||
impl IkeaOutlet {
|
||||
async fn create(config: IkeaOutletConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.info.identifier(), "Setting up IkeaOutlet");
|
||||
|
||||
Ok(Self {
|
||||
config,
|
||||
last_known_state: false,
|
||||
handle: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for IkeaOutlet {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.info.identifier()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +111,7 @@ impl OnMqtt for IkeaOutlet {
|
||||
let state = match OnOffMessage::try_from(message) {
|
||||
Ok(state) => state.state(),
|
||||
Err(err) => {
|
||||
error!(id = self.identifier, "Failed to parse message: {err}");
|
||||
error!(id = Device::get_id(self), "Failed to parse message: {err}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -135,7 +124,7 @@ impl OnMqtt for IkeaOutlet {
|
||||
// Abort any timer that is currently running
|
||||
self.stop_timeout().await.unwrap();
|
||||
|
||||
debug!(id = self.identifier, "Updating state to {state}");
|
||||
debug!(id = Device::get_id(self), "Updating state to {state}");
|
||||
self.last_known_state = state;
|
||||
|
||||
// If this is a kettle start a timeout for turning it of again
|
||||
@@ -146,7 +135,7 @@ impl OnMqtt for IkeaOutlet {
|
||||
let action = match RemoteMessage::try_from(message) {
|
||||
Ok(message) => message.action(),
|
||||
Err(err) => {
|
||||
error!(id = self.identifier, "Failed to parse message: {err}");
|
||||
error!(id = Device::get_id(self), "Failed to parse message: {err}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -166,7 +155,7 @@ impl OnPresence for IkeaOutlet {
|
||||
async fn on_presence(&mut self, presence: bool) {
|
||||
// Turn off the outlet when we leave the house (Not if it is a battery charger)
|
||||
if !presence && self.config.outlet_type != OutletType::Charger {
|
||||
debug!(id = self.identifier, "Turning device off");
|
||||
debug!(id = Device::get_id(self), "Turning device off");
|
||||
self.set_on(false).await.ok();
|
||||
}
|
||||
}
|
||||
@@ -186,7 +175,7 @@ impl GoogleHomeDevice for IkeaOutlet {
|
||||
device::Name::new(&self.config.info.name)
|
||||
}
|
||||
|
||||
fn get_id(&self) -> &str {
|
||||
fn get_id(&self) -> String {
|
||||
Device::get_id(self)
|
||||
}
|
||||
|
||||
@@ -228,7 +217,7 @@ impl crate::traits::Timeout for IkeaOutlet {
|
||||
// get dropped
|
||||
let client = self.config.client.clone();
|
||||
let topic = self.config.mqtt.topic.clone();
|
||||
let id = self.identifier.clone();
|
||||
let id = Device::get_id(self).clone();
|
||||
self.handle = Some(tokio::spawn(async move {
|
||||
debug!(id, "Starting timeout ({timeout:?})...");
|
||||
tokio::time::sleep(timeout).await;
|
||||
|
||||
@@ -13,39 +13,31 @@ use tokio::net::TcpStream;
|
||||
use tracing::trace;
|
||||
|
||||
use super::Device;
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::error::DeviceConfigError;
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct KasaOutletConfig {
|
||||
identifier: String,
|
||||
#[device_config(rename("ip"), with(|ip| SocketAddr::new(ip, 9999)))]
|
||||
addr: SocketAddr,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for KasaOutletConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
trace!(id = identifier, "Setting up KasaOutlet");
|
||||
|
||||
let device = KasaOutlet {
|
||||
identifier: identifier.into(),
|
||||
config: self.clone(),
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct KasaOutlet {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: KasaOutletConfig,
|
||||
}
|
||||
|
||||
impl KasaOutlet {
|
||||
async fn create(config: KasaOutletConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.identifier, "Setting up KasaOutlet");
|
||||
Ok(Self { config })
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for KasaOutlet {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.identifier.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ use rumqttc::Publish;
|
||||
use tracing::{debug, trace, warn};
|
||||
|
||||
use crate::config::MqttDeviceConfig;
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{self, Event, EventChannel, OnMqtt};
|
||||
@@ -12,6 +11,7 @@ use crate::messages::BrightnessMessage;
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct LightSensorConfig {
|
||||
identifier: String,
|
||||
#[device_config(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
pub min: isize,
|
||||
@@ -22,34 +22,27 @@ pub struct LightSensorConfig {
|
||||
|
||||
pub const DEFAULT: bool = false;
|
||||
|
||||
// TODO: The light sensor should get a list of devices that it should inform
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for LightSensorConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
let device = LightSensor {
|
||||
identifier: identifier.into(),
|
||||
// Add helper type that does this conversion for us
|
||||
config: self.clone(),
|
||||
is_dark: DEFAULT,
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct LightSensor {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: LightSensorConfig,
|
||||
|
||||
is_dark: bool,
|
||||
}
|
||||
|
||||
impl LightSensor {
|
||||
async fn create(config: LightSensorConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.identifier, "Setting up LightSensor");
|
||||
Ok(Self {
|
||||
config,
|
||||
is_dark: DEFAULT,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for LightSensor {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.identifier.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,5 +33,5 @@ use crate::traits::Timeout;
|
||||
|
||||
#[impl_cast::device(As: OnMqtt + OnPresence + OnDarkness + OnNotification + OnOff + Timeout)]
|
||||
pub trait Device: AsGoogleHomeDevice + std::fmt::Debug + Sync + Send {
|
||||
fn get_id(&self) -> &str;
|
||||
fn get_id(&self) -> String;
|
||||
}
|
||||
|
||||
@@ -1,21 +1,15 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use serde::Serialize;
|
||||
use serde_repr::*;
|
||||
use tracing::{debug, error, warn};
|
||||
use tracing::{error, trace, warn};
|
||||
|
||||
use crate::config::NtfyConfig;
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{self, Event, EventChannel, OnNotification, OnPresence};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Ntfy {
|
||||
base_url: String,
|
||||
topic: String,
|
||||
tx: event::Sender,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize_repr, Clone, Copy)]
|
||||
#[repr(u8)]
|
||||
pub enum Priority {
|
||||
@@ -116,22 +110,41 @@ impl Default for Notification {
|
||||
}
|
||||
}
|
||||
|
||||
impl Ntfy {
|
||||
pub fn new(config: NtfyConfig, event_channel: &EventChannel) -> Self {
|
||||
Self {
|
||||
base_url: config.url,
|
||||
topic: config.topic,
|
||||
tx: event_channel.get_tx(),
|
||||
}
|
||||
}
|
||||
#[derive(Debug, LuaDeviceConfig)]
|
||||
pub struct NtfyConfig {
|
||||
#[device_config(default("https://ntfy.sh".into()))]
|
||||
url: String,
|
||||
topic: String,
|
||||
#[device_config(rename("event_channel"), from_lua, with(|ec: EventChannel| ec.get_tx()))]
|
||||
tx: event::Sender,
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct Ntfy {
|
||||
#[config]
|
||||
config: NtfyConfig,
|
||||
}
|
||||
|
||||
impl Ntfy {
|
||||
async fn create(config: NtfyConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = "ntfy", "Setting up Ntfy");
|
||||
Ok(Self { config })
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for Ntfy {
|
||||
fn get_id(&self) -> String {
|
||||
"ntfy".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Ntfy {
|
||||
async fn send(&self, notification: Notification) {
|
||||
let notification = notification.finalize(&self.topic);
|
||||
debug!("Sending notfication");
|
||||
let notification = notification.finalize(&self.config.topic);
|
||||
|
||||
// Create the request
|
||||
let res = reqwest::Client::new()
|
||||
.post(self.base_url.clone())
|
||||
.post(self.config.url.clone())
|
||||
.json(¬ification)
|
||||
.send()
|
||||
.await;
|
||||
@@ -147,12 +160,6 @@ impl Ntfy {
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for Ntfy {
|
||||
fn get_id(&self) -> &str {
|
||||
"ntfy"
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl OnPresence for Ntfy {
|
||||
async fn on_presence(&mut self, presence: bool) {
|
||||
@@ -177,7 +184,13 @@ impl OnPresence for Ntfy {
|
||||
.add_action(action)
|
||||
.set_priority(Priority::Low);
|
||||
|
||||
if self.tx.send(Event::Ntfy(notification)).await.is_err() {
|
||||
if self
|
||||
.config
|
||||
.tx
|
||||
.send(Event::Ntfy(notification))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
warn!("There are no receivers on the event channel");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +1,64 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use rumqttc::Publish;
|
||||
use serde::Deserialize;
|
||||
use tracing::{debug, warn};
|
||||
use tracing::{debug, trace, warn};
|
||||
|
||||
use crate::config::MqttDeviceConfig;
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{self, Event, EventChannel, OnMqtt};
|
||||
use crate::messages::PresenceMessage;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, LuaDeviceConfig)]
|
||||
pub struct PresenceConfig {
|
||||
#[serde(flatten)]
|
||||
#[device_config(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
#[device_config(from_lua, rename("event_channel"), with(|ec: EventChannel| ec.get_tx()))]
|
||||
tx: event::Sender,
|
||||
}
|
||||
|
||||
pub const DEFAULT_PRESENCE: bool = false;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct Presence {
|
||||
tx: event::Sender,
|
||||
mqtt: MqttDeviceConfig,
|
||||
#[config]
|
||||
config: PresenceConfig,
|
||||
devices: HashMap<String, bool>,
|
||||
current_overall_presence: bool,
|
||||
}
|
||||
|
||||
impl Presence {
|
||||
pub fn new(config: PresenceConfig, event_channel: &EventChannel) -> Self {
|
||||
Self {
|
||||
tx: event_channel.get_tx(),
|
||||
mqtt: config.mqtt,
|
||||
async fn create(config: PresenceConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = "ntfy", "Setting up Presence");
|
||||
Ok(Self {
|
||||
config,
|
||||
devices: HashMap::new(),
|
||||
current_overall_presence: DEFAULT_PRESENCE,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for Presence {
|
||||
fn get_id(&self) -> &str {
|
||||
"presence"
|
||||
fn get_id(&self) -> String {
|
||||
"presence".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl OnMqtt for Presence {
|
||||
fn topics(&self) -> Vec<&str> {
|
||||
vec![&self.mqtt.topic]
|
||||
vec![&self.config.mqtt.topic]
|
||||
}
|
||||
|
||||
async fn on_mqtt(&mut self, message: Publish) {
|
||||
let offset = self
|
||||
.config
|
||||
.mqtt
|
||||
.topic
|
||||
.find('+')
|
||||
.or(self.mqtt.topic.find('#'))
|
||||
.or(self.config.mqtt.topic.find('#'))
|
||||
.expect("Presence::create fails if it does not contain wildcards");
|
||||
let device_name = message.topic[offset..].into();
|
||||
|
||||
@@ -81,6 +85,7 @@ impl OnMqtt for Presence {
|
||||
self.current_overall_presence = overall_presence;
|
||||
|
||||
if self
|
||||
.config
|
||||
.tx
|
||||
.send(Event::Presence(overall_presence))
|
||||
.await
|
||||
|
||||
@@ -12,7 +12,6 @@ use tracing::{debug, error, trace};
|
||||
|
||||
use super::Device;
|
||||
use crate::config::{InfoConfig, MqttDeviceConfig};
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::OnMqtt;
|
||||
use crate::messages::ActivateMessage;
|
||||
@@ -28,37 +27,23 @@ pub struct WakeOnLANConfig {
|
||||
broadcast_ip: Ipv4Addr,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for WakeOnLANConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
trace!(
|
||||
id = identifier,
|
||||
name = self.info.name,
|
||||
room = self.info.room,
|
||||
"Setting up WakeOnLAN"
|
||||
);
|
||||
|
||||
debug!("broadcast_ip = {}", self.broadcast_ip);
|
||||
|
||||
let device = WakeOnLAN {
|
||||
identifier: identifier.into(),
|
||||
config: self.clone(),
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct WakeOnLAN {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: WakeOnLANConfig,
|
||||
}
|
||||
|
||||
impl WakeOnLAN {
|
||||
async fn create(config: WakeOnLANConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.info.identifier(), "Setting up WakeOnLAN");
|
||||
|
||||
Ok(Self { config })
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for WakeOnLAN {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.info.identifier()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +57,7 @@ impl OnMqtt for WakeOnLAN {
|
||||
let activate = match ActivateMessage::try_from(message) {
|
||||
Ok(message) => message.activate(),
|
||||
Err(err) => {
|
||||
error!(id = self.identifier, "Failed to parse message: {err}");
|
||||
error!(id = Device::get_id(self), "Failed to parse message: {err}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -93,7 +78,7 @@ impl GoogleHomeDevice for WakeOnLAN {
|
||||
name
|
||||
}
|
||||
|
||||
fn get_id(&self) -> &str {
|
||||
fn get_id(&self) -> String {
|
||||
Device::get_id(self)
|
||||
}
|
||||
|
||||
@@ -111,14 +96,14 @@ impl traits::Scene for WakeOnLAN {
|
||||
async fn set_active(&self, activate: bool) -> Result<(), ErrorCode> {
|
||||
if activate {
|
||||
debug!(
|
||||
id = self.identifier,
|
||||
id = Device::get_id(self),
|
||||
"Activating Computer: {} (Sending to {})",
|
||||
self.config.mac_address,
|
||||
self.config.broadcast_ip
|
||||
);
|
||||
let wol = wakey::WolPacket::from_bytes(&self.config.mac_address.to_array()).map_err(
|
||||
|err| {
|
||||
error!(id = self.identifier, "invalid mac address: {err}");
|
||||
error!(id = Device::get_id(self), "invalid mac address: {err}");
|
||||
google_home::errors::DeviceError::TransientError
|
||||
},
|
||||
)?;
|
||||
@@ -129,13 +114,16 @@ impl traits::Scene for WakeOnLAN {
|
||||
)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
error!(id = self.identifier, "Failed to activate computer: {err}");
|
||||
error!(
|
||||
id = Device::get_id(self),
|
||||
"Failed to activate computer: {err}"
|
||||
);
|
||||
google_home::errors::DeviceError::TransientError.into()
|
||||
})
|
||||
.map(|_| debug!(id = self.identifier, "Success!"))
|
||||
.map(|_| debug!(id = Device::get_id(self), "Success!"))
|
||||
} else {
|
||||
debug!(
|
||||
id = self.identifier,
|
||||
id = Device::get_id(self),
|
||||
"Trying to deactivate computer, this is not currently supported"
|
||||
);
|
||||
// We do not support deactivating this scene
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
use async_trait::async_trait;
|
||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||
use rumqttc::Publish;
|
||||
use tracing::{debug, error, warn};
|
||||
use tracing::{debug, error, trace, warn};
|
||||
|
||||
use super::ntfy::Priority;
|
||||
use super::{Device, Notification};
|
||||
use crate::config::MqttDeviceConfig;
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{self, Event, EventChannel, OnMqtt};
|
||||
use crate::messages::PowerMessage;
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct WasherConfig {
|
||||
identifier: String,
|
||||
#[device_config(flatten)]
|
||||
mqtt: MqttDeviceConfig,
|
||||
// Power in Watt
|
||||
@@ -21,33 +21,25 @@ pub struct WasherConfig {
|
||||
pub tx: event::Sender,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for WasherConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
let device = Washer {
|
||||
identifier: identifier.into(),
|
||||
config: self.clone(),
|
||||
running: 0,
|
||||
};
|
||||
|
||||
Ok(Box::new(device))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add google home integration
|
||||
|
||||
#[derive(Debug, LuaDevice)]
|
||||
pub struct Washer {
|
||||
identifier: String,
|
||||
#[config]
|
||||
config: WasherConfig,
|
||||
|
||||
running: isize,
|
||||
}
|
||||
|
||||
impl Washer {
|
||||
async fn create(config: WasherConfig) -> Result<Self, DeviceConfigError> {
|
||||
trace!(id = config.identifier, "Setting up Washer");
|
||||
Ok(Self { config, running: 0 })
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for Washer {
|
||||
fn get_id(&self) -> &str {
|
||||
&self.identifier
|
||||
fn get_id(&self) -> String {
|
||||
self.config.identifier.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +58,10 @@ impl OnMqtt for Washer {
|
||||
let power = match PowerMessage::try_from(message) {
|
||||
Ok(state) => state.power(),
|
||||
Err(err) => {
|
||||
error!(id = self.identifier, "Failed to parse message: {err}");
|
||||
error!(
|
||||
id = self.config.identifier,
|
||||
"Failed to parse message: {err}"
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -76,7 +71,7 @@ impl OnMqtt for Washer {
|
||||
if power < self.config.threshold && self.running >= HYSTERESIS {
|
||||
// The washer is done running
|
||||
debug!(
|
||||
id = self.identifier,
|
||||
id = self.config.identifier,
|
||||
power,
|
||||
threshold = self.config.threshold,
|
||||
"Washer is done"
|
||||
@@ -104,7 +99,7 @@ impl OnMqtt for Washer {
|
||||
} else if power >= self.config.threshold && self.running < HYSTERESIS {
|
||||
// Washer could be starting
|
||||
debug!(
|
||||
id = self.identifier,
|
||||
id = self.config.identifier,
|
||||
power,
|
||||
threshold = self.config.threshold,
|
||||
"Washer is starting"
|
||||
|
||||
Reference in New Issue
Block a user