Compare commits
1 Commits
master
..
6c7a774d7e
| Author | SHA1 | Date | |
|---|---|---|---|
|
6c7a774d7e
|
@@ -9,10 +9,10 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
uses: dreaded_x/workflows/.gitea/workflows/docker-kubernetes.yaml@ef78704b98c72e4a6b8340f9bff7b085a7bdd95c
|
uses: dreaded_x/workflows/.gitea/workflows/rust-kubernetes.yaml@22ee0c1788a8d2157db87d6a6f8dbe520fe48592
|
||||||
secrets: inherit
|
secrets: inherit
|
||||||
with:
|
with:
|
||||||
push_manifests: false
|
upload_manifests: false
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
name: Deploy container
|
name: Deploy container
|
||||||
@@ -26,10 +26,6 @@ jobs:
|
|||||||
docker stop automation_rs || true
|
docker stop automation_rs || true
|
||||||
docker rm automation_rs || true
|
docker rm automation_rs || true
|
||||||
|
|
||||||
- name: Login to registry
|
|
||||||
run: |
|
|
||||||
docker login git.huizinga.dev -u ${{ gitea.actor }} -p ${{ secrets.REGISTRY_TOKEN }} \
|
|
||||||
|
|
||||||
- name: Create container
|
- name: Create container
|
||||||
run: |
|
run: |
|
||||||
docker create \
|
docker create \
|
||||||
@@ -41,7 +37,7 @@ jobs:
|
|||||||
-e AUTOMATION__SECRETS__MQTT_PASSWORD=${{ secrets.MQTT_PASSWORD }} \
|
-e AUTOMATION__SECRETS__MQTT_PASSWORD=${{ secrets.MQTT_PASSWORD }} \
|
||||||
-e AUTOMATION__SECRETS__HUE_TOKEN=${{ secrets.HUE_TOKEN }} \
|
-e AUTOMATION__SECRETS__HUE_TOKEN=${{ secrets.HUE_TOKEN }} \
|
||||||
-e AUTOMATION__SECRETS__NTFY_TOPIC=${{ secrets.NTFY_TOPIC }} \
|
-e AUTOMATION__SECRETS__NTFY_TOPIC=${{ secrets.NTFY_TOPIC }} \
|
||||||
$(echo ${{ toJSON(needs.build.outputs.images) }} | jq .automation -r)
|
git.huizinga.dev/dreaded_x/automation_rs@${{ needs.build.outputs.digest }}
|
||||||
|
|
||||||
docker network connect web automation_rs
|
docker network connect web automation_rs
|
||||||
|
|
||||||
|
|||||||
Generated
+575
-908
File diff suppressed because it is too large
Load Diff
+21
-21
@@ -16,28 +16,28 @@ members = [
|
|||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
air_filter_types = { git = "https://git.huizinga.dev/Dreaded_X/airfilter", tag = "v0.4.4" }
|
air_filter_types = { git = "https://git.huizinga.dev/Dreaded_X/airfilter", tag = "v0.4.4" }
|
||||||
anyhow = "1.0.102"
|
anyhow = "1.0.99"
|
||||||
async-trait = "0.1.89"
|
async-trait = "0.1.89"
|
||||||
automation_cast = { path = "./automation_cast" }
|
automation_cast = { path = "./automation_cast" }
|
||||||
automation_devices = { path = "./automation_devices" }
|
automation_devices = { path = "./automation_devices" }
|
||||||
automation_lib = { path = "./automation_lib" }
|
automation_lib = { path = "./automation_lib" }
|
||||||
automation_macro = { path = "./automation_macro" }
|
automation_macro = { path = "./automation_macro" }
|
||||||
axum = "0.8.9"
|
axum = "0.8.4"
|
||||||
bytes = "1.11.1"
|
bytes = "1.10.1"
|
||||||
dyn-clone = "1.0.20"
|
dyn-clone = "1.0.20"
|
||||||
eui48 = { version = "1.1.0", features = [
|
eui48 = { version = "1.1.0", features = [
|
||||||
"disp_hexstring",
|
"disp_hexstring",
|
||||||
"serde",
|
"serde",
|
||||||
], default-features = false }
|
], default-features = false }
|
||||||
futures = "0.3.32"
|
futures = "0.3.31"
|
||||||
google_home = { path = "./google_home/google_home" }
|
google_home = { path = "./google_home/google_home" }
|
||||||
google_home_macro = { path = "./google_home/google_home_macro" }
|
google_home_macro = { path = "./google_home/google_home_macro" }
|
||||||
hostname = "0.4.2"
|
hostname = "0.4.1"
|
||||||
inventory = "0.3.24"
|
inventory = "0.3.21"
|
||||||
itertools = "0.14.0"
|
itertools = "0.14.0"
|
||||||
json_value_merge = "2.0.1"
|
json_value_merge = "2.0.1"
|
||||||
lua_typed = { git = "https://git.huizinga.dev/Dreaded_X/lua_typed" }
|
lua_typed = { git = "https://git.huizinga.dev/Dreaded_X/lua_typed" }
|
||||||
mlua = { version = "0.11.6", features = [
|
mlua = { version = "0.11.3", features = [
|
||||||
"lua54",
|
"lua54",
|
||||||
"vendored",
|
"vendored",
|
||||||
"macros",
|
"macros",
|
||||||
@@ -45,23 +45,23 @@ mlua = { version = "0.11.6", features = [
|
|||||||
"async",
|
"async",
|
||||||
"send",
|
"send",
|
||||||
] }
|
] }
|
||||||
proc-macro2 = "1.0.106"
|
proc-macro2 = "1.0.101"
|
||||||
quote = "1.0.45"
|
quote = "1.0.40"
|
||||||
reqwest = { version = "0.13.3", features = [
|
reqwest = { version = "0.12.23", features = [
|
||||||
"json",
|
"json",
|
||||||
"rustls",
|
"rustls-tls",
|
||||||
], default-features = false } # Use rustls, since the other packages also use rustls
|
], default-features = false } # Use rustls, since the other packages also use rustls
|
||||||
rumqttc = "0.25.1"
|
rumqttc = "0.24.0"
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
serde_json = "1.0.149"
|
serde_json = "1.0.143"
|
||||||
serde_repr = "0.1.20"
|
serde_repr = "0.1.20"
|
||||||
syn = { version = "2.0.117" }
|
syn = { version = "2.0.106" }
|
||||||
thiserror = "2.0.18"
|
thiserror = "2.0.16"
|
||||||
tokio = { version = "1", features = ["rt-multi-thread"] }
|
tokio = { version = "1", features = ["rt-multi-thread"] }
|
||||||
tokio-cron-scheduler = "0.15.1"
|
tokio-cron-scheduler = "0.15.0"
|
||||||
tracing = "0.1.44"
|
tracing = "0.1.41"
|
||||||
tracing-subscriber = "0.3.23"
|
tracing-subscriber = "0.3.20"
|
||||||
wakey = "0.4.1"
|
wakey = "0.3.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
@@ -70,7 +70,7 @@ automation_devices = { workspace = true }
|
|||||||
automation_lib = { workspace = true }
|
automation_lib = { workspace = true }
|
||||||
automation_macro = { path = "./automation_macro" }
|
automation_macro = { path = "./automation_macro" }
|
||||||
axum = { workspace = true }
|
axum = { workspace = true }
|
||||||
config = { version = "0.15.22", default-features = false, features = [
|
config = { version = "0.15.15", default-features = false, features = [
|
||||||
"async",
|
"async",
|
||||||
"toml",
|
"toml",
|
||||||
] }
|
] }
|
||||||
|
|||||||
+2
-5
@@ -1,9 +1,8 @@
|
|||||||
FROM rust:1.95 AS base
|
FROM rust:1.89 AS base
|
||||||
ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
|
ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
|
||||||
RUN cargo install cargo-chef --locked --version 0.1.71 && \
|
RUN cargo install cargo-chef --locked --version 0.1.71 && \
|
||||||
cargo install cargo-auditable --locked --version 0.6.6
|
cargo install cargo-auditable --locked --version 0.6.6
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
RUN rustup toolchain install
|
|
||||||
|
|
||||||
FROM base AS planner
|
FROM base AS planner
|
||||||
COPY . .
|
COPY . .
|
||||||
@@ -20,9 +19,7 @@ ARG RELEASE_VERSION
|
|||||||
ENV RELEASE_VERSION=${RELEASE_VERSION}
|
ENV RELEASE_VERSION=${RELEASE_VERSION}
|
||||||
RUN cargo auditable build --release
|
RUN cargo auditable build --release
|
||||||
|
|
||||||
FROM gcr.io/distroless/cc-debian13:nonroot AS runtime
|
FROM gcr.io/distroless/cc-debian12:nonroot AS runtime
|
||||||
COPY --from=builder /app/target/release/automation /app/automation
|
COPY --from=builder /app/target/release/automation /app/automation
|
||||||
ENV AUTOMATION__ENTRYPOINT=/app/config/config.lua
|
|
||||||
ENV LUA_PATH="/app/?.lua;;"
|
|
||||||
COPY ./config /app/config
|
COPY ./config /app/config
|
||||||
CMD [ "/app/automation" ]
|
CMD [ "/app/automation" ]
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ Custom home automation solution with Google Home integration and lua scripting.
|
|||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
This repository uses [prek](https://prek.j178.dev/) to make sure everything is ready to go when committing.
|
This repository uses [pre-commit](https://pre-commit.com) to make sure everything is ready to go when committing.
|
||||||
Install the pre-commit hooks by running the following command:
|
Install the pre-commit hooks by running the following command:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
prek install
|
pre-commit install
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ use std::net::SocketAddr;
|
|||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use automation_lib::lua::traits::PartialUserData;
|
|
||||||
use automation_macro::{Device, LuaDeviceConfig};
|
use automation_macro::{Device, LuaDeviceConfig};
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
use google_home::traits::OnOff;
|
use google_home::traits::OnOff;
|
||||||
@@ -26,7 +25,6 @@ crate::register_type!(Config);
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Device)]
|
#[derive(Debug, Clone, Device)]
|
||||||
#[device(traits(OnOff))]
|
#[device(traits(OnOff))]
|
||||||
#[device(extra_user_data = AllOn)]
|
|
||||||
pub struct HueGroup {
|
pub struct HueGroup {
|
||||||
config: Config,
|
config: Config,
|
||||||
}
|
}
|
||||||
@@ -124,47 +122,6 @@ impl OnOff for HueGroup {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AllOn;
|
|
||||||
impl PartialUserData<HueGroup> for AllOn {
|
|
||||||
fn add_methods<M: mlua::UserDataMethods<HueGroup>>(methods: &mut M) {
|
|
||||||
methods.add_async_method("all_on", async |_lua, this, ()| {
|
|
||||||
let res = reqwest::Client::new()
|
|
||||||
.get(this.url_get_state())
|
|
||||||
.send()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
match res {
|
|
||||||
Ok(res) => {
|
|
||||||
let status = res.status();
|
|
||||||
if !status.is_success() {
|
|
||||||
warn!(id = this.get_id(), "Status code is not success: {status}");
|
|
||||||
}
|
|
||||||
|
|
||||||
let on = match res.json::<message::Info>().await {
|
|
||||||
Ok(info) => info.all_on(),
|
|
||||||
Err(err) => {
|
|
||||||
error!(id = this.get_id(), "Failed to parse message: {err}");
|
|
||||||
return Ok(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(on);
|
|
||||||
}
|
|
||||||
Err(err) => error!(id = this.get_id(), "Error: {err}"),
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(false)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn definitions() -> Option<String> {
|
|
||||||
Some(format!(
|
|
||||||
"---@async\n---@return boolean\nfunction {}:all_on() end\n",
|
|
||||||
<HueGroup as Typed>::type_name(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod message {
|
mod message {
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -207,9 +164,5 @@ mod message {
|
|||||||
pub fn any_on(&self) -> bool {
|
pub fn any_on(&self) -> bool {
|
||||||
self.state.any_on
|
self.state.any_on
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn all_on(&self) -> bool {
|
|
||||||
self.state.all_on
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,11 +122,15 @@ impl OnMqtt for HueSwitch {
|
|||||||
Action::LeftHold => self.config.left_hold_callback.call(self.clone()).await,
|
Action::LeftHold => self.config.left_hold_callback.call(self.clone()).await,
|
||||||
Action::RightHold => self.config.right_hold_callback.call(self.clone()).await,
|
Action::RightHold => self.config.right_hold_callback.call(self.clone()).await,
|
||||||
// If there is no hold action, the switch will act like a normal release
|
// If there is no hold action, the switch will act like a normal release
|
||||||
Action::RightHoldRelease if self.config.right_hold_callback.is_empty() => {
|
Action::RightHoldRelease => {
|
||||||
self.config.right_callback.call(self.clone()).await
|
if self.config.right_hold_callback.is_empty() {
|
||||||
|
self.config.right_callback.call(self.clone()).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Action::LeftHoldRelease if self.config.left_hold_callback.is_empty() => {
|
Action::LeftHoldRelease => {
|
||||||
self.config.left_callback.call(self.clone()).await
|
if self.config.left_hold_callback.is_empty() {
|
||||||
|
self.config.left_callback.call(self.clone()).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#![feature(iter_intersperse)]
|
||||||
#![feature(iterator_try_collect)]
|
#![feature(iterator_try_collect)]
|
||||||
mod device;
|
mod device;
|
||||||
mod lua_device_config;
|
mod lua_device_config;
|
||||||
|
|||||||
+1
-7
@@ -1,6 +1,5 @@
|
|||||||
local ntfy = require("config.ntfy")
|
local ntfy = require("config.ntfy")
|
||||||
|
|
||||||
--- @class BatteryModule: Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
--- @type {[string]: number}
|
--- @type {[string]: number}
|
||||||
@@ -18,7 +17,7 @@ function module.callback(device, battery)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function notify_low_battery()
|
function module.notify_low_battery()
|
||||||
-- Don't send notifications if there are now devices with low battery
|
-- Don't send notifications if there are now devices with low battery
|
||||||
if next(low_battery) == nil then
|
if next(low_battery) == nil then
|
||||||
print("No devices with low battery")
|
print("No devices with low battery")
|
||||||
@@ -39,9 +38,4 @@ local function notify_low_battery()
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
--- @type Schedule
|
|
||||||
module.schedule = {
|
|
||||||
["0 0 21 */1 * *"] = notify_low_battery,
|
|
||||||
}
|
|
||||||
|
|
||||||
return module
|
return module
|
||||||
|
|||||||
@@ -29,4 +29,14 @@ return {
|
|||||||
require("config.rooms"),
|
require("config.rooms"),
|
||||||
require("config.windows"),
|
require("config.windows"),
|
||||||
},
|
},
|
||||||
|
-- TODO: Make this also part of the modules
|
||||||
|
schedule = {
|
||||||
|
["0 0 19 * * *"] = function()
|
||||||
|
require("config.rooms.bedroom").set_airfilter_on(true)
|
||||||
|
end,
|
||||||
|
["0 0 20 * * *"] = function()
|
||||||
|
require("config.rooms.bedroom").set_airfilter_on(false)
|
||||||
|
end,
|
||||||
|
["0 0 21 */1 * *"] = require("config.battery").notify_low_battery,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ local presence = require("config.presence")
|
|||||||
local utils = require("automation:utils")
|
local utils = require("automation:utils")
|
||||||
local variables = require("automation:variables")
|
local variables = require("automation:variables")
|
||||||
|
|
||||||
--- @class DebugModule: Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
if variables.debug == "true" then
|
if variables.debug == "true" then
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
local debug = require("config.debug")
|
local debug = require("config.debug")
|
||||||
local utils = require("automation:utils")
|
local utils = require("automation:utils")
|
||||||
|
|
||||||
--- @class HallwayAutomationModule: Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
local timeout = utils.Timeout.new()
|
local timeout = utils.Timeout.new()
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
local utils = require("automation:utils")
|
local utils = require("automation:utils")
|
||||||
|
|
||||||
--- @class HelperModule: Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
--- @param topic string
|
--- @param topic string
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ local light = require("config.light")
|
|||||||
local presence = require("config.presence")
|
local presence = require("config.presence")
|
||||||
local secrets = require("automation:secrets")
|
local secrets = require("automation:secrets")
|
||||||
|
|
||||||
--- @class HueBridgeModule: Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
module.ip = "10.0.0.102"
|
module.ip = "10.0.0.102"
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
local devices = require("automation:devices")
|
local devices = require("automation:devices")
|
||||||
local helper = require("config.helper")
|
local helper = require("config.helper")
|
||||||
|
|
||||||
--- @class LightModule: Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
--- @class OnPresence
|
--- @class OnPresence
|
||||||
@@ -35,7 +34,6 @@ function module.setup(mqtt_client)
|
|||||||
callback = callback,
|
callback = callback,
|
||||||
})
|
})
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
module.device,
|
module.device,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
local devices = require("automation:devices")
|
local devices = require("automation:devices")
|
||||||
local secrets = require("automation:secrets")
|
local secrets = require("automation:secrets")
|
||||||
|
|
||||||
--- @class NtfyModule: Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
local ntfy_topic = secrets.ntfy_topic
|
local ntfy_topic = secrets.ntfy_topic
|
||||||
@@ -25,7 +24,6 @@ function module.setup()
|
|||||||
topic = ntfy_topic,
|
topic = ntfy_topic,
|
||||||
})
|
})
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
ntfy,
|
ntfy,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ local devices = require("automation:devices")
|
|||||||
local helper = require("config.helper")
|
local helper = require("config.helper")
|
||||||
local ntfy = require("config.ntfy")
|
local ntfy = require("config.ntfy")
|
||||||
|
|
||||||
--- @class PresenceModule: Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
--- @class OnPresence
|
--- @class OnPresence
|
||||||
@@ -62,7 +61,6 @@ function module.setup(mqtt_client)
|
|||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
presence,
|
presence,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ local devices = require("automation:devices")
|
|||||||
local helper = require("config.helper")
|
local helper = require("config.helper")
|
||||||
local ntfy = require("config.ntfy")
|
local ntfy = require("config.ntfy")
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
|
--- @type SetupFunction
|
||||||
function module.setup(mqtt_client)
|
function module.setup(mqtt_client)
|
||||||
local light = devices.LightOnOff.new({
|
local light = devices.LightOnOff.new({
|
||||||
name = "Light",
|
name = "Light",
|
||||||
@@ -30,7 +30,6 @@ function module.setup(mqtt_client)
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
light,
|
light,
|
||||||
washer,
|
washer,
|
||||||
|
|||||||
+14
-49
@@ -4,12 +4,12 @@ local helper = require("config.helper")
|
|||||||
local hue_bridge = require("config.hue_bridge")
|
local hue_bridge = require("config.hue_bridge")
|
||||||
local windows = require("config.windows")
|
local windows = require("config.windows")
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
--- @type AirFilter?
|
--- @type AirFilter?
|
||||||
local air_filter = nil
|
local air_filter = nil
|
||||||
|
|
||||||
|
--- @type SetupFunction
|
||||||
function module.setup(mqtt_client)
|
function module.setup(mqtt_client)
|
||||||
local lights = devices.HueGroup.new({
|
local lights = devices.HueGroup.new({
|
||||||
identifier = "bedroom_lights",
|
identifier = "bedroom_lights",
|
||||||
@@ -25,13 +25,6 @@ function module.setup(mqtt_client)
|
|||||||
group_id = 3,
|
group_id = 3,
|
||||||
scene_id = "60tfTyR168v2csz",
|
scene_id = "60tfTyR168v2csz",
|
||||||
})
|
})
|
||||||
local wardrobe_light = devices.HueGroup.new({
|
|
||||||
identifier = "bedroom_lights_wardrobe",
|
|
||||||
ip = hue_bridge.ip,
|
|
||||||
login = hue_bridge.token,
|
|
||||||
group_id = 3,
|
|
||||||
scene_id = "1IDvpsN2YLZsDV95",
|
|
||||||
})
|
|
||||||
|
|
||||||
air_filter = devices.AirFilter.new({
|
air_filter = devices.AirFilter.new({
|
||||||
name = "Air Filter",
|
name = "Air Filter",
|
||||||
@@ -39,36 +32,13 @@ function module.setup(mqtt_client)
|
|||||||
url = "http://10.0.0.103",
|
url = "http://10.0.0.103",
|
||||||
})
|
})
|
||||||
|
|
||||||
local wardrobe_door = devices.ContactSensor.new({
|
|
||||||
name = "Wardrobe Door",
|
|
||||||
room = "Bedroom",
|
|
||||||
sensor_type = "Door",
|
|
||||||
topic = helper.mqtt_z2m("bedroom/wardrobe_door"),
|
|
||||||
client = mqtt_client,
|
|
||||||
callback = function(_, open)
|
|
||||||
-- Technically this has an edge case where if one of the spots is
|
|
||||||
-- on, but that is not something I ever do
|
|
||||||
if not lights:all_on() then
|
|
||||||
wardrobe_light:set_on(open)
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
battery_callback = battery.callback,
|
|
||||||
})
|
|
||||||
|
|
||||||
local switch = devices.HueSwitch.new({
|
local switch = devices.HueSwitch.new({
|
||||||
name = "Switch",
|
name = "Switch",
|
||||||
room = "Bedroom",
|
room = "Bedroom",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
topic = helper.mqtt_z2m("bedroom/switch"),
|
topic = helper.mqtt_z2m("bedroom/switch"),
|
||||||
left_callback = function()
|
left_callback = function()
|
||||||
local on = not lights:all_on()
|
lights:set_on(not lights:on())
|
||||||
lights:set_on(on)
|
|
||||||
-- This is a bit janky as the light will start to dim before turning
|
|
||||||
-- back on, however this is really and edge case that probably won't
|
|
||||||
-- happen often, so for now it's fine
|
|
||||||
if not on and wardrobe_door:open_percent() == 100 then
|
|
||||||
wardrobe_light:set_on(true)
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
left_hold_callback = function()
|
left_hold_callback = function()
|
||||||
lights_relax:set_on(true)
|
lights_relax:set_on(true)
|
||||||
@@ -85,25 +55,20 @@ function module.setup(mqtt_client)
|
|||||||
})
|
})
|
||||||
windows.add(window)
|
windows.add(window)
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
devices = {
|
lights,
|
||||||
lights,
|
lights_relax,
|
||||||
lights_relax,
|
air_filter,
|
||||||
air_filter,
|
switch,
|
||||||
wardrobe_door,
|
window,
|
||||||
switch,
|
|
||||||
window,
|
|
||||||
},
|
|
||||||
schedule = {
|
|
||||||
["0 0 19 * * *"] = function()
|
|
||||||
air_filter:set_on(true)
|
|
||||||
end,
|
|
||||||
["0 0 20 * * *"] = function()
|
|
||||||
air_filter:set_on(false)
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- @param on boolean
|
||||||
|
function module.set_airfilter_on(on)
|
||||||
|
if air_filter then
|
||||||
|
air_filter:set_on(on)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return module
|
return module
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ local helper = require("config.helper")
|
|||||||
local presence = require("config.presence")
|
local presence = require("config.presence")
|
||||||
local windows = require("config.windows")
|
local windows = require("config.windows")
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
|
--- @type SetupFunction
|
||||||
function module.setup(mqtt_client)
|
function module.setup(mqtt_client)
|
||||||
local light = devices.LightOnOff.new({
|
local light = devices.LightOnOff.new({
|
||||||
name = "Light",
|
name = "Light",
|
||||||
@@ -25,18 +25,9 @@ function module.setup(mqtt_client)
|
|||||||
})
|
})
|
||||||
windows.add(window)
|
windows.add(window)
|
||||||
|
|
||||||
local printer = devices.OutletOnOff.new({
|
|
||||||
name = "3D Printer",
|
|
||||||
room = "Guest Room",
|
|
||||||
topic = helper.mqtt_z2m("guest/printer"),
|
|
||||||
client = mqtt_client,
|
|
||||||
})
|
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
light,
|
light,
|
||||||
window,
|
window,
|
||||||
printer,
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ local presence = require("config.presence")
|
|||||||
local utils = require("automation:utils")
|
local utils = require("automation:utils")
|
||||||
local windows = require("config.windows")
|
local windows = require("config.windows")
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
|
--- @type SetupFunction
|
||||||
function module.setup(mqtt_client)
|
function module.setup(mqtt_client)
|
||||||
local main_light = devices.HueGroup.new({
|
local main_light = devices.HueGroup.new({
|
||||||
identifier = "hallway_main_light",
|
identifier = "hallway_main_light",
|
||||||
@@ -59,21 +59,26 @@ function module.setup(mqtt_client)
|
|||||||
})
|
})
|
||||||
hallway_automation.set_trash(trash)
|
hallway_automation.set_trash(trash)
|
||||||
|
|
||||||
local timeout = utils.Timeout.new()
|
---@param duration number
|
||||||
local function frontdoor_presence(_, open)
|
---@return fun(_, open: boolean)
|
||||||
if open then
|
local function frontdoor_presence(duration)
|
||||||
timeout:cancel()
|
local timeout = utils.Timeout.new()
|
||||||
|
|
||||||
if not presence.overall_presence() then
|
return function(_, open)
|
||||||
mqtt_client:send_message(helper.mqtt_automation("presence/contact/frontdoor"), {
|
if open then
|
||||||
state = true,
|
timeout:cancel()
|
||||||
updated = utils.get_epoch(),
|
|
||||||
})
|
if presence.overall_presence() then
|
||||||
|
mqtt_client:send_message(helper.mqtt_automation("presence/contact/frontdoor"), {
|
||||||
|
state = true,
|
||||||
|
updated = utils.get_epoch(),
|
||||||
|
})
|
||||||
|
end
|
||||||
|
else
|
||||||
|
timeout:start(duration, function()
|
||||||
|
mqtt_client:send_message(helper.mqtt_automation("presence/contact/frontdoor"), nil)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
else
|
|
||||||
timeout:start(debug.debug_mode and 10 or 15 * 60, function()
|
|
||||||
mqtt_client:send_message(helper.mqtt_automation("presence/contact/frontdoor"), nil)
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -84,7 +89,7 @@ function module.setup(mqtt_client)
|
|||||||
topic = helper.mqtt_z2m("hallway/frontdoor"),
|
topic = helper.mqtt_z2m("hallway/frontdoor"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
callback = {
|
callback = {
|
||||||
frontdoor_presence,
|
frontdoor_presence(debug.debug_mode and 10 or 15 * 60),
|
||||||
hallway_automation.door_callback,
|
hallway_automation.door_callback,
|
||||||
},
|
},
|
||||||
battery_callback = battery.callback,
|
battery_callback = battery.callback,
|
||||||
@@ -92,7 +97,6 @@ function module.setup(mqtt_client)
|
|||||||
windows.add(frontdoor)
|
windows.add(frontdoor)
|
||||||
hallway_automation.set_door(frontdoor)
|
hallway_automation.set_door(frontdoor)
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
main_light,
|
main_light,
|
||||||
storage_light,
|
storage_light,
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ local devices = require("automation:devices")
|
|||||||
local helper = require("config.helper")
|
local helper = require("config.helper")
|
||||||
local hue_bridge = require("config.hue_bridge")
|
local hue_bridge = require("config.hue_bridge")
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
|
--- @type SetupFunction
|
||||||
function module.setup(mqtt_client)
|
function module.setup(mqtt_client)
|
||||||
local light = devices.HueGroup.new({
|
local light = devices.HueGroup.new({
|
||||||
identifier = "hallway_top_light",
|
identifier = "hallway_top_light",
|
||||||
@@ -37,7 +37,6 @@ function module.setup(mqtt_client)
|
|||||||
battery_callback = battery.callback,
|
battery_callback = battery.callback,
|
||||||
})
|
})
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
light,
|
light,
|
||||||
top_switch,
|
top_switch,
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ local helper = require("config.helper")
|
|||||||
local hue_bridge = require("config.hue_bridge")
|
local hue_bridge = require("config.hue_bridge")
|
||||||
local presence = require("config.presence")
|
local presence = require("config.presence")
|
||||||
|
|
||||||
--- @class KitchenModule: Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
--- @type HueGroup?
|
--- @type HueGroup?
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ local hue_bridge = require("config.hue_bridge")
|
|||||||
local presence = require("config.presence")
|
local presence = require("config.presence")
|
||||||
local windows = require("config.windows")
|
local windows = require("config.windows")
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
|
--- @type SetupFunction
|
||||||
function module.setup(mqtt_client)
|
function module.setup(mqtt_client)
|
||||||
local lights = devices.HueGroup.new({
|
local lights = devices.HueGroup.new({
|
||||||
identifier = "living_lights",
|
identifier = "living_lights",
|
||||||
@@ -109,7 +109,6 @@ function module.setup(mqtt_client)
|
|||||||
})
|
})
|
||||||
windows.add(window)
|
windows.add(window)
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
lights,
|
lights,
|
||||||
lights_relax,
|
lights_relax,
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ local devices = require("automation:devices")
|
|||||||
local helper = require("config.helper")
|
local helper = require("config.helper")
|
||||||
local presence = require("config.presence")
|
local presence = require("config.presence")
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
|
--- @type SetupFunction
|
||||||
function module.setup(mqtt_client)
|
function module.setup(mqtt_client)
|
||||||
local light = devices.LightBrightness.new({
|
local light = devices.LightBrightness.new({
|
||||||
name = "Light",
|
name = "Light",
|
||||||
@@ -31,7 +31,6 @@ function module.setup(mqtt_client)
|
|||||||
battery_callback = battery.callback,
|
battery_callback = battery.callback,
|
||||||
})
|
})
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
light,
|
light,
|
||||||
door,
|
door,
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ local helper = require("config.helper")
|
|||||||
local presence = require("config.presence")
|
local presence = require("config.presence")
|
||||||
local utils = require("automation:utils")
|
local utils = require("automation:utils")
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
|
--- @type SetupFunction
|
||||||
function module.setup(mqtt_client)
|
function module.setup(mqtt_client)
|
||||||
local charger = devices.OutletOnOff.new({
|
local charger = devices.OutletOnOff.new({
|
||||||
name = "Charger",
|
name = "Charger",
|
||||||
@@ -57,7 +57,6 @@ function module.setup(mqtt_client)
|
|||||||
battery_callback = battery.callback,
|
battery_callback = battery.callback,
|
||||||
})
|
})
|
||||||
|
|
||||||
--- @type Module
|
|
||||||
return {
|
return {
|
||||||
charger,
|
charger,
|
||||||
outlets,
|
outlets,
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
local ntfy = require("config.ntfy")
|
local ntfy = require("config.ntfy")
|
||||||
local presence = require("config.presence")
|
local presence = require("config.presence")
|
||||||
|
|
||||||
--- @class WindowsModule: Module
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
--- @class OnPresence
|
--- @class OnPresence
|
||||||
|
|||||||
@@ -116,9 +116,6 @@ devices.HueGroup = {}
|
|||||||
---@param config HueGroupConfig
|
---@param config HueGroupConfig
|
||||||
---@return HueGroup
|
---@return HueGroup
|
||||||
function devices.HueGroup.new(config) end
|
function devices.HueGroup.new(config) end
|
||||||
---@async
|
|
||||||
---@return boolean
|
|
||||||
function HueGroup:all_on() end
|
|
||||||
|
|
||||||
---@class HueGroupConfig
|
---@class HueGroupConfig
|
||||||
---@field identifier string
|
---@field identifier string
|
||||||
|
|||||||
@@ -11,17 +11,13 @@ local FulfillmentConfig
|
|||||||
---@field fulfillment FulfillmentConfig
|
---@field fulfillment FulfillmentConfig
|
||||||
---@field modules (Module)[]
|
---@field modules (Module)[]
|
||||||
---@field mqtt MqttConfig
|
---@field mqtt MqttConfig
|
||||||
|
---@field schedule (table<string, fun() | fun()[]>)?
|
||||||
local Config
|
local Config
|
||||||
|
|
||||||
---@alias SetupFunction fun(mqtt_client: AsyncClient): Module | DeviceInterface[] | nil
|
|
||||||
|
|
||||||
---@alias Schedule table<string, fun() | fun()[]>
|
|
||||||
|
|
||||||
---@class Module
|
---@class Module
|
||||||
---@field setup (SetupFunction)?
|
---@field setup (fun(mqtt_client: AsyncClient): Module | DeviceInterface[] | nil)?
|
||||||
---@field devices (DeviceInterface)[]?
|
---@field devices DeviceInterface[]?
|
||||||
---@field schedule Schedule?
|
---@field [number] Module[]?
|
||||||
---@field [number] (Module)[]?
|
|
||||||
local Module
|
local Module
|
||||||
|
|
||||||
---@class MqttConfig
|
---@class MqttConfig
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
variable "TAG_BASE" {}
|
|
||||||
variable "RELEASE_VERSION" {}
|
|
||||||
|
|
||||||
group "default" {
|
|
||||||
targets = ["automation"]
|
|
||||||
}
|
|
||||||
|
|
||||||
target "docker-metadata-action" {}
|
|
||||||
|
|
||||||
target "automation" {
|
|
||||||
inherits = ["docker-metadata-action"]
|
|
||||||
context = "./"
|
|
||||||
dockerfile = "Dockerfile"
|
|
||||||
tags = [for tag in target.docker-metadata-action.tags : "${TAG_BASE}:${tag}"]
|
|
||||||
args = {
|
|
||||||
RELEASE_VERSION="${RELEASE_VERSION}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
[toolchain]
|
[toolchain]
|
||||||
channel = "nightly-2026-05-12"
|
channel = "nightly-2025-08-20"
|
||||||
components = ["rustfmt", "clippy", "rust-analyzer"]
|
components = ["rustfmt", "clippy", "rust-analyzer"]
|
||||||
profile = "minimal"
|
profile = "minimal"
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use std::process;
|
|||||||
|
|
||||||
use ::config::{Environment, File};
|
use ::config::{Environment, File};
|
||||||
use automation::config::{Config, Setup};
|
use automation::config::{Config, Setup};
|
||||||
|
use automation::schedule::start_scheduler;
|
||||||
use automation::secret::EnvironmentSecretFile;
|
use automation::secret::EnvironmentSecretFile;
|
||||||
use automation::version::VERSION;
|
use automation::version::VERSION;
|
||||||
use automation::web::{ApiError, User};
|
use automation::web::{ApiError, User};
|
||||||
@@ -144,7 +145,7 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
device_manager.add(device).await;
|
device_manager.add(device).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolved.scheduler.start().await?;
|
start_scheduler(config.schedule).await?;
|
||||||
|
|
||||||
// Create google home fulfillment route
|
// Create google home fulfillment route
|
||||||
let fulfillment = Router::new().route("/google_home", post(fulfillment));
|
let fulfillment = Router::new().route("/google_home", post(fulfillment));
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use automation::config::generate_definitions;
|
use automation::config::{Config, FulfillmentConfig, Module as ConfigModule};
|
||||||
use automation_lib::Module;
|
use automation_lib::Module;
|
||||||
|
use automation_lib::mqtt::{MqttConfig, WrappedAsyncClient};
|
||||||
|
use lua_typed::Typed;
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
extern crate automation_devices;
|
extern crate automation_devices;
|
||||||
@@ -25,6 +27,24 @@ fn write_definitions(filename: &str, definitions: &str) -> std::io::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn config_definitions() -> String {
|
||||||
|
let mut output = "---@meta\n\n".to_string();
|
||||||
|
|
||||||
|
output +=
|
||||||
|
&FulfillmentConfig::generate_full().expect("FulfillmentConfig should have a definition");
|
||||||
|
output += "\n";
|
||||||
|
output += &Config::generate_full().expect("Config should have a definition");
|
||||||
|
output += "\n";
|
||||||
|
output += &ConfigModule::generate_full().expect("Module should have a definition");
|
||||||
|
output += "\n";
|
||||||
|
output += &MqttConfig::generate_full().expect("MqttConfig should have a definition");
|
||||||
|
output += "\n";
|
||||||
|
output +=
|
||||||
|
&WrappedAsyncClient::generate_full().expect("WrappedAsyncClient should have a definition");
|
||||||
|
|
||||||
|
output
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
@@ -39,7 +59,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
write_definitions("config.lua", &generate_definitions())?;
|
write_definitions("config.lua", &config_definitions())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-99
@@ -1,6 +1,5 @@
|
|||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
use std::net::{Ipv4Addr, SocketAddr};
|
use std::net::{Ipv4Addr, SocketAddr};
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
use automation_lib::action_callback::ActionCallback;
|
use automation_lib::action_callback::ActionCallback;
|
||||||
use automation_lib::device::Device;
|
use automation_lib::device::Device;
|
||||||
@@ -10,8 +9,6 @@ use lua_typed::Typed;
|
|||||||
use mlua::FromLua;
|
use mlua::FromLua;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::schedule::Scheduler;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Setup {
|
pub struct Setup {
|
||||||
#[serde(default = "default_entrypoint")]
|
#[serde(default = "default_entrypoint")]
|
||||||
@@ -38,20 +35,15 @@ pub struct FulfillmentConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct SetupFunction(mlua::Function);
|
pub struct SetupFunction(mlua::Function);
|
||||||
|
|
||||||
impl Typed for SetupFunction {
|
impl Typed for SetupFunction {
|
||||||
fn type_name() -> String {
|
fn type_name() -> String {
|
||||||
"SetupFunction".into()
|
format!(
|
||||||
}
|
"fun(mqtt_client: {}): {} | DeviceInterface[] | nil",
|
||||||
|
|
||||||
fn generate_header() -> Option<String> {
|
|
||||||
Some(format!(
|
|
||||||
"---@alias {} fun(mqtt_client: {}): {} | DeviceInterface[] | nil\n",
|
|
||||||
Self::type_name(),
|
|
||||||
WrappedAsyncClient::type_name(),
|
WrappedAsyncClient::type_name(),
|
||||||
Module::type_name()
|
Module::type_name()
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,53 +53,11 @@ impl FromLua for SetupFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for SetupFunction {
|
|
||||||
type Target = mlua::Function;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
struct Schedule(HashMap<String, ActionCallback<()>>);
|
pub struct Module {
|
||||||
|
pub setup: Option<SetupFunction>,
|
||||||
impl Typed for Schedule {
|
pub devices: Vec<Box<dyn Device>>,
|
||||||
fn type_name() -> String {
|
pub modules: Vec<Module>,
|
||||||
"Schedule".into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_header() -> Option<String> {
|
|
||||||
Some(format!(
|
|
||||||
"---@alias {} {}\n",
|
|
||||||
Self::type_name(),
|
|
||||||
HashMap::<String, ActionCallback<()>>::type_name(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromLua for Schedule {
|
|
||||||
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
|
||||||
Ok(Self(FromLua::from_lua(value, lua)?))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoIterator for Schedule {
|
|
||||||
type Item = <HashMap<String, ActionCallback<()>> as IntoIterator>::Item;
|
|
||||||
|
|
||||||
type IntoIter = <HashMap<String, ActionCallback<()>> as IntoIterator>::IntoIter;
|
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
|
||||||
self.0.into_iter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
struct Module {
|
|
||||||
setup: Option<SetupFunction>,
|
|
||||||
devices: Vec<Box<dyn Device>>,
|
|
||||||
schedule: Schedule,
|
|
||||||
modules: Vec<Module>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add option to typed to rename field
|
// TODO: Add option to typed to rename field
|
||||||
@@ -123,14 +73,10 @@ impl Typed for Module {
|
|||||||
fn generate_members() -> Option<String> {
|
fn generate_members() -> Option<String> {
|
||||||
Some(format!(
|
Some(format!(
|
||||||
r#"---@field setup {}
|
r#"---@field setup {}
|
||||||
---@field devices {}?
|
---@field devices DeviceInterface[]?
|
||||||
---@field schedule {}?
|
---@field [number] Module[]?
|
||||||
---@field [number] {}?
|
|
||||||
"#,
|
"#,
|
||||||
Option::<SetupFunction>::type_name(),
|
Option::<SetupFunction>::type_name()
|
||||||
Vec::<Box<dyn Device>>::type_name(),
|
|
||||||
Schedule::type_name(),
|
|
||||||
Vec::<Module>::type_name(),
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,9 +103,8 @@ impl FromLua for Module {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let setup = table.get("setup")?;
|
let setup = table.get("setup")?;
|
||||||
let devices = table.get("devices").unwrap_or_default();
|
|
||||||
let schedule = table.get("schedule").unwrap_or_default();
|
|
||||||
|
|
||||||
|
let devices = table.get("devices").unwrap_or_else(|_| Vec::new());
|
||||||
let mut modules = Vec::new();
|
let mut modules = Vec::new();
|
||||||
|
|
||||||
for module in table.sequence_values::<Module>() {
|
for module in table.sequence_values::<Module>() {
|
||||||
@@ -169,7 +114,6 @@ impl FromLua for Module {
|
|||||||
Ok(Module {
|
Ok(Module {
|
||||||
setup,
|
setup,
|
||||||
devices,
|
devices,
|
||||||
schedule,
|
|
||||||
modules,
|
modules,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -196,10 +140,10 @@ impl Modules {
|
|||||||
lua: &mlua::Lua,
|
lua: &mlua::Lua,
|
||||||
client: &WrappedAsyncClient,
|
client: &WrappedAsyncClient,
|
||||||
) -> mlua::Result<Resolved> {
|
) -> mlua::Result<Resolved> {
|
||||||
let mut devices = Vec::new();
|
|
||||||
let mut scheduler = Scheduler::default();
|
|
||||||
|
|
||||||
let mut modules: VecDeque<_> = self.0.into();
|
let mut modules: VecDeque<_> = self.0.into();
|
||||||
|
|
||||||
|
let mut devices = Vec::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let Some(module) = modules.pop_front() else {
|
let Some(module) = modules.pop_front() else {
|
||||||
break;
|
break;
|
||||||
@@ -208,7 +152,7 @@ impl Modules {
|
|||||||
modules.extend(module.modules);
|
modules.extend(module.modules);
|
||||||
|
|
||||||
if let Some(setup) = module.setup {
|
if let Some(setup) = module.setup {
|
||||||
let result: mlua::Value = setup.call_async(client.clone()).await?;
|
let result: mlua::Value = setup.0.call_async(client.clone()).await?;
|
||||||
|
|
||||||
if result.is_nil() {
|
if result.is_nil() {
|
||||||
// We ignore nil results
|
// We ignore nil results
|
||||||
@@ -228,19 +172,15 @@ impl Modules {
|
|||||||
}
|
}
|
||||||
|
|
||||||
devices.extend(module.devices);
|
devices.extend(module.devices);
|
||||||
for (cron, f) in module.schedule {
|
|
||||||
scheduler.add_job(cron, f);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Resolved { devices, scheduler })
|
Ok(Resolved { devices })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Resolved {
|
pub struct Resolved {
|
||||||
pub devices: Vec<Box<dyn Device>>,
|
pub devices: Vec<Box<dyn Device>>,
|
||||||
pub scheduler: Scheduler,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDeviceConfig, Typed)]
|
#[derive(Debug, LuaDeviceConfig, Typed)]
|
||||||
@@ -250,6 +190,9 @@ pub struct Config {
|
|||||||
pub modules: Modules,
|
pub modules: Modules,
|
||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub mqtt: MqttConfig,
|
pub mqtt: MqttConfig,
|
||||||
|
#[device_config(from_lua, default)]
|
||||||
|
#[typed(default)]
|
||||||
|
pub schedule: HashMap<String, ActionCallback<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<FulfillmentConfig> for SocketAddr {
|
impl From<FulfillmentConfig> for SocketAddr {
|
||||||
@@ -264,25 +207,3 @@ fn default_fulfillment_ip() -> Ipv4Addr {
|
|||||||
fn default_fulfillment_port() -> u16 {
|
fn default_fulfillment_port() -> u16 {
|
||||||
7878
|
7878
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_definitions() -> String {
|
|
||||||
let mut output = "---@meta\n\n".to_string();
|
|
||||||
|
|
||||||
output +=
|
|
||||||
&FulfillmentConfig::generate_full().expect("FulfillmentConfig should have a definition");
|
|
||||||
output += "\n";
|
|
||||||
output += &Config::generate_full().expect("Config should have a definition");
|
|
||||||
output += "\n";
|
|
||||||
output += &SetupFunction::generate_full().expect("SetupFunction should have a definition");
|
|
||||||
output += "\n";
|
|
||||||
output += &Schedule::generate_full().expect("Schedule should have a definition");
|
|
||||||
output += "\n";
|
|
||||||
output += &Module::generate_full().expect("Module should have a definition");
|
|
||||||
output += "\n";
|
|
||||||
output += &MqttConfig::generate_full().expect("MqttConfig should have a definition");
|
|
||||||
output += "\n";
|
|
||||||
output +=
|
|
||||||
&WrappedAsyncClient::generate_full().expect("WrappedAsyncClient should have a definition");
|
|
||||||
|
|
||||||
output
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#![feature(if_let_guard)]
|
||||||
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod schedule;
|
pub mod schedule;
|
||||||
pub mod secret;
|
pub mod secret;
|
||||||
|
|||||||
+20
-28
@@ -1,37 +1,29 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
use automation_lib::action_callback::ActionCallback;
|
use automation_lib::action_callback::ActionCallback;
|
||||||
use tokio_cron_scheduler::{Job, JobScheduler, JobSchedulerError};
|
use tokio_cron_scheduler::{Job, JobScheduler, JobSchedulerError};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
pub async fn start_scheduler(
|
||||||
pub struct Scheduler {
|
schedule: HashMap<String, ActionCallback<()>>,
|
||||||
jobs: Vec<(String, ActionCallback<()>)>,
|
) -> Result<(), JobSchedulerError> {
|
||||||
}
|
let scheduler = JobScheduler::new().await?;
|
||||||
|
|
||||||
impl Scheduler {
|
for (s, f) in schedule {
|
||||||
pub fn add_job(&mut self, cron: String, f: ActionCallback<()>) {
|
let job = {
|
||||||
self.jobs.push((cron, f));
|
move |_uuid, _lock| -> Pin<Box<dyn Future<Output = ()> + Send>> {
|
||||||
|
let f = f.clone();
|
||||||
|
|
||||||
|
Box::pin(async move {
|
||||||
|
f.call(()).await;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let job = Job::new_async(s, job)?;
|
||||||
|
|
||||||
|
scheduler.add(job).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn start(self) -> Result<(), JobSchedulerError> {
|
scheduler.start().await
|
||||||
let scheduler = JobScheduler::new().await?;
|
|
||||||
|
|
||||||
for (s, f) in self.jobs {
|
|
||||||
let job = {
|
|
||||||
move |_uuid, _lock| -> Pin<Box<dyn Future<Output = ()> + Send>> {
|
|
||||||
let f = f.clone();
|
|
||||||
|
|
||||||
Box::pin(async move {
|
|
||||||
f.call(()).await;
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let job = Job::new_async(s, job)?;
|
|
||||||
|
|
||||||
scheduler.add(job).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
scheduler.start().await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user