Compare commits
20 Commits
b784cfed4a
...
a95574b731
| Author | SHA1 | Date | |
|---|---|---|---|
|
a95574b731
|
|||
|
810fae8da5
|
|||
|
6fc3783d7a
|
|||
|
df64804b00
|
|||
|
11b9787890
|
|||
|
8961101fdf
|
|||
|
17a68e8991
|
|||
|
be1602d0e2
|
|||
|
9bddeae54e
|
|||
|
97b944874a
|
|||
|
54164c517b
|
|||
|
518abd169d
|
|||
|
30ea9b2737
|
|||
|
cd470cadaf
|
|||
|
4b76bde2a6
|
|||
|
006a561307
|
|||
|
745a1025bb
|
|||
|
45485fca37
|
|||
|
1532958a86
|
|||
|
76eb63cd97
|
@@ -57,24 +57,12 @@ repos:
|
||||
files: (\.rs|Cargo.lock)$
|
||||
pass_filenames: false
|
||||
|
||||
- id: audit
|
||||
name: audit
|
||||
description: Audit packages
|
||||
entry: cargo audit
|
||||
args: ["--deny", "warnings"]
|
||||
- id: generate_definitions
|
||||
name: generate definitions
|
||||
description: Generate lua definitions
|
||||
entry: cargo run --bin generate_definitions
|
||||
language: system
|
||||
pass_filenames: false
|
||||
verbose: true
|
||||
always_run: true
|
||||
|
||||
- id: udeps
|
||||
name: unused
|
||||
description: Check for unused crates
|
||||
entry: cargo udeps
|
||||
args: ["--workspace"]
|
||||
language: system
|
||||
types: [file]
|
||||
files: (\.rs|Cargo.lock)$
|
||||
types: [rust]
|
||||
pass_filenames: false
|
||||
|
||||
- id: test
|
||||
@@ -87,6 +75,26 @@ repos:
|
||||
files: (\.rs|Cargo.lock)$
|
||||
pass_filenames: false
|
||||
|
||||
- id: udeps
|
||||
name: unused
|
||||
description: Check for unused crates
|
||||
entry: cargo udeps
|
||||
args: ["--workspace"]
|
||||
language: system
|
||||
types: [file]
|
||||
files: (\.rs|Cargo.lock)$
|
||||
pass_filenames: false
|
||||
|
||||
- id: audit
|
||||
name: audit
|
||||
description: Audit packages
|
||||
entry: cargo audit
|
||||
args: ["--deny", "warnings"]
|
||||
language: system
|
||||
pass_filenames: false
|
||||
verbose: true
|
||||
always_run: true
|
||||
|
||||
- repo: https://github.com/hadolint/hadolint
|
||||
rev: v2.13.1
|
||||
hooks:
|
||||
|
||||
46
Cargo.lock
generated
46
Cargo.lock
generated
@@ -96,9 +96,9 @@ dependencies = [
|
||||
"automation_lib",
|
||||
"axum",
|
||||
"config",
|
||||
"dotenvy",
|
||||
"git-version",
|
||||
"google_home",
|
||||
"inventory",
|
||||
"mlua",
|
||||
"reqwest",
|
||||
"rumqttc",
|
||||
@@ -128,6 +128,7 @@ dependencies = [
|
||||
"eui48",
|
||||
"google_home",
|
||||
"inventory",
|
||||
"lua_typed",
|
||||
"mlua",
|
||||
"reqwest",
|
||||
"rumqttc",
|
||||
@@ -153,6 +154,7 @@ dependencies = [
|
||||
"hostname",
|
||||
"indexmap",
|
||||
"inventory",
|
||||
"lua_typed",
|
||||
"mlua",
|
||||
"rumqttc",
|
||||
"serde",
|
||||
@@ -345,6 +347,15 @@ dependencies = [
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "convert_case"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baaaa0ecca5b51987b9423ccdc971514dd8b0bb7b4060b983d3664dad3f1f89f"
|
||||
dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation"
|
||||
version = "0.9.4"
|
||||
@@ -422,12 +433,6 @@ dependencies = [
|
||||
"syn 2.0.106",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dotenvy"
|
||||
version = "0.15.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
|
||||
|
||||
[[package]]
|
||||
name = "dyn-clone"
|
||||
version = "1.0.20"
|
||||
@@ -1094,6 +1099,27 @@ dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lua_typed"
|
||||
version = "0.1.0"
|
||||
source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#08f5c4533a93131e8eda6702c062fb841d14d4e1"
|
||||
dependencies = [
|
||||
"eui48",
|
||||
"lua_typed_macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lua_typed_macro"
|
||||
version = "0.1.0"
|
||||
source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#08f5c4533a93131e8eda6702c062fb841d14d4e1"
|
||||
dependencies = [
|
||||
"convert_case",
|
||||
"itertools",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.106",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "luajit-src"
|
||||
version = "210.6.1+f9140a6"
|
||||
@@ -2256,6 +2282,12 @@ version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
name = "automation"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
default-run = "automation"
|
||||
|
||||
[workspace]
|
||||
members = [
|
||||
@@ -23,7 +24,6 @@ automation_lib = { path = "./automation_lib" }
|
||||
automation_macro = { path = "./automation_macro" }
|
||||
axum = "0.8.4"
|
||||
bytes = "1.10.1"
|
||||
dotenvy = "0.15.7"
|
||||
dyn-clone = "1.0.20"
|
||||
eui48 = { version = "1.1.0", features = [
|
||||
"disp_hexstring",
|
||||
@@ -37,6 +37,7 @@ indexmap = { version = "2.11.0", features = ["serde"] }
|
||||
inventory = "0.3.21"
|
||||
itertools = "0.14.0"
|
||||
json_value_merge = "2.0.1"
|
||||
lua_typed = { git = "https://git.huizinga.dev/Dreaded_X/lua_typed" }
|
||||
mlua = { version = "0.11.3", features = [
|
||||
"lua54",
|
||||
"vendored",
|
||||
@@ -74,9 +75,9 @@ config = { version = "0.15.15", default-features = false, features = [
|
||||
"async",
|
||||
"toml",
|
||||
] }
|
||||
dotenvy = { workspace = true }
|
||||
git-version = "0.3.9"
|
||||
google_home = { workspace = true }
|
||||
inventory = { workspace = true }
|
||||
mlua = { workspace = true }
|
||||
reqwest = { workspace = true }
|
||||
rumqttc = { workspace = true }
|
||||
|
||||
@@ -14,6 +14,7 @@ dyn-clone = { workspace = true }
|
||||
eui48 = { workspace = true }
|
||||
google_home = { workspace = true }
|
||||
inventory = { workspace = true }
|
||||
lua_typed = { workspace = true }
|
||||
mlua = { workspace = true }
|
||||
reqwest = { workspace = true }
|
||||
rumqttc = { workspace = true }
|
||||
|
||||
@@ -9,15 +9,19 @@ use google_home::traits::{
|
||||
TemperatureUnit,
|
||||
};
|
||||
use google_home::types::Type;
|
||||
use lua_typed::Typed;
|
||||
use thiserror::Error;
|
||||
use tracing::{debug, trace};
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "AirFilterConfig")]
|
||||
pub struct Config {
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub info: InfoConfig,
|
||||
pub url: String,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(traits(OnOff))]
|
||||
|
||||
@@ -13,35 +13,45 @@ use google_home::device;
|
||||
use google_home::errors::{DeviceError, ErrorCode};
|
||||
use google_home::traits::OpenClose;
|
||||
use google_home::types::Type;
|
||||
use lua_typed::Typed;
|
||||
use serde::Deserialize;
|
||||
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use tracing::{debug, error, trace};
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Copy)]
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Copy, Typed)]
|
||||
pub enum SensorType {
|
||||
Door,
|
||||
Drawer,
|
||||
Window,
|
||||
}
|
||||
crate::register_type!(SensorType);
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "ContactSensorConfig")]
|
||||
pub struct Config {
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub info: InfoConfig,
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
|
||||
#[device_config(default(SensorType::Window))]
|
||||
#[typed(default)]
|
||||
pub sensor_type: SensorType,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub callback: ActionCallback<(ContactSensor, bool)>,
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub battery_callback: ActionCallback<(ContactSensor, f32)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
#[typed(default)]
|
||||
pub client: WrappedAsyncClient,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct State {
|
||||
|
||||
@@ -3,40 +3,72 @@ use std::net::SocketAddr;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::lua::traits::PartialUserData;
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use lua_typed::Typed;
|
||||
use mlua::LuaSerdeExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing::{error, trace, warn};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, Deserialize, Typed)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[typed(rename_all = "snake_case")]
|
||||
pub enum Flag {
|
||||
Presence,
|
||||
Darkness,
|
||||
}
|
||||
crate::register_type!(Flag);
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Typed)]
|
||||
pub struct FlagIDs {
|
||||
presence: isize,
|
||||
darkness: isize,
|
||||
}
|
||||
crate::register_type!(FlagIDs);
|
||||
|
||||
#[derive(Debug, LuaDeviceConfig, Clone)]
|
||||
#[derive(Debug, LuaDeviceConfig, Clone, Typed)]
|
||||
#[typed(as = "HueBridgeConfig")]
|
||||
pub struct Config {
|
||||
pub identifier: String,
|
||||
#[device_config(rename("ip"), with(|ip| SocketAddr::new(ip, 80)))]
|
||||
#[typed(as = "ip")]
|
||||
pub addr: SocketAddr,
|
||||
pub login: String,
|
||||
pub flags: FlagIDs,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(add_methods(Self::add_methods))]
|
||||
#[device(extra_user_data = SetFlag)]
|
||||
pub struct HueBridge {
|
||||
config: Config,
|
||||
}
|
||||
crate::register_device!(HueBridge);
|
||||
|
||||
struct SetFlag;
|
||||
impl PartialUserData<HueBridge> for SetFlag {
|
||||
fn add_methods<M: mlua::UserDataMethods<HueBridge>>(methods: &mut M) {
|
||||
methods.add_async_method(
|
||||
"set_flag",
|
||||
async |lua, this, (flag, value): (mlua::Value, bool)| {
|
||||
let flag: Flag = lua.from_value(flag)?;
|
||||
|
||||
this.set_flag(flag, value).await;
|
||||
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn definitions() -> Option<String> {
|
||||
Some(format!(
|
||||
"---@async\n---@param flag {}\n---@param value boolean\nfunction {}:set_flag(flag, value) end\n",
|
||||
<Flag as Typed>::type_name(),
|
||||
<HueBridge as Typed>::type_name(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct FlagMessage {
|
||||
flag: bool,
|
||||
@@ -84,19 +116,6 @@ impl HueBridge {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_async_method(
|
||||
"set_flag",
|
||||
async |lua, this, (flag, value): (mlua::Value, bool)| {
|
||||
let flag: Flag = lua.from_value(flag)?;
|
||||
|
||||
this.set_flag(flag, value).await;
|
||||
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl Device for HueBridge {
|
||||
|
||||
@@ -5,19 +5,23 @@ use async_trait::async_trait;
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use google_home::errors::ErrorCode;
|
||||
use google_home::traits::OnOff;
|
||||
use lua_typed::Typed;
|
||||
use tracing::{error, trace, warn};
|
||||
|
||||
use super::{Device, LuaDeviceCreate};
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "HueGroupConfig")]
|
||||
pub struct Config {
|
||||
pub identifier: String,
|
||||
#[device_config(rename("ip"), with(|ip| SocketAddr::new(ip, 80)))]
|
||||
#[typed(as = "ip")]
|
||||
pub addr: SocketAddr,
|
||||
pub login: String,
|
||||
pub group_id: isize,
|
||||
pub scene_id: String,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(traits(OnOff))]
|
||||
|
||||
@@ -5,36 +5,46 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use lua_typed::Typed;
|
||||
use rumqttc::{Publish, matches};
|
||||
use serde::Deserialize;
|
||||
use tracing::{debug, trace, warn};
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "HueSwitchConfig")]
|
||||
pub struct Config {
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub info: InfoConfig,
|
||||
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub left_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub right_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub left_hold_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub right_hold_callback: ActionCallback<HueSwitch>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub battery_callback: ActionCallback<(HueSwitch, f32)>,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
#[derive(Debug, Copy, Clone, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
|
||||
@@ -6,28 +6,36 @@ use automation_lib::event::OnMqtt;
|
||||
use automation_lib::messages::{RemoteAction, RemoteMessage};
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use lua_typed::Typed;
|
||||
use rumqttc::{Publish, matches};
|
||||
use tracing::{debug, error, trace};
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "IkeaRemoteConfig")]
|
||||
pub struct Config {
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub info: InfoConfig,
|
||||
|
||||
#[device_config(default)]
|
||||
#[typed(default)]
|
||||
pub single_button: bool,
|
||||
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub callback: ActionCallback<(IkeaRemote, bool)>,
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub battery_callback: ActionCallback<(IkeaRemote, f32)>,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
#[derive(Debug, Clone, Device)]
|
||||
pub struct IkeaRemote {
|
||||
|
||||
@@ -8,18 +8,22 @@ use automation_macro::{Device, LuaDeviceConfig};
|
||||
use bytes::{Buf, BufMut};
|
||||
use google_home::errors::{self, DeviceError};
|
||||
use google_home::traits::OnOff;
|
||||
use lua_typed::Typed;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::TcpStream;
|
||||
use tracing::trace;
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "KasaOutletConfig")]
|
||||
pub struct Config {
|
||||
pub identifier: String,
|
||||
#[device_config(rename("ip"), with(|ip| SocketAddr::new(ip, 9999)))]
|
||||
#[typed(as = "ip")]
|
||||
pub addr: SocketAddr,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(traits(OnOff))]
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#![feature(iter_intersperse)]
|
||||
mod air_filter;
|
||||
mod contact_sensor;
|
||||
mod hue_bridge;
|
||||
@@ -14,33 +15,26 @@ mod zigbee;
|
||||
|
||||
use automation_lib::Module;
|
||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use tracing::debug;
|
||||
use tracing::{debug, warn};
|
||||
|
||||
macro_rules! register_device {
|
||||
($device:ty) => {
|
||||
::inventory::submit!(crate::RegisteredDevice::new(
|
||||
stringify!($device),
|
||||
::mlua::Lua::create_proxy::<$device>
|
||||
));
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use register_device;
|
||||
|
||||
type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::AnyUserData>;
|
||||
type DeviceNameFn = fn() -> String;
|
||||
type RegisterDeviceFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::AnyUserData>;
|
||||
|
||||
pub struct RegisteredDevice {
|
||||
name: &'static str,
|
||||
register_fn: RegisterFn,
|
||||
name_fn: DeviceNameFn,
|
||||
register_fn: RegisterDeviceFn,
|
||||
}
|
||||
|
||||
impl RegisteredDevice {
|
||||
pub const fn new(name: &'static str, register_fn: RegisterFn) -> Self {
|
||||
Self { name, register_fn }
|
||||
pub const fn new(name_fn: DeviceNameFn, register_fn: RegisterDeviceFn) -> Self {
|
||||
Self {
|
||||
name_fn,
|
||||
register_fn,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn get_name(&self) -> &'static str {
|
||||
self.name
|
||||
pub fn get_name(&self) -> String {
|
||||
(self.name_fn)()
|
||||
}
|
||||
|
||||
pub fn register(&self, lua: &mlua::Lua) -> mlua::Result<mlua::AnyUserData> {
|
||||
@@ -48,6 +42,18 @@ impl RegisteredDevice {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! register_device {
|
||||
($device:ty) => {
|
||||
::inventory::submit!(crate::RegisteredDevice::new(
|
||||
<$device as ::lua_typed::Typed>::type_name,
|
||||
::mlua::Lua::create_proxy::<$device>
|
||||
));
|
||||
|
||||
crate::register_type!($device);
|
||||
};
|
||||
}
|
||||
pub(crate) use register_device;
|
||||
|
||||
inventory::collect!(RegisteredDevice);
|
||||
|
||||
pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
@@ -55,12 +61,45 @@ pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
|
||||
debug!("Loading devices...");
|
||||
for device in inventory::iter::<RegisteredDevice> {
|
||||
debug!(name = device.get_name(), "Registering device");
|
||||
let name = device.get_name();
|
||||
debug!(name, "Registering device");
|
||||
let proxy = device.register(lua)?;
|
||||
devices.set(device.get_name(), proxy)?;
|
||||
devices.set(name, proxy)?;
|
||||
}
|
||||
|
||||
Ok(devices)
|
||||
}
|
||||
|
||||
inventory::submit! {Module::new("devices", create_module)}
|
||||
type RegisterTypeFn = fn() -> Option<String>;
|
||||
|
||||
pub struct RegisteredType(RegisterTypeFn);
|
||||
|
||||
macro_rules! register_type {
|
||||
($ty:ty) => {
|
||||
::inventory::submit!(crate::RegisteredType(
|
||||
<$ty as ::lua_typed::Typed>::generate_full
|
||||
));
|
||||
};
|
||||
}
|
||||
pub(crate) use register_type;
|
||||
|
||||
inventory::collect!(RegisteredType);
|
||||
|
||||
fn generate_definitions() -> String {
|
||||
let mut output = String::new();
|
||||
|
||||
output += "---@meta\n\nlocal devices\n\n";
|
||||
for ty in inventory::iter::<RegisteredType> {
|
||||
if let Some(def) = ty.0() {
|
||||
output += &(def + "\n");
|
||||
} else {
|
||||
// NOTE: Due to how this works the typed is erased, so we don't know the cause
|
||||
warn!("Registered type is missing generate_full function");
|
||||
}
|
||||
}
|
||||
output += "return devices";
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
inventory::submit! {Module::new("automation:devices", create_module, Some(generate_definitions))}
|
||||
|
||||
@@ -8,24 +8,29 @@ use automation_lib::event::OnMqtt;
|
||||
use automation_lib::messages::BrightnessMessage;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use lua_typed::Typed;
|
||||
use rumqttc::Publish;
|
||||
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use tracing::{debug, trace, warn};
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "LightSensorConfig")]
|
||||
pub struct Config {
|
||||
pub identifier: String,
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
pub min: isize,
|
||||
pub max: isize,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub callback: ActionCallback<(LightSensor, bool)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
const DEFAULT: bool = false;
|
||||
|
||||
|
||||
@@ -3,15 +3,18 @@ use std::convert::Infallible;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::lua::traits::PartialUserData;
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use lua_typed::Typed;
|
||||
use mlua::LuaSerdeExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_repr::*;
|
||||
use tracing::{error, trace, warn};
|
||||
|
||||
#[derive(Debug, Serialize_repr, Deserialize, Clone, Copy)]
|
||||
#[derive(Debug, Serialize_repr, Deserialize, Clone, Copy, Typed)]
|
||||
#[repr(u8)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[typed(rename_all = "snake_case")]
|
||||
pub enum Priority {
|
||||
Min = 1,
|
||||
Low,
|
||||
@@ -19,44 +22,54 @@ pub enum Priority {
|
||||
High,
|
||||
Max,
|
||||
}
|
||||
crate::register_type!(Priority);
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, Typed)]
|
||||
#[serde(rename_all = "snake_case", tag = "action")]
|
||||
#[typed(rename_all = "snake_case", tag = "action")]
|
||||
pub enum ActionType {
|
||||
Broadcast {
|
||||
#[serde(skip_serializing_if = "HashMap::is_empty")]
|
||||
#[serde(default)]
|
||||
#[typed(default)]
|
||||
extras: HashMap<String, String>,
|
||||
},
|
||||
// View,
|
||||
// Http
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, Typed)]
|
||||
pub struct Action {
|
||||
#[serde(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub action: ActionType,
|
||||
pub label: String,
|
||||
pub clear: Option<bool>,
|
||||
}
|
||||
crate::register_type!(Action);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Typed)]
|
||||
struct NotificationFinal {
|
||||
topic: String,
|
||||
#[serde(flatten)]
|
||||
#[typed(flatten)]
|
||||
inner: Notification,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone, Deserialize)]
|
||||
#[derive(Debug, Serialize, Clone, Deserialize, Typed)]
|
||||
pub struct Notification {
|
||||
title: String,
|
||||
message: Option<String>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty", default = "Default::default")]
|
||||
#[typed(default)]
|
||||
tags: Vec<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
priority: Option<Priority>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty", default = "Default::default")]
|
||||
#[typed(default)]
|
||||
actions: Vec<Action>,
|
||||
}
|
||||
crate::register_type!(Notification);
|
||||
|
||||
impl Notification {
|
||||
fn finalize(self, topic: &str) -> NotificationFinal {
|
||||
@@ -67,22 +80,26 @@ impl Notification {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "NtfyConfig")]
|
||||
pub struct Config {
|
||||
#[device_config(default("https://ntfy.sh".into()))]
|
||||
#[typed(default)]
|
||||
pub url: String,
|
||||
pub topic: String,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(add_methods(Self::add_methods))]
|
||||
#[device(extra_user_data = SendNotification)]
|
||||
pub struct Ntfy {
|
||||
config: Config,
|
||||
}
|
||||
crate::register_device!(Ntfy);
|
||||
|
||||
impl Ntfy {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
|
||||
struct SendNotification;
|
||||
impl PartialUserData<Ntfy> for SendNotification {
|
||||
fn add_methods<M: mlua::UserDataMethods<Ntfy>>(methods: &mut M) {
|
||||
methods.add_async_method(
|
||||
"send_notification",
|
||||
async |lua, this, notification: mlua::Value| {
|
||||
@@ -94,6 +111,14 @@ impl Ntfy {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn definitions() -> Option<String> {
|
||||
Some(format!(
|
||||
"---@async\n---@param notification {}\nfunction {}:send_notification(notification) end\n",
|
||||
<Notification as Typed>::type_name(),
|
||||
<Ntfy as Typed>::type_name(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
||||
@@ -6,24 +6,30 @@ use automation_lib::action_callback::ActionCallback;
|
||||
use automation_lib::config::MqttDeviceConfig;
|
||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||
use automation_lib::event::OnMqtt;
|
||||
use automation_lib::lua::traits::PartialUserData;
|
||||
use automation_lib::messages::PresenceMessage;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use lua_typed::Typed;
|
||||
use rumqttc::Publish;
|
||||
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use tracing::{debug, trace, warn};
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "PresenceConfig")]
|
||||
pub struct Config {
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub callback: ActionCallback<(Presence, bool)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
pub const DEFAULT_PRESENCE: bool = false;
|
||||
|
||||
@@ -34,13 +40,29 @@ pub struct State {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(add_methods(Self::add_methods))]
|
||||
#[device(extra_user_data = OverallPresence)]
|
||||
pub struct Presence {
|
||||
config: Config,
|
||||
state: Arc<RwLock<State>>,
|
||||
}
|
||||
crate::register_device!(Presence);
|
||||
|
||||
struct OverallPresence;
|
||||
impl PartialUserData<Presence> for OverallPresence {
|
||||
fn add_methods<M: mlua::UserDataMethods<Presence>>(methods: &mut M) {
|
||||
methods.add_async_method("overall_presence", async |_lua, this, ()| {
|
||||
Ok(this.state().await.current_overall_presence)
|
||||
});
|
||||
}
|
||||
|
||||
fn definitions() -> Option<String> {
|
||||
Some(format!(
|
||||
"---@async\n---@return boolean\nfunction {}:overall_presence() end\n",
|
||||
<Presence as Typed>::type_name(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Presence {
|
||||
async fn state(&self) -> RwLockReadGuard<'_, State> {
|
||||
self.state.read().await
|
||||
@@ -49,12 +71,6 @@ impl Presence {
|
||||
async fn state_mut(&self) -> RwLockWriteGuard<'_, State> {
|
||||
self.state.write().await
|
||||
}
|
||||
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_async_method("overall_presence", async |_lua, this, ()| {
|
||||
Ok(this.state().await.current_overall_presence)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
||||
@@ -12,21 +12,27 @@ use google_home::device;
|
||||
use google_home::errors::ErrorCode;
|
||||
use google_home::traits::{self, Scene};
|
||||
use google_home::types::Type;
|
||||
use lua_typed::Typed;
|
||||
use rumqttc::Publish;
|
||||
use tracing::{debug, error, trace};
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "WolConfig")]
|
||||
pub struct Config {
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub info: InfoConfig,
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
pub mac_address: MacAddress,
|
||||
#[device_config(default(Ipv4Addr::new(255, 255, 255, 255)))]
|
||||
#[typed(default)]
|
||||
pub broadcast_ip: Ipv4Addr,
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
#[derive(Debug, Clone, Device)]
|
||||
pub struct WakeOnLAN {
|
||||
|
||||
@@ -8,24 +8,29 @@ use automation_lib::event::OnMqtt;
|
||||
use automation_lib::messages::PowerMessage;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::{Device, LuaDeviceConfig};
|
||||
use lua_typed::Typed;
|
||||
use rumqttc::Publish;
|
||||
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use tracing::{debug, error, trace};
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "WasherConfig")]
|
||||
pub struct Config {
|
||||
pub identifier: String,
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
// Power in Watt
|
||||
pub threshold: f32,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub done_callback: ActionCallback<Washer>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
}
|
||||
crate::register_type!(Config);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct State {
|
||||
|
||||
@@ -15,6 +15,7 @@ use google_home::device;
|
||||
use google_home::errors::ErrorCode;
|
||||
use google_home::traits::{Brightness, Color, ColorSetting, ColorTemperatureRange, OnOff};
|
||||
use google_home::types::Type;
|
||||
use lua_typed::Typed;
|
||||
use rumqttc::{Publish, matches};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
@@ -22,33 +23,47 @@ use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use tracing::{debug, trace, warn};
|
||||
|
||||
pub trait LightState:
|
||||
Debug + Clone + Default + Sync + Send + Serialize + Into<StateOnOff> + 'static
|
||||
Debug + Clone + Default + Sync + Send + Serialize + Into<StateOnOff> + Typed + 'static
|
||||
{
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct Config<T: LightState> {
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "ConfigLight")]
|
||||
pub struct Config<T: LightState>
|
||||
where
|
||||
Light<T>: Typed,
|
||||
{
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub info: InfoConfig,
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub callback: ActionCallback<(Light<T>, T)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
#[typed(default)]
|
||||
pub client: WrappedAsyncClient,
|
||||
}
|
||||
crate::register_type!(Config<StateOnOff>);
|
||||
crate::register_type!(Config<StateBrightness>);
|
||||
crate::register_type!(Config<StateColorTemperature>);
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize, Typed)]
|
||||
#[typed(as = "LightStateOnOff")]
|
||||
pub struct StateOnOff {
|
||||
#[serde(deserialize_with = "state_deserializer")]
|
||||
state: bool,
|
||||
}
|
||||
|
||||
impl LightState for StateOnOff {}
|
||||
crate::register_type!(StateOnOff);
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize, Typed)]
|
||||
#[typed(as = "LightStateBrightness")]
|
||||
pub struct StateBrightness {
|
||||
#[serde(deserialize_with = "state_deserializer")]
|
||||
state: bool,
|
||||
@@ -56,6 +71,7 @@ pub struct StateBrightness {
|
||||
}
|
||||
|
||||
impl LightState for StateBrightness {}
|
||||
crate::register_type!(StateBrightness);
|
||||
|
||||
impl From<StateBrightness> for StateOnOff {
|
||||
fn from(state: StateBrightness) -> Self {
|
||||
@@ -63,13 +79,15 @@ impl From<StateBrightness> for StateOnOff {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize, Typed)]
|
||||
#[typed(as = "LightStateColorTemperature")]
|
||||
pub struct StateColorTemperature {
|
||||
#[serde(deserialize_with = "state_deserializer")]
|
||||
state: bool,
|
||||
brightness: f32,
|
||||
color_temp: u32,
|
||||
}
|
||||
crate::register_type!(StateColorTemperature);
|
||||
|
||||
impl LightState for StateColorTemperature {}
|
||||
|
||||
@@ -92,7 +110,10 @@ impl From<StateColorTemperature> for StateBrightness {
|
||||
#[device(traits(OnOff for LightOnOff, LightBrightness, LightColorTemperature))]
|
||||
#[device(traits(Brightness for LightBrightness, LightColorTemperature))]
|
||||
#[device(traits(ColorSetting for LightColorTemperature))]
|
||||
pub struct Light<T: LightState> {
|
||||
pub struct Light<T: LightState>
|
||||
where
|
||||
Light<T>: Typed,
|
||||
{
|
||||
config: Config<T>,
|
||||
|
||||
state: Arc<RwLock<T>>,
|
||||
@@ -107,7 +128,10 @@ crate::register_device!(LightBrightness);
|
||||
pub type LightColorTemperature = Light<StateColorTemperature>;
|
||||
crate::register_device!(LightColorTemperature);
|
||||
|
||||
impl<T: LightState> Light<T> {
|
||||
impl<T: LightState> Light<T>
|
||||
where
|
||||
Light<T>: Typed,
|
||||
{
|
||||
async fn state(&self) -> RwLockReadGuard<'_, T> {
|
||||
self.state.read().await
|
||||
}
|
||||
@@ -118,7 +142,10 @@ impl<T: LightState> Light<T> {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: LightState> LuaDeviceCreate for Light<T> {
|
||||
impl<T: LightState> LuaDeviceCreate for Light<T>
|
||||
where
|
||||
Light<T>: Typed,
|
||||
{
|
||||
type Config = Config<T>;
|
||||
type Error = rumqttc::ClientError;
|
||||
|
||||
@@ -137,7 +164,10 @@ impl<T: LightState> LuaDeviceCreate for Light<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: LightState> Device for Light<T> {
|
||||
impl<T: LightState> Device for Light<T>
|
||||
where
|
||||
Light<T>: Typed,
|
||||
{
|
||||
fn get_id(&self) -> String {
|
||||
self.config.info.identifier()
|
||||
}
|
||||
@@ -257,7 +287,10 @@ impl OnMqtt for LightColorTemperature {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: LightState> google_home::Device for Light<T> {
|
||||
impl<T: LightState> google_home::Device for Light<T>
|
||||
where
|
||||
Light<T>: Typed,
|
||||
{
|
||||
fn get_device_type(&self) -> Type {
|
||||
Type::Light
|
||||
}
|
||||
@@ -288,6 +321,7 @@ impl<T: LightState> google_home::Device for Light<T> {
|
||||
impl<T> OnOff for Light<T>
|
||||
where
|
||||
T: LightState,
|
||||
Light<T>: Typed,
|
||||
{
|
||||
async fn on(&self) -> Result<bool, ErrorCode> {
|
||||
let state = self.state().await;
|
||||
@@ -327,6 +361,7 @@ impl<T> Brightness for Light<T>
|
||||
where
|
||||
T: LightState,
|
||||
T: Into<StateBrightness>,
|
||||
Light<T>: Typed,
|
||||
{
|
||||
async fn brightness(&self) -> Result<u8, ErrorCode> {
|
||||
let state = self.state().await;
|
||||
@@ -368,6 +403,7 @@ impl<T> ColorSetting for Light<T>
|
||||
where
|
||||
T: LightState,
|
||||
T: Into<StateColorTemperature>,
|
||||
Light<T>: Typed,
|
||||
{
|
||||
fn color_temperature_range(&self) -> ColorTemperatureRange {
|
||||
ColorTemperatureRange {
|
||||
|
||||
@@ -15,6 +15,7 @@ use google_home::device;
|
||||
use google_home::errors::ErrorCode;
|
||||
use google_home::traits::OnOff;
|
||||
use google_home::types::Type;
|
||||
use lua_typed::Typed;
|
||||
use rumqttc::{Publish, matches};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
@@ -22,15 +23,16 @@ use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use tracing::{debug, trace, warn};
|
||||
|
||||
pub trait OutletState:
|
||||
Debug + Clone + Default + Sync + Send + Serialize + Into<StateOnOff> + 'static
|
||||
Debug + Clone + Default + Sync + Send + Serialize + Into<StateOnOff> + Typed + 'static
|
||||
{
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Copy)]
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Copy, Typed)]
|
||||
pub enum OutletType {
|
||||
Outlet,
|
||||
Kettle,
|
||||
}
|
||||
crate::register_type!(OutletType);
|
||||
|
||||
impl From<OutletType> for Type {
|
||||
fn from(outlet: OutletType) -> Self {
|
||||
@@ -41,36 +43,50 @@ impl From<OutletType> for Type {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct Config<T: OutletState> {
|
||||
#[derive(Debug, Clone, LuaDeviceConfig, Typed)]
|
||||
#[typed(as = "ConfigOutlet")]
|
||||
pub struct Config<T: OutletState>
|
||||
where
|
||||
Outlet<T>: Typed,
|
||||
{
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub info: InfoConfig,
|
||||
#[device_config(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
#[device_config(default(OutletType::Outlet))]
|
||||
#[typed(default)]
|
||||
pub outlet_type: OutletType,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub callback: ActionCallback<(Outlet<T>, T)>,
|
||||
|
||||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
}
|
||||
crate::register_type!(Config<StateOnOff>);
|
||||
crate::register_type!(Config<StatePower>);
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize, Typed)]
|
||||
#[typed(as = "OutletStateOnOff")]
|
||||
pub struct StateOnOff {
|
||||
#[serde(deserialize_with = "state_deserializer")]
|
||||
state: bool,
|
||||
}
|
||||
crate::register_type!(StateOnOff);
|
||||
|
||||
impl OutletState for StateOnOff {}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize, Typed)]
|
||||
#[typed(as = "OutletStatePower")]
|
||||
pub struct StatePower {
|
||||
#[serde(deserialize_with = "state_deserializer")]
|
||||
state: bool,
|
||||
power: f64,
|
||||
}
|
||||
crate::register_type!(StatePower);
|
||||
|
||||
impl OutletState for StatePower {}
|
||||
|
||||
@@ -82,7 +98,10 @@ impl From<StatePower> for StateOnOff {
|
||||
|
||||
#[derive(Debug, Clone, Device)]
|
||||
#[device(traits(OnOff for OutletOnOff, OutletPower))]
|
||||
pub struct Outlet<T: OutletState> {
|
||||
pub struct Outlet<T: OutletState>
|
||||
where
|
||||
Outlet<T>: Typed,
|
||||
{
|
||||
config: Config<T>,
|
||||
|
||||
state: Arc<RwLock<T>>,
|
||||
@@ -94,7 +113,10 @@ crate::register_device!(OutletOnOff);
|
||||
pub type OutletPower = Outlet<StatePower>;
|
||||
crate::register_device!(OutletPower);
|
||||
|
||||
impl<T: OutletState> Outlet<T> {
|
||||
impl<T: OutletState> Outlet<T>
|
||||
where
|
||||
Outlet<T>: Typed,
|
||||
{
|
||||
async fn state(&self) -> RwLockReadGuard<'_, T> {
|
||||
self.state.read().await
|
||||
}
|
||||
@@ -105,7 +127,10 @@ impl<T: OutletState> Outlet<T> {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: OutletState> LuaDeviceCreate for Outlet<T> {
|
||||
impl<T: OutletState> LuaDeviceCreate for Outlet<T>
|
||||
where
|
||||
Outlet<T>: Typed,
|
||||
{
|
||||
type Config = Config<T>;
|
||||
type Error = rumqttc::ClientError;
|
||||
|
||||
@@ -124,7 +149,10 @@ impl<T: OutletState> LuaDeviceCreate for Outlet<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: OutletState> Device for Outlet<T> {
|
||||
impl<T: OutletState> Device for Outlet<T>
|
||||
where
|
||||
Outlet<T>: Typed,
|
||||
{
|
||||
fn get_id(&self) -> String {
|
||||
self.config.info.identifier()
|
||||
}
|
||||
@@ -201,7 +229,10 @@ impl OnMqtt for OutletPower {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: OutletState> google_home::Device for Outlet<T> {
|
||||
impl<T: OutletState> google_home::Device for Outlet<T>
|
||||
where
|
||||
Outlet<T>: Typed,
|
||||
{
|
||||
fn get_device_type(&self) -> Type {
|
||||
self.config.outlet_type.into()
|
||||
}
|
||||
@@ -232,6 +263,7 @@ impl<T: OutletState> google_home::Device for Outlet<T> {
|
||||
impl<T> OnOff for Outlet<T>
|
||||
where
|
||||
T: OutletState,
|
||||
Outlet<T>: Typed,
|
||||
{
|
||||
async fn on(&self) -> Result<bool, ErrorCode> {
|
||||
let state = self.state().await;
|
||||
|
||||
@@ -13,6 +13,7 @@ google_home = { workspace = true }
|
||||
hostname = { workspace = true }
|
||||
indexmap = { workspace = true }
|
||||
inventory = { workspace = true }
|
||||
lua_typed = { workspace = true }
|
||||
mlua = { workspace = true }
|
||||
rumqttc = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::future::try_join_all;
|
||||
use lua_typed::Typed;
|
||||
use mlua::{FromLua, IntoLuaMulti};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -9,6 +10,29 @@ pub struct ActionCallback<P> {
|
||||
_parameters: PhantomData<P>,
|
||||
}
|
||||
|
||||
impl Typed for ActionCallback<()> {
|
||||
fn type_name() -> String {
|
||||
"fun() | fun()[]".into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Typed> Typed for ActionCallback<A> {
|
||||
fn type_name() -> String {
|
||||
let type_name = A::type_name();
|
||||
format!("fun(_: {type_name}) | fun(_: {type_name})[]")
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Typed, B: Typed> Typed for ActionCallback<(A, B)> {
|
||||
fn type_name() -> String {
|
||||
let type_name_a = A::type_name();
|
||||
let type_name_b = B::type_name();
|
||||
format!(
|
||||
"fun(_: {type_name_a}, _: {type_name_b}) | fun(_: {type_name_a}, _: {type_name_b})[]"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: For some reason the derive macro combined with PhantomData leads to issues where it
|
||||
// requires all types part of P to implement default, even if they never actually get constructed.
|
||||
// By manually implemented Default it works fine.
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
use std::time::Duration;
|
||||
|
||||
use lua_typed::Typed;
|
||||
use rumqttc::{MqttOptions, Transport};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Typed)]
|
||||
pub struct MqttConfig {
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
@@ -52,7 +53,7 @@ fn default_fulfillment_port() -> u16 {
|
||||
7878
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Typed)]
|
||||
pub struct InfoConfig {
|
||||
pub name: String,
|
||||
pub room: Option<String>,
|
||||
@@ -68,7 +69,7 @@ impl InfoConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Typed)]
|
||||
pub struct MqttDeviceConfig {
|
||||
pub topic: String,
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ use std::sync::Arc;
|
||||
|
||||
use futures::Future;
|
||||
use futures::future::join_all;
|
||||
use lua_typed::Typed;
|
||||
use mlua::FromLua;
|
||||
use tokio::sync::{RwLock, RwLockReadGuard};
|
||||
use tokio_cron_scheduler::{Job, JobScheduler};
|
||||
use tracing::{debug, instrument, trace};
|
||||
@@ -13,7 +15,7 @@ use crate::event::{Event, EventChannel, OnMqtt};
|
||||
|
||||
pub type DeviceMap = HashMap<String, Box<dyn Device>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, FromLua)]
|
||||
pub struct DeviceManager {
|
||||
devices: Arc<RwLock<DeviceMap>>,
|
||||
event_channel: EventChannel,
|
||||
@@ -142,3 +144,9 @@ impl mlua::UserData for DeviceManager {
|
||||
methods.add_method("event_channel", |_lua, this, ()| Ok(this.event_channel()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Typed for DeviceManager {
|
||||
fn type_name() -> String {
|
||||
"DeviceManager".into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#![allow(incomplete_features)]
|
||||
#![feature(iterator_try_collect)]
|
||||
#![feature(with_negative_coherence)]
|
||||
|
||||
use tracing::debug;
|
||||
|
||||
@@ -16,15 +16,25 @@ pub mod mqtt;
|
||||
pub mod schedule;
|
||||
|
||||
type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::Table>;
|
||||
type DefinitionsFn = fn() -> String;
|
||||
|
||||
pub struct Module {
|
||||
name: &'static str,
|
||||
register_fn: RegisterFn,
|
||||
definitions_fn: Option<DefinitionsFn>,
|
||||
}
|
||||
|
||||
impl Module {
|
||||
pub const fn new(name: &'static str, register_fn: RegisterFn) -> Self {
|
||||
Self { name, register_fn }
|
||||
pub const fn new(
|
||||
name: &'static str,
|
||||
register_fn: RegisterFn,
|
||||
definitions_fn: Option<DefinitionsFn>,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
register_fn,
|
||||
definitions_fn,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn get_name(&self) -> &'static str {
|
||||
@@ -34,13 +44,17 @@ impl Module {
|
||||
pub fn register(&self, lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
(self.register_fn)(lua)
|
||||
}
|
||||
|
||||
pub fn definitions(&self) -> Option<String> {
|
||||
self.definitions_fn.map(|f| f())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_modules(lua: &mlua::Lua) -> mlua::Result<()> {
|
||||
for module in inventory::iter::<Module> {
|
||||
debug!(name = module.get_name(), "Loading module");
|
||||
let table = module.register(lua)?;
|
||||
lua.register_module(&format!("automation:{}", module.get_name()), table)?;
|
||||
lua.register_module(module.get_name(), table)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -2,11 +2,40 @@ use std::ops::Deref;
|
||||
|
||||
// TODO: Enable and disable functions based on query_only and command_only
|
||||
|
||||
pub trait OnOff {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
|
||||
pub trait PartialUserData<T> {
|
||||
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M);
|
||||
|
||||
fn interface_name() -> Option<&'static str> {
|
||||
None
|
||||
}
|
||||
|
||||
fn definitions() -> Option<String> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Device;
|
||||
|
||||
impl<T> PartialUserData<T> for Device
|
||||
where
|
||||
Self: Sized + google_home::traits::OnOff + 'static,
|
||||
T: crate::device::Device + 'static,
|
||||
{
|
||||
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M) {
|
||||
methods.add_async_method("get_id", async |_lua, this, _: ()| Ok(this.get_id()));
|
||||
}
|
||||
|
||||
fn interface_name() -> Option<&'static str> {
|
||||
Some("DeviceInterface")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OnOff;
|
||||
|
||||
impl<T> PartialUserData<T> for OnOff
|
||||
where
|
||||
T: google_home::traits::OnOff + 'static,
|
||||
{
|
||||
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M) {
|
||||
methods.add_async_method("set_on", async |_lua, this, on: bool| {
|
||||
this.deref().set_on(on).await.unwrap();
|
||||
|
||||
@@ -17,14 +46,19 @@ pub trait OnOff {
|
||||
Ok(this.deref().on().await.unwrap())
|
||||
});
|
||||
}
|
||||
}
|
||||
impl<T> OnOff for T where T: google_home::traits::OnOff {}
|
||||
|
||||
pub trait Brightness {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
|
||||
fn interface_name() -> Option<&'static str> {
|
||||
Some("OnOffInterface")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Brightness;
|
||||
|
||||
impl<T> PartialUserData<T> for Brightness
|
||||
where
|
||||
Self: Sized + google_home::traits::Brightness + 'static,
|
||||
T: google_home::traits::Brightness + 'static,
|
||||
{
|
||||
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M) {
|
||||
methods.add_async_method("set_brightness", async |_lua, this, brightness: u8| {
|
||||
this.set_brightness(brightness).await.unwrap();
|
||||
|
||||
@@ -35,14 +69,19 @@ pub trait Brightness {
|
||||
Ok(this.brightness().await.unwrap())
|
||||
});
|
||||
}
|
||||
}
|
||||
impl<T> Brightness for T where T: google_home::traits::Brightness {}
|
||||
|
||||
pub trait ColorSetting {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
|
||||
fn interface_name() -> Option<&'static str> {
|
||||
Some("BrightnessInterface")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ColorSetting;
|
||||
|
||||
impl<T> PartialUserData<T> for ColorSetting
|
||||
where
|
||||
Self: Sized + google_home::traits::ColorSetting + 'static,
|
||||
T: google_home::traits::ColorSetting + 'static,
|
||||
{
|
||||
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M) {
|
||||
methods.add_async_method(
|
||||
"set_color_temperature",
|
||||
async |_lua, this, temperature: u32| {
|
||||
@@ -58,14 +97,19 @@ pub trait ColorSetting {
|
||||
Ok(this.color().await.temperature)
|
||||
});
|
||||
}
|
||||
}
|
||||
impl<T> ColorSetting for T where T: google_home::traits::ColorSetting {}
|
||||
|
||||
pub trait OpenClose {
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M)
|
||||
fn interface_name() -> Option<&'static str> {
|
||||
Some("ColorSettingInterface")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OpenClose;
|
||||
|
||||
impl<T> PartialUserData<T> for OpenClose
|
||||
where
|
||||
Self: Sized + google_home::traits::OpenClose + 'static,
|
||||
T: google_home::traits::OpenClose + 'static,
|
||||
{
|
||||
fn add_methods<M: mlua::UserDataMethods<T>>(methods: &mut M) {
|
||||
methods.add_async_method("set_open_percent", async |_lua, this, open_percent: u8| {
|
||||
this.set_open_percent(open_percent).await.unwrap();
|
||||
|
||||
@@ -76,5 +120,8 @@ pub trait OpenClose {
|
||||
Ok(this.open_percent().await.unwrap())
|
||||
});
|
||||
}
|
||||
|
||||
fn interface_name() -> Option<&'static str> {
|
||||
Some("OpenCloseInterface")
|
||||
}
|
||||
}
|
||||
impl<T> OpenClose for T where T: google_home::traits::OpenClose {}
|
||||
|
||||
@@ -2,6 +2,7 @@ mod timeout;
|
||||
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use lua_typed::Typed;
|
||||
pub use timeout::Timeout;
|
||||
|
||||
use crate::Module;
|
||||
@@ -9,7 +10,7 @@ use crate::Module;
|
||||
fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
let utils = lua.create_table()?;
|
||||
|
||||
utils.set("Timeout", lua.create_proxy::<Timeout>()?)?;
|
||||
utils.set(Timeout::type_name(), lua.create_proxy::<Timeout>()?)?;
|
||||
|
||||
let get_hostname = lua.create_function(|_lua, ()| {
|
||||
hostname::get()
|
||||
@@ -28,4 +29,20 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
Ok(utils)
|
||||
}
|
||||
|
||||
inventory::submit! {Module::new("utils", create_module)}
|
||||
fn generate_definitions() -> String {
|
||||
let mut output = String::new();
|
||||
|
||||
output += "---@meta\n\nlocal utils\n\n";
|
||||
|
||||
output += &Timeout::generate_full().expect("Timeout should have generate_full");
|
||||
output += "\n";
|
||||
|
||||
output += "---@return string\nfunction utils.get_hostname() end\n\n";
|
||||
output += "---@return integer\nfunction utils.get_epoch() end\n\n";
|
||||
|
||||
output += "return utils";
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
inventory::submit! {Module::new("automation:utils", create_module, Some(generate_definitions))}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use lua_typed::Typed;
|
||||
use tokio::sync::RwLock;
|
||||
use tokio::task::JoinHandle;
|
||||
use tracing::debug;
|
||||
@@ -74,3 +75,44 @@ impl mlua::UserData for Timeout {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Typed for Timeout {
|
||||
fn type_name() -> String {
|
||||
"Timeout".into()
|
||||
}
|
||||
|
||||
fn generate_header() -> Option<String> {
|
||||
let type_name = Self::type_name();
|
||||
Some(format!("---@class {type_name}\nlocal {type_name}\n"))
|
||||
}
|
||||
|
||||
fn generate_members() -> Option<String> {
|
||||
let mut output = String::new();
|
||||
|
||||
let type_name = Self::type_name();
|
||||
|
||||
output += &format!(
|
||||
"---@async\n---@param timeout number\n---@param callback {}\nfunction {type_name}:start(timeout, callback) end\n",
|
||||
ActionCallback::<()>::type_name()
|
||||
);
|
||||
|
||||
output += &format!("---@async\nfunction {type_name}:cancel() end\n",);
|
||||
|
||||
output +=
|
||||
&format!("---@async\n---@return boolean\nfunction {type_name}:is_waiting() end\n",);
|
||||
|
||||
Some(output)
|
||||
}
|
||||
|
||||
fn generate_footer() -> Option<String> {
|
||||
let mut output = String::new();
|
||||
|
||||
let type_name = Self::type_name();
|
||||
|
||||
output += &format!("utils.{type_name} = {{}}\n");
|
||||
output += &format!("---@return {type_name}\n");
|
||||
output += &format!("function utils.{type_name}.new() end\n");
|
||||
|
||||
Some(output)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,55 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use mlua::FromLua;
|
||||
use lua_typed::Typed;
|
||||
use mlua::{FromLua, LuaSerdeExt};
|
||||
use rumqttc::{AsyncClient, Event, EventLoop, Incoming};
|
||||
use tracing::{debug, warn};
|
||||
|
||||
use crate::Module;
|
||||
use crate::config::MqttConfig;
|
||||
use crate::device_manager::DeviceManager;
|
||||
use crate::event::{self, EventChannel};
|
||||
|
||||
#[derive(Debug, Clone, FromLua)]
|
||||
pub struct WrappedAsyncClient(pub AsyncClient);
|
||||
|
||||
impl Typed for WrappedAsyncClient {
|
||||
fn type_name() -> String {
|
||||
"AsyncClient".into()
|
||||
}
|
||||
|
||||
fn generate_header() -> Option<String> {
|
||||
let type_name = Self::type_name();
|
||||
Some(format!("---@class {type_name}\nlocal {type_name}\n"))
|
||||
}
|
||||
|
||||
fn generate_members() -> Option<String> {
|
||||
let mut output = String::new();
|
||||
|
||||
let type_name = Self::type_name();
|
||||
|
||||
output += &format!(
|
||||
"---@async\n---@param topic string\n---@param message table?\nfunction {type_name}:send_message(topic, message) end\n"
|
||||
);
|
||||
|
||||
Some(output)
|
||||
}
|
||||
|
||||
fn generate_footer() -> Option<String> {
|
||||
let mut output = String::new();
|
||||
|
||||
let type_name = Self::type_name();
|
||||
|
||||
output += &format!("mqtt.{type_name} = {{}}\n");
|
||||
output += &format!("---@param device_manager {}\n", DeviceManager::type_name());
|
||||
output += &format!("---@param config {}\n", MqttConfig::type_name());
|
||||
output += &format!("---@return {type_name}\n");
|
||||
output += "function mqtt.new(device_manager, config) end\n";
|
||||
|
||||
Some(output)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for WrappedAsyncClient {
|
||||
type Target = AsyncClient;
|
||||
|
||||
@@ -70,3 +111,41 @@ pub fn start(mut eventloop: EventLoop, event_channel: &EventChannel) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
let mqtt = lua.create_table()?;
|
||||
let mqtt_new = lua.create_function(
|
||||
move |lua, (device_manager, config): (DeviceManager, mlua::Value)| {
|
||||
let event_channel = device_manager.event_channel();
|
||||
let config: MqttConfig = lua.from_value(config)?;
|
||||
|
||||
// Create a mqtt client
|
||||
// TODO: When starting up, the devices are not yet created, this could lead to a device being out of sync
|
||||
let (client, eventloop) = AsyncClient::new(config.into(), 100);
|
||||
start(eventloop, &event_channel);
|
||||
|
||||
Ok(WrappedAsyncClient(client))
|
||||
},
|
||||
)?;
|
||||
mqtt.set("new", mqtt_new)?;
|
||||
|
||||
Ok(mqtt)
|
||||
}
|
||||
|
||||
fn generate_definitions() -> String {
|
||||
let mut output = String::new();
|
||||
|
||||
output += "---@meta\n\nlocal mqtt\n\n";
|
||||
|
||||
output += &MqttConfig::generate_full().expect("WrappedAsyncClient should have generate_full");
|
||||
output += "\n";
|
||||
output +=
|
||||
&WrappedAsyncClient::generate_full().expect("WrappedAsyncClient should have generate_full");
|
||||
output += "\n";
|
||||
|
||||
output += "return mqtt";
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
inventory::submit! {Module::new("automation:mqtt", create_module, Some(generate_definitions))}
|
||||
|
||||
@@ -1,35 +1,36 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::{ToTokens, quote};
|
||||
use quote::quote;
|
||||
use syn::parse::{Parse, ParseStream};
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::spanned::Spanned;
|
||||
use syn::{Attribute, DeriveInput, Token, parenthesized};
|
||||
|
||||
enum Attr {
|
||||
Trait(TraitAttr),
|
||||
AddMethods(AddMethodsAttr),
|
||||
ExtraUserData(ExtraUserDataAttr),
|
||||
}
|
||||
|
||||
impl Parse for Attr {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
let ident: syn::Ident = input.parse()?;
|
||||
|
||||
let attr;
|
||||
_ = parenthesized!(attr in input);
|
||||
|
||||
let attr = match ident.to_string().as_str() {
|
||||
"traits" => Attr::Trait(attr.parse()?),
|
||||
"add_methods" => Attr::AddMethods(attr.parse()?),
|
||||
_ => {
|
||||
return Err(syn::Error::new(
|
||||
ident.span(),
|
||||
"Expected 'traits' or 'add_methods'",
|
||||
));
|
||||
impl Attr {
|
||||
fn parse(attr: &Attribute) -> syn::Result<Self> {
|
||||
let mut parsed = None;
|
||||
attr.parse_nested_meta(|meta| {
|
||||
if meta.path.is_ident("traits") {
|
||||
let input;
|
||||
_ = parenthesized!(input in meta.input);
|
||||
parsed = Some(Attr::Trait(input.parse()?));
|
||||
} else if meta.path.is_ident("extra_user_data") {
|
||||
let value = meta.value()?;
|
||||
parsed = Some(Attr::ExtraUserData(value.parse()?));
|
||||
} else {
|
||||
return Err(syn::Error::new(meta.path.span(), "Unknown attribute"));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(attr)
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(parsed.expect("Parsed should be set"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,18 +66,6 @@ impl Parse for Traits {
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Traits {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream2) {
|
||||
let Self(traits) = &self;
|
||||
|
||||
tokens.extend(quote! {
|
||||
#(
|
||||
::automation_lib::lua::traits::#traits::add_methods(methods);
|
||||
)*
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Aliases(Vec<syn::Ident>);
|
||||
|
||||
@@ -106,28 +95,18 @@ impl Parse for Aliases {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AddMethodsAttr(syn::Path);
|
||||
struct ExtraUserDataAttr(syn::Ident);
|
||||
|
||||
impl Parse for AddMethodsAttr {
|
||||
impl Parse for ExtraUserDataAttr {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
Ok(Self(input.parse()?))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for AddMethodsAttr {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream2) {
|
||||
let Self(path) = self;
|
||||
|
||||
tokens.extend(quote! {
|
||||
#path
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct Implementation {
|
||||
name: syn::Ident,
|
||||
traits: Traits,
|
||||
add_methods: Vec<AddMethodsAttr>,
|
||||
extra_user_data: Vec<ExtraUserDataAttr>,
|
||||
}
|
||||
|
||||
impl quote::ToTokens for Implementation {
|
||||
@@ -135,8 +114,10 @@ impl quote::ToTokens for Implementation {
|
||||
let Self {
|
||||
name,
|
||||
traits,
|
||||
add_methods,
|
||||
extra_user_data,
|
||||
} = &self;
|
||||
let Traits(traits) = traits;
|
||||
let extra_user_data: Vec<_> = extra_user_data.iter().map(|tr| tr.0.clone()).collect();
|
||||
|
||||
tokens.extend(quote! {
|
||||
impl mlua::UserData for #name {
|
||||
@@ -154,13 +135,64 @@ impl quote::ToTokens for Implementation {
|
||||
Ok(b)
|
||||
});
|
||||
|
||||
methods.add_async_method("get_id", async |_lua, this, _: ()| { Ok(this.get_id()) });
|
||||
|
||||
#traits
|
||||
<::automation_lib::lua::traits::Device as ::automation_lib::lua::traits::PartialUserData<#name>>::add_methods(methods);
|
||||
|
||||
#(
|
||||
#add_methods(methods);
|
||||
<::automation_lib::lua::traits::#traits as ::automation_lib::lua::traits::PartialUserData<#name>>::add_methods(methods);
|
||||
)*
|
||||
|
||||
#(
|
||||
<#extra_user_data as ::automation_lib::lua::traits::PartialUserData<#name>>::add_methods(methods);
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
impl ::lua_typed::Typed for #name {
|
||||
fn type_name() -> String {
|
||||
stringify!(#name).into()
|
||||
}
|
||||
|
||||
fn generate_header() -> std::option::Option<::std::string::String> {
|
||||
let type_name = <Self as ::lua_typed::Typed>::type_name();
|
||||
let mut output = String::new();
|
||||
|
||||
let interfaces: String = [
|
||||
<::automation_lib::lua::traits::Device as ::automation_lib::lua::traits::PartialUserData<#name>>::interface_name(),
|
||||
#(
|
||||
<::automation_lib::lua::traits::#traits as ::automation_lib::lua::traits::PartialUserData<#name>>::interface_name(),
|
||||
)*
|
||||
].into_iter().flatten().intersperse(", ").collect();
|
||||
|
||||
let interfaces = if interfaces.is_empty() {
|
||||
"".into()
|
||||
} else {
|
||||
format!(": {interfaces}")
|
||||
};
|
||||
|
||||
Some(format!("---@class {type_name}{interfaces}\nlocal {type_name}\n"))
|
||||
}
|
||||
|
||||
fn generate_members() -> Option<String> {
|
||||
let mut output = String::new();
|
||||
|
||||
let type_name = <Self as ::lua_typed::Typed>::type_name();
|
||||
output += &format!("devices.{type_name} = {{}}\n");
|
||||
let config_name = <<Self as ::automation_lib::device::LuaDeviceCreate>::Config as ::lua_typed::Typed>::type_name();
|
||||
output += &format!("---@param config {config_name}\n");
|
||||
output += &format!("---@return {type_name}\n");
|
||||
output += &format!("function devices.{type_name}.new(config) end\n");
|
||||
|
||||
output += &<::automation_lib::lua::traits::Device as ::automation_lib::lua::traits::PartialUserData<#name>>::definitions().unwrap_or("".into());
|
||||
|
||||
#(
|
||||
output += &<::automation_lib::lua::traits::#traits as ::automation_lib::lua::traits::PartialUserData<#name>>::definitions().unwrap_or("".into());
|
||||
)*
|
||||
#(
|
||||
output += &<#extra_user_data as ::automation_lib::lua::traits::PartialUserData<#name>>::definitions().unwrap_or("".into());
|
||||
)*
|
||||
|
||||
|
||||
Some(output)
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -188,7 +220,7 @@ impl Implementations {
|
||||
all.extend(&attribute.traits);
|
||||
}
|
||||
}
|
||||
Attr::AddMethods(attribute) => add_methods.push(attribute),
|
||||
Attr::ExtraUserData(attribute) => add_methods.push(attribute),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,7 +238,7 @@ impl Implementations {
|
||||
.map(|(alias, traits)| Implementation {
|
||||
name: alias.unwrap_or(name.clone()),
|
||||
traits,
|
||||
add_methods: add_methods.clone(),
|
||||
extra_user_data: add_methods.clone(),
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
@@ -218,7 +250,7 @@ pub fn device(input: DeriveInput) -> TokenStream2 {
|
||||
.attrs
|
||||
.iter()
|
||||
.filter(|attr| attr.path().is_ident("device"))
|
||||
.map(Attribute::parse_args)
|
||||
.map(Attr::parse)
|
||||
.try_collect::<Vec<_>>()
|
||||
{
|
||||
Ok(attr) => Implementations::from_attr(attr, input.ident),
|
||||
|
||||
@@ -64,7 +64,7 @@ pub fn lua_serialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream
|
||||
/// ```
|
||||
/// It can then be registered with:
|
||||
/// ```rust
|
||||
/// #[device(add_methods(top_secret))]
|
||||
/// #[device(add_methods = top_secret)]
|
||||
/// ```
|
||||
#[proc_macro_derive(Device, attributes(device))]
|
||||
pub fn device(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
|
||||
84
config.lua
84
config.lua
@@ -2,17 +2,21 @@ local devices = require("automation:devices")
|
||||
local device_manager = require("automation:device_manager")
|
||||
local utils = require("automation:utils")
|
||||
local secrets = require("automation:secrets")
|
||||
local debug = require("automation:variables").debug or false
|
||||
local debug = require("automation:variables").debug and true or false
|
||||
|
||||
print(_VERSION)
|
||||
|
||||
local host = utils.get_hostname()
|
||||
print("Running @" .. host)
|
||||
|
||||
--- @param topic string
|
||||
--- @return string
|
||||
local function mqtt_z2m(topic)
|
||||
return "zigbee2mqtt/" .. topic
|
||||
end
|
||||
|
||||
--- @param topic string
|
||||
--- @return string
|
||||
local function mqtt_automation(topic)
|
||||
return "automation/" .. topic
|
||||
end
|
||||
@@ -21,7 +25,7 @@ local fulfillment = {
|
||||
openid_url = "https://login.huizinga.dev/api/oidc",
|
||||
}
|
||||
|
||||
local mqtt_client = require("automation:mqtt").new({
|
||||
local mqtt_client = require("automation:mqtt").new(device_manager, {
|
||||
host = ((host == "zeus" or host == "hephaestus") and "olympus.lan.huizinga.dev") or "mosquitto",
|
||||
port = 8883,
|
||||
client_name = "automation-" .. host,
|
||||
@@ -30,12 +34,19 @@ local mqtt_client = require("automation:mqtt").new({
|
||||
tls = host == "zeus" or host == "hephaestus",
|
||||
})
|
||||
|
||||
local ntfy_topic = secrets.ntfy_topic
|
||||
if ntfy_topic == nil then
|
||||
error("Ntfy topic is not specified")
|
||||
end
|
||||
local ntfy = devices.Ntfy.new({
|
||||
topic = secrets.ntfy_topic,
|
||||
topic = ntfy_topic,
|
||||
})
|
||||
device_manager:add(ntfy)
|
||||
|
||||
--- @type {[string]: number}
|
||||
local low_battery = {}
|
||||
--- @param device DeviceInterface
|
||||
--- @param battery number
|
||||
local function check_battery(device, battery)
|
||||
local id = device:get_id()
|
||||
if battery < 15 then
|
||||
@@ -66,11 +77,13 @@ device_manager:schedule("0 0 21 */1 * *", function()
|
||||
})
|
||||
end)
|
||||
|
||||
local on_presence = {
|
||||
add = function(self, f)
|
||||
--- @class OnPresence
|
||||
--- @field [integer] fun(presence: boolean)
|
||||
local on_presence = {}
|
||||
--- @param f fun(presence: boolean)
|
||||
function on_presence:add(f)
|
||||
self[#self + 1] = f
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
local presence_system = devices.Presence.new({
|
||||
topic = mqtt_automation("presence/+/#"),
|
||||
@@ -110,11 +123,13 @@ on_presence:add(function(presence)
|
||||
})
|
||||
end)
|
||||
|
||||
local window_sensors = {
|
||||
add = function(self, f)
|
||||
self[#self + 1] = f
|
||||
end,
|
||||
}
|
||||
--- @class WindowSensor
|
||||
--- @field [integer] OpenCloseInterface
|
||||
local window_sensors = {}
|
||||
--- @param sensor OpenCloseInterface
|
||||
function window_sensors:add(sensor)
|
||||
self[#self + 1] = sensor
|
||||
end
|
||||
on_presence:add(function(presence)
|
||||
if not presence then
|
||||
local open = {}
|
||||
@@ -139,6 +154,7 @@ on_presence:add(function(presence)
|
||||
end
|
||||
end)
|
||||
|
||||
--- @param device OnOffInterface
|
||||
local function turn_off_when_away(device)
|
||||
on_presence:add(function(presence)
|
||||
if not presence then
|
||||
@@ -147,11 +163,13 @@ local function turn_off_when_away(device)
|
||||
end)
|
||||
end
|
||||
|
||||
local on_light = {
|
||||
add = function(self, f)
|
||||
--- @class OnLight
|
||||
--- @field [integer] fun(light: boolean)
|
||||
local on_light = {}
|
||||
--- @param f fun(light: boolean)
|
||||
function on_light:add(f)
|
||||
self[#self + 1] = f
|
||||
end,
|
||||
}
|
||||
end
|
||||
device_manager:add(devices.LightSensor.new({
|
||||
identifier = "living_light_sensor",
|
||||
topic = mqtt_z2m("living/light"),
|
||||
@@ -175,6 +193,9 @@ end)
|
||||
|
||||
local hue_ip = "10.0.0.102"
|
||||
local hue_token = secrets.hue_token
|
||||
if hue_token == nil then
|
||||
error("Hue token is not specified")
|
||||
end
|
||||
|
||||
local hue_bridge = devices.HueBridge.new({
|
||||
identifier = "hue_bridge",
|
||||
@@ -287,6 +308,7 @@ device_manager:add(devices.IkeaRemote.new({
|
||||
battery_callback = check_battery,
|
||||
}))
|
||||
|
||||
--- @return fun(self: OnOffInterface, state: {state: boolean, power: number})
|
||||
local function kettle_timeout()
|
||||
local timeout = utils.Timeout.new()
|
||||
|
||||
@@ -301,6 +323,7 @@ local function kettle_timeout()
|
||||
end
|
||||
end
|
||||
|
||||
--- @type OutletPower
|
||||
local kettle = devices.OutletPower.new({
|
||||
outlet_type = "Kettle",
|
||||
name = "Kettle",
|
||||
@@ -312,6 +335,7 @@ local kettle = devices.OutletPower.new({
|
||||
turn_off_when_away(kettle)
|
||||
device_manager:add(kettle)
|
||||
|
||||
--- @param on boolean
|
||||
local function set_kettle(_, on)
|
||||
kettle:set_on(on)
|
||||
end
|
||||
@@ -336,6 +360,8 @@ device_manager:add(devices.IkeaRemote.new({
|
||||
battery_callback = check_battery,
|
||||
}))
|
||||
|
||||
--- @param duration number
|
||||
--- @return fun(self: OnOffInterface, state: {state: boolean})
|
||||
local function off_timeout(duration)
|
||||
local timeout = utils.Timeout.new()
|
||||
|
||||
@@ -455,14 +481,19 @@ device_manager:add(devices.HueSwitch.new({
|
||||
local hallway_light_automation = {
|
||||
timeout = utils.Timeout.new(),
|
||||
forced = false,
|
||||
switch_callback = function(self)
|
||||
trash = nil,
|
||||
door = nil,
|
||||
}
|
||||
---@return fun(_, on: boolean)
|
||||
function hallway_light_automation:switch_callback()
|
||||
return function(_, on)
|
||||
self.timeout:cancel()
|
||||
self.group.set_on(on)
|
||||
self.forced = on
|
||||
end
|
||||
end,
|
||||
door_callback = function(self)
|
||||
end
|
||||
---@return fun(_, open: boolean)
|
||||
function hallway_light_automation:door_callback()
|
||||
return function(_, open)
|
||||
if open then
|
||||
self.timeout:cancel()
|
||||
@@ -476,8 +507,9 @@ local hallway_light_automation = {
|
||||
end)
|
||||
end
|
||||
end
|
||||
end,
|
||||
trash_callback = function(self)
|
||||
end
|
||||
---@return fun(_, open: boolean)
|
||||
function hallway_light_automation:trash_callback()
|
||||
return function(_, open)
|
||||
if open then
|
||||
self.group.set_on(true)
|
||||
@@ -491,8 +523,9 @@ local hallway_light_automation = {
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
light_callback = function(self)
|
||||
end
|
||||
---@return fun(_, state: { on: boolean })
|
||||
function hallway_light_automation:light_callback()
|
||||
return function(_, state)
|
||||
if
|
||||
state.on
|
||||
@@ -507,8 +540,7 @@ local hallway_light_automation = {
|
||||
self.forced = false
|
||||
end
|
||||
end
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
local hallway_storage = devices.LightBrightness.new({
|
||||
name = "Storage",
|
||||
@@ -540,6 +572,8 @@ hallway_light_automation.group = {
|
||||
end,
|
||||
}
|
||||
|
||||
---@param duration number
|
||||
---@return fun(_, open: boolean)
|
||||
local function presence(duration)
|
||||
local timeout = utils.Timeout.new()
|
||||
|
||||
|
||||
12
definitions/automation:device_manager.lua
Normal file
12
definitions/automation:device_manager.lua
Normal file
@@ -0,0 +1,12 @@
|
||||
---@meta
|
||||
|
||||
---@class DeviceManager
|
||||
local DeviceManager
|
||||
---@param device DeviceInterface
|
||||
function DeviceManager:add(device) end
|
||||
|
||||
---@param cron string
|
||||
---@param callback fun()
|
||||
function DeviceManager:schedule(cron, callback) end
|
||||
|
||||
return DeviceManager
|
||||
337
definitions/automation:devices.lua
Normal file
337
definitions/automation:devices.lua
Normal file
@@ -0,0 +1,337 @@
|
||||
-- DO NOT MODIFY, FILE IS AUTOMATICALLY GENERATED
|
||||
---@meta
|
||||
|
||||
local devices
|
||||
|
||||
---@class KasaOutletConfig
|
||||
---@field identifier string
|
||||
---@field ip string
|
||||
local KasaOutletConfig
|
||||
|
||||
---@class KasaOutlet: DeviceInterface, OnOffInterface
|
||||
local KasaOutlet
|
||||
devices.KasaOutlet = {}
|
||||
---@param config KasaOutletConfig
|
||||
---@return KasaOutlet
|
||||
function devices.KasaOutlet.new(config) end
|
||||
|
||||
---@class AirFilter: DeviceInterface, OnOffInterface
|
||||
local AirFilter
|
||||
devices.AirFilter = {}
|
||||
---@param config AirFilterConfig
|
||||
---@return AirFilter
|
||||
function devices.AirFilter.new(config) end
|
||||
|
||||
---@class AirFilterConfig
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field url string
|
||||
local AirFilterConfig
|
||||
|
||||
---@class Presence: DeviceInterface
|
||||
local Presence
|
||||
devices.Presence = {}
|
||||
---@param config PresenceConfig
|
||||
---@return Presence
|
||||
function devices.Presence.new(config) end
|
||||
---@async
|
||||
---@return boolean
|
||||
function Presence:overall_presence() end
|
||||
|
||||
---@class PresenceConfig
|
||||
---@field topic string
|
||||
---@field callback fun(_: Presence, _: boolean) | fun(_: Presence, _: boolean)[]?
|
||||
---@field client AsyncClient
|
||||
local PresenceConfig
|
||||
|
||||
---@class HueBridge: DeviceInterface
|
||||
local HueBridge
|
||||
devices.HueBridge = {}
|
||||
---@param config HueBridgeConfig
|
||||
---@return HueBridge
|
||||
function devices.HueBridge.new(config) end
|
||||
---@async
|
||||
---@param flag Flag
|
||||
---@param value boolean
|
||||
function HueBridge:set_flag(flag, value) end
|
||||
|
||||
---@class FlagIDs
|
||||
---@field presence integer
|
||||
---@field darkness integer
|
||||
local FlagIDs
|
||||
|
||||
---@class HueBridgeConfig
|
||||
---@field identifier string
|
||||
---@field ip string
|
||||
---@field login string
|
||||
---@field flags FlagIDs
|
||||
local HueBridgeConfig
|
||||
|
||||
---@alias Flag
|
||||
---| "presence"
|
||||
---| "darkness"
|
||||
|
||||
---@class WasherConfig
|
||||
---@field identifier string
|
||||
---@field topic string
|
||||
---@field threshold number
|
||||
---@field done_callback fun(_: Washer) | fun(_: Washer)[]?
|
||||
---@field client AsyncClient
|
||||
local WasherConfig
|
||||
|
||||
---@class Washer: DeviceInterface
|
||||
local Washer
|
||||
devices.Washer = {}
|
||||
---@param config WasherConfig
|
||||
---@return Washer
|
||||
function devices.Washer.new(config) end
|
||||
|
||||
---@class LightSensor: DeviceInterface
|
||||
local LightSensor
|
||||
devices.LightSensor = {}
|
||||
---@param config LightSensorConfig
|
||||
---@return LightSensor
|
||||
function devices.LightSensor.new(config) end
|
||||
|
||||
---@class LightSensorConfig
|
||||
---@field identifier string
|
||||
---@field topic string
|
||||
---@field min integer
|
||||
---@field max integer
|
||||
---@field callback fun(_: LightSensor, _: boolean) | fun(_: LightSensor, _: boolean)[]?
|
||||
---@field client AsyncClient
|
||||
local LightSensorConfig
|
||||
|
||||
---@class HueGroupConfig
|
||||
---@field identifier string
|
||||
---@field ip string
|
||||
---@field login string
|
||||
---@field group_id integer
|
||||
---@field scene_id string
|
||||
local HueGroupConfig
|
||||
|
||||
---@class HueGroup: DeviceInterface, OnOffInterface
|
||||
local HueGroup
|
||||
devices.HueGroup = {}
|
||||
---@param config HueGroupConfig
|
||||
---@return HueGroup
|
||||
function devices.HueGroup.new(config) end
|
||||
|
||||
---@class IkeaRemote: DeviceInterface
|
||||
local IkeaRemote
|
||||
devices.IkeaRemote = {}
|
||||
---@param config IkeaRemoteConfig
|
||||
---@return IkeaRemote
|
||||
function devices.IkeaRemote.new(config) end
|
||||
|
||||
---@class IkeaRemoteConfig
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field single_button boolean?
|
||||
---@field topic string
|
||||
---@field client AsyncClient
|
||||
---@field callback fun(_: IkeaRemote, _: boolean) | fun(_: IkeaRemote, _: boolean)[]?
|
||||
---@field battery_callback fun(_: IkeaRemote, _: number) | fun(_: IkeaRemote, _: number)[]?
|
||||
local IkeaRemoteConfig
|
||||
|
||||
---@class WolConfig
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field topic string
|
||||
---@field mac_address string
|
||||
---@field broadcast_ip string?
|
||||
---@field client AsyncClient
|
||||
local WolConfig
|
||||
|
||||
---@class WakeOnLAN: DeviceInterface
|
||||
local WakeOnLAN
|
||||
devices.WakeOnLAN = {}
|
||||
---@param config WolConfig
|
||||
---@return WakeOnLAN
|
||||
function devices.WakeOnLAN.new(config) end
|
||||
|
||||
---@class ConfigOutletOutletStateOnOff
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field topic string
|
||||
---@field outlet_type OutletType?
|
||||
---@field callback fun(_: OutletOnOff, _: OutletStateOnOff) | fun(_: OutletOnOff, _: OutletStateOnOff)[]?
|
||||
---@field client AsyncClient
|
||||
local ConfigOutletOutletStateOnOff
|
||||
|
||||
---@class OutletStatePower
|
||||
---@field state boolean
|
||||
---@field power number
|
||||
local OutletStatePower
|
||||
|
||||
---@class ConfigOutletOutletStatePower
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field topic string
|
||||
---@field outlet_type OutletType?
|
||||
---@field callback fun(_: OutletPower, _: OutletStatePower) | fun(_: OutletPower, _: OutletStatePower)[]?
|
||||
---@field client AsyncClient
|
||||
local ConfigOutletOutletStatePower
|
||||
|
||||
---@alias OutletType
|
||||
---| "Outlet"
|
||||
---| "Kettle"
|
||||
|
||||
---@class OutletStateOnOff
|
||||
---@field state boolean
|
||||
local OutletStateOnOff
|
||||
|
||||
---@class OutletOnOff: DeviceInterface, OnOffInterface
|
||||
local OutletOnOff
|
||||
devices.OutletOnOff = {}
|
||||
---@param config ConfigOutletOutletStateOnOff
|
||||
---@return OutletOnOff
|
||||
function devices.OutletOnOff.new(config) end
|
||||
|
||||
---@class OutletPower: DeviceInterface, OnOffInterface
|
||||
local OutletPower
|
||||
devices.OutletPower = {}
|
||||
---@param config ConfigOutletOutletStatePower
|
||||
---@return OutletPower
|
||||
function devices.OutletPower.new(config) end
|
||||
|
||||
---@class ContactSensorConfig
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field topic string
|
||||
---@field sensor_type SensorType?
|
||||
---@field callback fun(_: ContactSensor, _: boolean) | fun(_: ContactSensor, _: boolean)[]?
|
||||
---@field battery_callback fun(_: ContactSensor, _: number) | fun(_: ContactSensor, _: number)[]?
|
||||
---@field client AsyncClient?
|
||||
local ContactSensorConfig
|
||||
|
||||
---@alias SensorType
|
||||
---| "Door"
|
||||
---| "Drawer"
|
||||
---| "Window"
|
||||
|
||||
---@class ContactSensor: DeviceInterface, OpenCloseInterface
|
||||
local ContactSensor
|
||||
devices.ContactSensor = {}
|
||||
---@param config ContactSensorConfig
|
||||
---@return ContactSensor
|
||||
function devices.ContactSensor.new(config) end
|
||||
|
||||
---@class LightStateOnOff
|
||||
---@field state boolean
|
||||
local LightStateOnOff
|
||||
|
||||
---@class ConfigLightLightStateColorTemperature
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field topic string
|
||||
---@field callback fun(_: LightColorTemperature, _: LightStateColorTemperature) | fun(_: LightColorTemperature, _: LightStateColorTemperature)[]?
|
||||
---@field client AsyncClient?
|
||||
local ConfigLightLightStateColorTemperature
|
||||
|
||||
---@class LightStateBrightness
|
||||
---@field state boolean
|
||||
---@field brightness number
|
||||
local LightStateBrightness
|
||||
|
||||
---@class LightStateColorTemperature
|
||||
---@field state boolean
|
||||
---@field brightness number
|
||||
---@field color_temp integer
|
||||
local LightStateColorTemperature
|
||||
|
||||
---@class LightBrightness: DeviceInterface, OnOffInterface, BrightnessInterface
|
||||
local LightBrightness
|
||||
devices.LightBrightness = {}
|
||||
---@param config ConfigLightLightStateBrightness
|
||||
---@return LightBrightness
|
||||
function devices.LightBrightness.new(config) end
|
||||
|
||||
---@class LightColorTemperature: DeviceInterface, OnOffInterface, BrightnessInterface, ColorSettingInterface
|
||||
local LightColorTemperature
|
||||
devices.LightColorTemperature = {}
|
||||
---@param config ConfigLightLightStateColorTemperature
|
||||
---@return LightColorTemperature
|
||||
function devices.LightColorTemperature.new(config) end
|
||||
|
||||
---@class ConfigLightLightStateOnOff
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field topic string
|
||||
---@field callback fun(_: LightOnOff, _: LightStateOnOff) | fun(_: LightOnOff, _: LightStateOnOff)[]?
|
||||
---@field client AsyncClient?
|
||||
local ConfigLightLightStateOnOff
|
||||
|
||||
---@class ConfigLightLightStateBrightness
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field topic string
|
||||
---@field callback fun(_: LightBrightness, _: LightStateBrightness) | fun(_: LightBrightness, _: LightStateBrightness)[]?
|
||||
---@field client AsyncClient?
|
||||
local ConfigLightLightStateBrightness
|
||||
|
||||
---@class LightOnOff: DeviceInterface, OnOffInterface
|
||||
local LightOnOff
|
||||
devices.LightOnOff = {}
|
||||
---@param config ConfigLightLightStateOnOff
|
||||
---@return LightOnOff
|
||||
function devices.LightOnOff.new(config) end
|
||||
|
||||
---@class Action
|
||||
---@field action
|
||||
---| "broadcast"
|
||||
---@field extras table<string, string>?
|
||||
---@field label string
|
||||
---@field clear boolean?
|
||||
local Action
|
||||
|
||||
---@class NtfyConfig
|
||||
---@field url string?
|
||||
---@field topic string
|
||||
local NtfyConfig
|
||||
|
||||
---@class Notification
|
||||
---@field title string
|
||||
---@field message string?
|
||||
---@field tags string[]?
|
||||
---@field priority Priority?
|
||||
---@field actions Action[]?
|
||||
local Notification
|
||||
|
||||
---@alias Priority
|
||||
---| "min"
|
||||
---| "low"
|
||||
---| "default"
|
||||
---| "high"
|
||||
---| "max"
|
||||
|
||||
---@class Ntfy: DeviceInterface
|
||||
local Ntfy
|
||||
devices.Ntfy = {}
|
||||
---@param config NtfyConfig
|
||||
---@return Ntfy
|
||||
function devices.Ntfy.new(config) end
|
||||
---@async
|
||||
---@param notification Notification
|
||||
function Ntfy:send_notification(notification) end
|
||||
|
||||
---@class HueSwitch: DeviceInterface
|
||||
local HueSwitch
|
||||
devices.HueSwitch = {}
|
||||
---@param config HueSwitchConfig
|
||||
---@return HueSwitch
|
||||
function devices.HueSwitch.new(config) end
|
||||
|
||||
---@class HueSwitchConfig
|
||||
---@field name string
|
||||
---@field room string?
|
||||
---@field topic string
|
||||
---@field client AsyncClient
|
||||
---@field left_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
||||
---@field right_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
||||
---@field left_hold_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
||||
---@field right_hold_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
||||
---@field battery_callback fun(_: HueSwitch, _: number) | fun(_: HueSwitch, _: number)[]?
|
||||
local HueSwitchConfig
|
||||
|
||||
return devices
|
||||
27
definitions/automation:mqtt.lua
Normal file
27
definitions/automation:mqtt.lua
Normal file
@@ -0,0 +1,27 @@
|
||||
-- DO NOT MODIFY, FILE IS AUTOMATICALLY GENERATED
|
||||
---@meta
|
||||
|
||||
local mqtt
|
||||
|
||||
---@class MqttConfig
|
||||
---@field host string
|
||||
---@field port integer
|
||||
---@field client_name string
|
||||
---@field username string
|
||||
---@field password string
|
||||
---@field tls boolean
|
||||
local MqttConfig
|
||||
|
||||
---@class AsyncClient
|
||||
local AsyncClient
|
||||
---@async
|
||||
---@param topic string
|
||||
---@param message table?
|
||||
function AsyncClient:send_message(topic, message) end
|
||||
mqtt.AsyncClient = {}
|
||||
---@param device_manager DeviceManager
|
||||
---@param config MqttConfig
|
||||
---@return AsyncClient
|
||||
function mqtt.new(device_manager, config) end
|
||||
|
||||
return mqtt
|
||||
6
definitions/automation:secrets.lua
Normal file
6
definitions/automation:secrets.lua
Normal file
@@ -0,0 +1,6 @@
|
||||
---@meta
|
||||
|
||||
---@type table<string, string?>
|
||||
local secrets
|
||||
|
||||
return secrets
|
||||
27
definitions/automation:utils.lua
Normal file
27
definitions/automation:utils.lua
Normal file
@@ -0,0 +1,27 @@
|
||||
-- DO NOT MODIFY, FILE IS AUTOMATICALLY GENERATED
|
||||
---@meta
|
||||
|
||||
local utils
|
||||
|
||||
---@class Timeout
|
||||
local Timeout
|
||||
---@async
|
||||
---@param timeout number
|
||||
---@param callback fun() | fun()[]
|
||||
function Timeout:start(timeout, callback) end
|
||||
---@async
|
||||
function Timeout:cancel() end
|
||||
---@async
|
||||
---@return boolean
|
||||
function Timeout:is_waiting() end
|
||||
utils.Timeout = {}
|
||||
---@return Timeout
|
||||
function utils.Timeout.new() end
|
||||
|
||||
---@return string
|
||||
function utils.get_hostname() end
|
||||
|
||||
---@return integer
|
||||
function utils.get_epoch() end
|
||||
|
||||
return utils
|
||||
6
definitions/automation:variables.lua
Normal file
6
definitions/automation:variables.lua
Normal file
@@ -0,0 +1,6 @@
|
||||
---@meta
|
||||
|
||||
---@type table<string, string?>
|
||||
local variables
|
||||
|
||||
return variables
|
||||
42
definitions/interfaces.lua
Normal file
42
definitions/interfaces.lua
Normal file
@@ -0,0 +1,42 @@
|
||||
--- @meta
|
||||
|
||||
---@class DeviceInterface
|
||||
local DeviceInterface
|
||||
---@return string
|
||||
function DeviceInterface:get_id() end
|
||||
|
||||
---@class OnOffInterface: DeviceInterface
|
||||
local OnOffInterface
|
||||
---@async
|
||||
---@param on boolean
|
||||
function OnOffInterface:set_on(on) end
|
||||
---@async
|
||||
---@return boolean
|
||||
function OnOffInterface:on() end
|
||||
|
||||
---@class BrightnessInterface: DeviceInterface
|
||||
local BrightnessInterface
|
||||
---@async
|
||||
---@param brightness integer
|
||||
function BrightnessInterface:set_brightness(brightness) end
|
||||
---@async
|
||||
---@return integer
|
||||
function BrightnessInterface:brightness() end
|
||||
|
||||
---@class ColorSettingInterface: DeviceInterface
|
||||
local ColorSettingInterface
|
||||
---@async
|
||||
---@param temperature integer
|
||||
function ColorSettingInterface:set_color_temperature(temperature) end
|
||||
---@async
|
||||
---@return integer
|
||||
function ColorSettingInterface:color_temperature() end
|
||||
|
||||
---@class OpenCloseInterface: DeviceInterface
|
||||
local OpenCloseInterface
|
||||
---@async
|
||||
---@param open_percent integer
|
||||
function OpenCloseInterface:set_open_percent(open_percent) end
|
||||
---@async
|
||||
---@return integer
|
||||
function OpenCloseInterface:open_percent() end
|
||||
32
src/bin/generate_definitions.rs
Normal file
32
src/bin/generate_definitions.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
|
||||
use automation_lib::Module;
|
||||
use tracing::{info, warn};
|
||||
|
||||
extern crate automation_devices;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let definitions_directory =
|
||||
std::path::Path::new(std::env!("CARGO_MANIFEST_DIR")).join("definitions");
|
||||
fs::create_dir_all(&definitions_directory)?;
|
||||
|
||||
for module in inventory::iter::<Module> {
|
||||
if let Some(definitions) = module.definitions() {
|
||||
info!(name = module.get_name(), "Generating definitions");
|
||||
|
||||
let filename = format!("{}.lua", module.get_name());
|
||||
let mut file = File::create(definitions_directory.join(filename))?;
|
||||
|
||||
file.write_all(b"-- DO NOT MODIFY, FILE IS AUTOMATICALLY GENERATED\n")?;
|
||||
file.write_all(definitions.as_bytes())?;
|
||||
file.write_all(b"\n")?;
|
||||
} else {
|
||||
warn!(name = module.get_name(), "No definitions");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
22
src/main.rs
22
src/main.rs
@@ -9,18 +9,15 @@ use std::path::Path;
|
||||
use std::process;
|
||||
|
||||
use ::config::{Environment, File};
|
||||
use automation_lib::config::{FulfillmentConfig, MqttConfig};
|
||||
use automation_lib::config::FulfillmentConfig;
|
||||
use automation_lib::device_manager::DeviceManager;
|
||||
use automation_lib::mqtt::{self, WrappedAsyncClient};
|
||||
use axum::extract::{FromRef, State};
|
||||
use axum::http::StatusCode;
|
||||
use axum::routing::post;
|
||||
use axum::{Json, Router};
|
||||
use config::Config;
|
||||
use dotenvy::dotenv;
|
||||
use google_home::{GoogleHome, Request, Response};
|
||||
use mlua::LuaSerdeExt;
|
||||
use rumqttc::AsyncClient;
|
||||
use tokio::net::TcpListener;
|
||||
use tracing::{debug, error, info, warn};
|
||||
use web::{ApiError, User};
|
||||
@@ -75,8 +72,6 @@ async fn fulfillment(
|
||||
}
|
||||
|
||||
async fn app() -> anyhow::Result<()> {
|
||||
dotenv().ok();
|
||||
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
info!(version = VERSION, "automation_rs");
|
||||
@@ -141,21 +136,6 @@ async fn app() -> anyhow::Result<()> {
|
||||
|
||||
automation_lib::load_modules(&lua)?;
|
||||
|
||||
let mqtt = lua.create_table()?;
|
||||
let event_channel = device_manager.event_channel();
|
||||
let mqtt_new = lua.create_function(move |lua, config: mlua::Value| {
|
||||
let config: MqttConfig = lua.from_value(config)?;
|
||||
|
||||
// Create a mqtt client
|
||||
// TODO: When starting up, the devices are not yet created, this could lead to a device being out of sync
|
||||
let (client, eventloop) = AsyncClient::new(config.into(), 100);
|
||||
mqtt::start(eventloop, &event_channel);
|
||||
|
||||
Ok(WrappedAsyncClient(client))
|
||||
})?;
|
||||
mqtt.set("new", mqtt_new)?;
|
||||
lua.register_module("automation:mqtt", mqtt)?;
|
||||
|
||||
lua.register_module("automation:device_manager", device_manager.clone())?;
|
||||
|
||||
lua.register_module("automation:variables", lua.to_value(&config.variables)?)?;
|
||||
|
||||
Reference in New Issue
Block a user