Removed old darkness system
This commit is contained in:
@@ -4,7 +4,7 @@ use async_trait::async_trait;
|
|||||||
use automation_lib::action_callback::ActionCallback;
|
use automation_lib::action_callback::ActionCallback;
|
||||||
use automation_lib::config::MqttDeviceConfig;
|
use automation_lib::config::MqttDeviceConfig;
|
||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||||
use automation_lib::event::{self, Event, EventChannel, OnMqtt};
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::messages::BrightnessMessage;
|
use automation_lib::messages::BrightnessMessage;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
||||||
@@ -19,8 +19,6 @@ pub struct Config {
|
|||||||
pub mqtt: MqttDeviceConfig,
|
pub mqtt: MqttDeviceConfig,
|
||||||
pub min: isize,
|
pub min: isize,
|
||||||
pub max: isize,
|
pub max: isize,
|
||||||
#[device_config(rename("event_channel"), from_lua, with(|ec: EventChannel| ec.get_tx()))]
|
|
||||||
pub tx: event::Sender,
|
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub callback: ActionCallback<LightSensor, bool>,
|
pub callback: ActionCallback<LightSensor, bool>,
|
||||||
@@ -114,10 +112,6 @@ impl OnMqtt for LightSensor {
|
|||||||
debug!("Dark state has changed: {is_dark}");
|
debug!("Dark state has changed: {is_dark}");
|
||||||
self.state_mut().await.is_dark = is_dark;
|
self.state_mut().await.is_dark = is_dark;
|
||||||
|
|
||||||
if self.config.tx.send(Event::Darkness(is_dark)).await.is_err() {
|
|
||||||
warn!("There are no receivers on the event channel");
|
|
||||||
}
|
|
||||||
|
|
||||||
self.config
|
self.config
|
||||||
.callback
|
.callback
|
||||||
.call(self, &!self.state().await.is_dark)
|
.call(self, &!self.state().await.is_dark)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use dyn_clone::DynClone;
|
|||||||
use google_home::traits::OnOff;
|
use google_home::traits::OnOff;
|
||||||
use mlua::ObjectLike;
|
use mlua::ObjectLike;
|
||||||
|
|
||||||
use crate::event::{OnDarkness, OnMqtt, OnPresence};
|
use crate::event::{OnMqtt, OnPresence};
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait LuaDeviceCreate {
|
pub trait LuaDeviceCreate {
|
||||||
@@ -25,7 +25,6 @@ pub trait Device:
|
|||||||
+ Cast<dyn google_home::Device>
|
+ Cast<dyn google_home::Device>
|
||||||
+ Cast<dyn OnMqtt>
|
+ Cast<dyn OnMqtt>
|
||||||
+ Cast<dyn OnPresence>
|
+ Cast<dyn OnPresence>
|
||||||
+ Cast<dyn OnDarkness>
|
|
||||||
+ Cast<dyn OnOff>
|
+ Cast<dyn OnOff>
|
||||||
{
|
{
|
||||||
fn get_id(&self) -> String;
|
fn get_id(&self) -> String;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use tokio_cron_scheduler::{Job, JobScheduler};
|
|||||||
use tracing::{debug, instrument, trace};
|
use tracing::{debug, instrument, trace};
|
||||||
|
|
||||||
use crate::device::Device;
|
use crate::device::Device;
|
||||||
use crate::event::{Event, EventChannel, OnDarkness, OnMqtt, OnPresence};
|
use crate::event::{Event, EventChannel, OnMqtt, OnPresence};
|
||||||
|
|
||||||
pub type DeviceMap = HashMap<String, Box<dyn Device>>;
|
pub type DeviceMap = HashMap<String, Box<dyn Device>>;
|
||||||
|
|
||||||
@@ -94,19 +94,6 @@ impl DeviceManager {
|
|||||||
|
|
||||||
join_all(iter).await;
|
join_all(iter).await;
|
||||||
}
|
}
|
||||||
Event::Darkness(dark) => {
|
|
||||||
let devices = self.devices.read().await;
|
|
||||||
let iter = devices.iter().map(|(id, device)| async move {
|
|
||||||
let device: Option<&dyn OnDarkness> = device.cast();
|
|
||||||
if let Some(device) = device {
|
|
||||||
trace!(id, "Handling");
|
|
||||||
device.on_darkness(dark).await;
|
|
||||||
trace!(id, "Done");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
join_all(iter).await;
|
|
||||||
}
|
|
||||||
Event::Presence(presence) => {
|
Event::Presence(presence) => {
|
||||||
let devices = self.devices.read().await;
|
let devices = self.devices.read().await;
|
||||||
let iter = devices.iter().map(|(id, device)| async move {
|
let iter = devices.iter().map(|(id, device)| async move {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ use tokio::sync::mpsc;
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
MqttMessage(Publish),
|
MqttMessage(Publish),
|
||||||
Darkness(bool),
|
|
||||||
Presence(bool),
|
Presence(bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,8 +39,3 @@ pub trait OnMqtt: Sync + Send {
|
|||||||
pub trait OnPresence: Sync + Send {
|
pub trait OnPresence: Sync + Send {
|
||||||
async fn on_presence(&self, presence: bool);
|
async fn on_presence(&self, presence: bool);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
pub trait OnDarkness: Sync + Send {
|
|
||||||
async fn on_darkness(&self, dark: bool);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -162,40 +162,6 @@ impl TryFrom<Publish> for ContactMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message used to report the current darkness state
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
|
||||||
pub struct DarknessMessage {
|
|
||||||
state: bool,
|
|
||||||
updated: Option<u128>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DarknessMessage {
|
|
||||||
pub fn new(state: bool) -> Self {
|
|
||||||
Self {
|
|
||||||
state,
|
|
||||||
updated: Some(
|
|
||||||
SystemTime::now()
|
|
||||||
.duration_since(UNIX_EPOCH)
|
|
||||||
.expect("Time is after UNIX EPOCH")
|
|
||||||
.as_millis(),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_dark(&self) -> bool {
|
|
||||||
self.state
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<Publish> for DarknessMessage {
|
|
||||||
type Error = ParseError;
|
|
||||||
|
|
||||||
fn try_from(message: Publish) -> Result<Self, Self::Error> {
|
|
||||||
serde_json::from_slice(&message.payload)
|
|
||||||
.or(Err(ParseError::InvalidPayload(message.payload.clone())))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Message used to report the power draw a smart plug
|
// Message used to report the power draw a smart plug
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct PowerMessage {
|
pub struct PowerMessage {
|
||||||
|
|||||||
@@ -126,7 +126,6 @@ automation.device_manager:add(LightSensor.new({
|
|||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
min = 22000,
|
min = 22000,
|
||||||
max = 23500,
|
max = 23500,
|
||||||
event_channel = automation.device_manager:event_channel(),
|
|
||||||
callback = function(_, light)
|
callback = function(_, light)
|
||||||
hue_bridge:set_flag("darkness", not light)
|
hue_bridge:set_flag("darkness", not light)
|
||||||
mqtt_client:send_message(mqtt_automation("debug") .. "/darkness", {
|
mqtt_client:send_message(mqtt_automation("debug") .. "/darkness", {
|
||||||
|
|||||||
Reference in New Issue
Block a user