Compare commits
12 Commits
7db628709a
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
8c6adae3ae
|
|||
|
2158bde1c2
|
|||
|
b547f66d86
|
|||
|
f3de8e36ea
|
|||
|
44f2c57819
|
|||
|
ad158f2c22
|
|||
|
f36adf2f19
|
|||
|
5947098bfb
|
|||
|
8a3143a3ea
|
|||
|
9546585440
|
|||
|
a938f3d71b
|
|||
|
a6c19eb9b4
|
@@ -9,10 +9,10 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
uses: dreaded_x/workflows/.gitea/workflows/rust-kubernetes.yaml@22ee0c1788a8d2157db87d6a6f8dbe520fe48592
|
uses: dreaded_x/workflows/.gitea/workflows/docker-kubernetes.yaml@ef78704b98c72e4a6b8340f9bff7b085a7bdd95c
|
||||||
secrets: inherit
|
secrets: inherit
|
||||||
with:
|
with:
|
||||||
upload_manifests: false
|
push_manifests: false
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
name: Deploy container
|
name: Deploy container
|
||||||
@@ -37,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 }} \
|
||||||
git.huizinga.dev/dreaded_x/automation_rs@${{ needs.build.outputs.digest }}
|
$(echo ${{ toJSON(needs.build.outputs.images) }} | jq .automation -r)
|
||||||
|
|
||||||
docker network connect web automation_rs
|
docker network connect web automation_rs
|
||||||
|
|
||||||
|
|||||||
11
Cargo.lock
generated
11
Cargo.lock
generated
@@ -555,7 +555,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "778e2ac28f6c47af28e4907f13ffd1e1ddbd400980a9abd7c8df189bf578a5ad"
|
checksum = "778e2ac28f6c47af28e4907f13ffd1e1ddbd400980a9abd7c8df189bf578a5ad"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.59.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1163,17 +1163,16 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "lua_typed"
|
name = "lua_typed"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#f6a684291432aae2ef7109712882e7e3ed758d08"
|
source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#3d29c9dd143737c8bffe4bacae8e701de3c6ee10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"eui48",
|
"eui48",
|
||||||
"lua_typed_macro",
|
"lua_typed_macro",
|
||||||
"mlua",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lua_typed_macro"
|
name = "lua_typed_macro"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#f6a684291432aae2ef7109712882e7e3ed758d08"
|
source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#3d29c9dd143737c8bffe4bacae8e701de3c6ee10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"convert_case",
|
"convert_case",
|
||||||
"itertools",
|
"itertools",
|
||||||
@@ -1568,7 +1567,7 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tracing",
|
"tracing",
|
||||||
"windows-sys 0.59.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1745,7 +1744,7 @@ dependencies = [
|
|||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys",
|
"linux-raw-sys",
|
||||||
"windows-sys 0.59.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ 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
|
||||||
|
COPY ./rust-toolchain.toml .
|
||||||
|
RUN rustup toolchain install
|
||||||
|
|
||||||
FROM base AS planner
|
FROM base AS planner
|
||||||
COPY . .
|
COPY . .
|
||||||
@@ -21,5 +23,7 @@ RUN cargo auditable build --release
|
|||||||
|
|
||||||
FROM gcr.io/distroless/cc-debian12: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" ]
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
local ntfy = require("config.ntfy")
|
local ntfy = require("config.ntfy")
|
||||||
|
|
||||||
|
--- @class BatteryModule: Module
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
--- @type {[string]: number}
|
--- @type {[string]: number}
|
||||||
@@ -17,7 +18,7 @@ function module.callback(device, battery)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function module.notify_low_battery()
|
local function 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")
|
||||||
@@ -38,4 +39,9 @@ function module.notify_low_battery()
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- @type Schedule
|
||||||
|
module.schedule = {
|
||||||
|
["0 0 21 */1 * *"] = notify_low_battery,
|
||||||
|
}
|
||||||
|
|
||||||
return module
|
return module
|
||||||
|
|||||||
@@ -29,14 +29,4 @@ 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,6 +4,7 @@ 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,6 +1,7 @@
|
|||||||
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,5 +1,6 @@
|
|||||||
local utils = require("automation:utils")
|
local utils = require("automation:utils")
|
||||||
|
|
||||||
|
--- @class HelperModule: Module
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
--- @param topic string
|
--- @param topic string
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ 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,6 +1,7 @@
|
|||||||
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
|
||||||
@@ -34,6 +35,7 @@ function module.setup(mqtt_client)
|
|||||||
callback = callback,
|
callback = callback,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
--- @type Module
|
||||||
return {
|
return {
|
||||||
module.device,
|
module.device,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
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
|
||||||
@@ -24,6 +25,7 @@ function module.setup()
|
|||||||
topic = ntfy_topic,
|
topic = ntfy_topic,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
--- @type Module
|
||||||
return {
|
return {
|
||||||
ntfy,
|
ntfy,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ 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
|
||||||
@@ -61,6 +62,7 @@ function module.setup(mqtt_client)
|
|||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
--- @type Module
|
||||||
return {
|
return {
|
||||||
presence,
|
presence,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
--- @type SetupTable
|
--- @type Module
|
||||||
return {
|
return {
|
||||||
require("config.rooms.bathroom"),
|
require("config.rooms.bathroom"),
|
||||||
require("config.rooms.bedroom"),
|
require("config.rooms.bedroom"),
|
||||||
|
|||||||
@@ -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,6 +30,7 @@ function module.setup(mqtt_client)
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
--- @type Module
|
||||||
return {
|
return {
|
||||||
light,
|
light,
|
||||||
washer,
|
washer,
|
||||||
|
|||||||
@@ -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",
|
||||||
@@ -55,20 +55,24 @@ function module.setup(mqtt_client)
|
|||||||
})
|
})
|
||||||
windows.add(window)
|
windows.add(window)
|
||||||
|
|
||||||
|
--- @type Module
|
||||||
return {
|
return {
|
||||||
lights,
|
devices = {
|
||||||
lights_relax,
|
lights,
|
||||||
air_filter,
|
lights_relax,
|
||||||
switch,
|
air_filter,
|
||||||
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,9 +25,18 @@ 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,26 +59,21 @@ function module.setup(mqtt_client)
|
|||||||
})
|
})
|
||||||
hallway_automation.set_trash(trash)
|
hallway_automation.set_trash(trash)
|
||||||
|
|
||||||
---@param duration number
|
local timeout = utils.Timeout.new()
|
||||||
---@return fun(_, open: boolean)
|
local function frontdoor_presence(_, open)
|
||||||
local function frontdoor_presence(duration)
|
if open then
|
||||||
local timeout = utils.Timeout.new()
|
timeout:cancel()
|
||||||
|
|
||||||
return function(_, open)
|
if not presence.overall_presence() then
|
||||||
if open then
|
mqtt_client:send_message(helper.mqtt_automation("presence/contact/frontdoor"), {
|
||||||
timeout:cancel()
|
state = true,
|
||||||
|
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
|
||||||
|
|
||||||
@@ -89,7 +84,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(debug.debug_mode and 10 or 15 * 60),
|
frontdoor_presence,
|
||||||
hallway_automation.door_callback,
|
hallway_automation.door_callback,
|
||||||
},
|
},
|
||||||
battery_callback = battery.callback,
|
battery_callback = battery.callback,
|
||||||
@@ -97,6 +92,7 @@ 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,6 +37,7 @@ function module.setup(mqtt_client)
|
|||||||
battery_callback = battery.callback,
|
battery_callback = battery.callback,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
--- @type Module
|
||||||
return {
|
return {
|
||||||
light,
|
light,
|
||||||
top_switch,
|
top_switch,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ 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,6 +109,7 @@ 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,6 +31,7 @@ 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,6 +57,7 @@ function module.setup(mqtt_client)
|
|||||||
battery_callback = battery.callback,
|
battery_callback = battery.callback,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
--- @type Module
|
||||||
return {
|
return {
|
||||||
charger,
|
charger,
|
||||||
outlets,
|
outlets,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
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
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ local devices
|
|||||||
---@class Action
|
---@class Action
|
||||||
---@field action
|
---@field action
|
||||||
---| "broadcast"
|
---| "broadcast"
|
||||||
---@field extras table<string, string>?
|
---@field extras (table<string, string>)?
|
||||||
---@field label string
|
---@field label string
|
||||||
---@field clear boolean?
|
---@field clear (boolean)?
|
||||||
local Action
|
local Action
|
||||||
|
|
||||||
---@class AirFilter: DeviceInterface, OnOffInterface
|
---@class AirFilter: DeviceInterface, OnOffInterface
|
||||||
@@ -20,49 +20,49 @@ function devices.AirFilter.new(config) end
|
|||||||
|
|
||||||
---@class AirFilterConfig
|
---@class AirFilterConfig
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field room string?
|
---@field room (string)?
|
||||||
---@field url string
|
---@field url string
|
||||||
local AirFilterConfig
|
local AirFilterConfig
|
||||||
|
|
||||||
---@class ConfigLightLightStateBrightness
|
---@class ConfigLightLightStateBrightness
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field room string?
|
---@field room (string)?
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field callback fun(_: LightBrightness, _: LightStateBrightness) | fun(_: LightBrightness, _: LightStateBrightness)[]?
|
---@field callback (fun(_: LightBrightness, _: LightStateBrightness) | fun(_: LightBrightness, _: LightStateBrightness)[])?
|
||||||
---@field client AsyncClient?
|
---@field client (AsyncClient)?
|
||||||
local ConfigLightLightStateBrightness
|
local ConfigLightLightStateBrightness
|
||||||
|
|
||||||
---@class ConfigLightLightStateColorTemperature
|
---@class ConfigLightLightStateColorTemperature
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field room string?
|
---@field room (string)?
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field callback fun(_: LightColorTemperature, _: LightStateColorTemperature) | fun(_: LightColorTemperature, _: LightStateColorTemperature)[]?
|
---@field callback (fun(_: LightColorTemperature, _: LightStateColorTemperature) | fun(_: LightColorTemperature, _: LightStateColorTemperature)[])?
|
||||||
---@field client AsyncClient?
|
---@field client (AsyncClient)?
|
||||||
local ConfigLightLightStateColorTemperature
|
local ConfigLightLightStateColorTemperature
|
||||||
|
|
||||||
---@class ConfigLightLightStateOnOff
|
---@class ConfigLightLightStateOnOff
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field room string?
|
---@field room (string)?
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field callback fun(_: LightOnOff, _: LightStateOnOff) | fun(_: LightOnOff, _: LightStateOnOff)[]?
|
---@field callback (fun(_: LightOnOff, _: LightStateOnOff) | fun(_: LightOnOff, _: LightStateOnOff)[])?
|
||||||
---@field client AsyncClient?
|
---@field client (AsyncClient)?
|
||||||
local ConfigLightLightStateOnOff
|
local ConfigLightLightStateOnOff
|
||||||
|
|
||||||
---@class ConfigOutletOutletStateOnOff
|
---@class ConfigOutletOutletStateOnOff
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field room string?
|
---@field room (string)?
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field outlet_type OutletType?
|
---@field outlet_type (OutletType)?
|
||||||
---@field callback fun(_: OutletOnOff, _: OutletStateOnOff) | fun(_: OutletOnOff, _: OutletStateOnOff)[]?
|
---@field callback (fun(_: OutletOnOff, _: OutletStateOnOff) | fun(_: OutletOnOff, _: OutletStateOnOff)[])?
|
||||||
---@field client AsyncClient
|
---@field client AsyncClient
|
||||||
local ConfigOutletOutletStateOnOff
|
local ConfigOutletOutletStateOnOff
|
||||||
|
|
||||||
---@class ConfigOutletOutletStatePower
|
---@class ConfigOutletOutletStatePower
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field room string?
|
---@field room (string)?
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field outlet_type OutletType?
|
---@field outlet_type (OutletType)?
|
||||||
---@field callback fun(_: OutletPower, _: OutletStatePower) | fun(_: OutletPower, _: OutletStatePower)[]?
|
---@field callback (fun(_: OutletPower, _: OutletStatePower) | fun(_: OutletPower, _: OutletStatePower)[])?
|
||||||
---@field client AsyncClient
|
---@field client AsyncClient
|
||||||
local ConfigOutletOutletStatePower
|
local ConfigOutletOutletStatePower
|
||||||
|
|
||||||
@@ -75,12 +75,12 @@ function devices.ContactSensor.new(config) end
|
|||||||
|
|
||||||
---@class ContactSensorConfig
|
---@class ContactSensorConfig
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field room string?
|
---@field room (string)?
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field sensor_type SensorType?
|
---@field sensor_type (SensorType)?
|
||||||
---@field callback fun(_: ContactSensor, _: boolean) | fun(_: ContactSensor, _: boolean)[]?
|
---@field callback (fun(_: ContactSensor, _: boolean) | fun(_: ContactSensor, _: boolean)[])?
|
||||||
---@field battery_callback fun(_: ContactSensor, _: number) | fun(_: ContactSensor, _: number)[]?
|
---@field battery_callback (fun(_: ContactSensor, _: number) | fun(_: ContactSensor, _: number)[])?
|
||||||
---@field client AsyncClient?
|
---@field client (AsyncClient)?
|
||||||
local ContactSensorConfig
|
local ContactSensorConfig
|
||||||
|
|
||||||
---@alias Flag
|
---@alias Flag
|
||||||
@@ -134,14 +134,14 @@ function devices.HueSwitch.new(config) end
|
|||||||
|
|
||||||
---@class HueSwitchConfig
|
---@class HueSwitchConfig
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field room string?
|
---@field room (string)?
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field client AsyncClient
|
---@field client AsyncClient
|
||||||
---@field left_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
---@field left_callback (fun(_: HueSwitch) | fun(_: HueSwitch)[])?
|
||||||
---@field right_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
---@field right_callback (fun(_: HueSwitch) | fun(_: HueSwitch)[])?
|
||||||
---@field left_hold_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
---@field left_hold_callback (fun(_: HueSwitch) | fun(_: HueSwitch)[])?
|
||||||
---@field right_hold_callback fun(_: HueSwitch) | fun(_: HueSwitch)[]?
|
---@field right_hold_callback (fun(_: HueSwitch) | fun(_: HueSwitch)[])?
|
||||||
---@field battery_callback fun(_: HueSwitch, _: number) | fun(_: HueSwitch, _: number)[]?
|
---@field battery_callback (fun(_: HueSwitch, _: number) | fun(_: HueSwitch, _: number)[])?
|
||||||
local HueSwitchConfig
|
local HueSwitchConfig
|
||||||
|
|
||||||
---@class IkeaRemote: DeviceInterface
|
---@class IkeaRemote: DeviceInterface
|
||||||
@@ -153,12 +153,12 @@ function devices.IkeaRemote.new(config) end
|
|||||||
|
|
||||||
---@class IkeaRemoteConfig
|
---@class IkeaRemoteConfig
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field room string?
|
---@field room (string)?
|
||||||
---@field single_button boolean?
|
---@field single_button (boolean)?
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field client AsyncClient
|
---@field client AsyncClient
|
||||||
---@field callback fun(_: IkeaRemote, _: boolean) | fun(_: IkeaRemote, _: boolean)[]?
|
---@field callback (fun(_: IkeaRemote, _: boolean) | fun(_: IkeaRemote, _: boolean)[])?
|
||||||
---@field battery_callback fun(_: IkeaRemote, _: number) | fun(_: IkeaRemote, _: number)[]?
|
---@field battery_callback (fun(_: IkeaRemote, _: number) | fun(_: IkeaRemote, _: number)[])?
|
||||||
local IkeaRemoteConfig
|
local IkeaRemoteConfig
|
||||||
|
|
||||||
---@class KasaOutlet: DeviceInterface, OnOffInterface
|
---@class KasaOutlet: DeviceInterface, OnOffInterface
|
||||||
@@ -206,7 +206,7 @@ function devices.LightSensor.new(config) end
|
|||||||
---@field topic string
|
---@field topic string
|
||||||
---@field min integer
|
---@field min integer
|
||||||
---@field max integer
|
---@field max integer
|
||||||
---@field callback fun(_: LightSensor, _: boolean) | fun(_: LightSensor, _: boolean)[]?
|
---@field callback (fun(_: LightSensor, _: boolean) | fun(_: LightSensor, _: boolean)[])?
|
||||||
---@field client AsyncClient
|
---@field client AsyncClient
|
||||||
local LightSensorConfig
|
local LightSensorConfig
|
||||||
|
|
||||||
@@ -227,10 +227,10 @@ local LightStateOnOff
|
|||||||
|
|
||||||
---@class Notification
|
---@class Notification
|
||||||
---@field title string
|
---@field title string
|
||||||
---@field message string?
|
---@field message (string)?
|
||||||
---@field tags string[]?
|
---@field tags ((string)[])?
|
||||||
---@field priority Priority?
|
---@field priority (Priority)?
|
||||||
---@field actions Action[]?
|
---@field actions ((Action)[])?
|
||||||
local Notification
|
local Notification
|
||||||
|
|
||||||
---@class Ntfy: DeviceInterface
|
---@class Ntfy: DeviceInterface
|
||||||
@@ -244,7 +244,7 @@ function devices.Ntfy.new(config) end
|
|||||||
function Ntfy:send_notification(notification) end
|
function Ntfy:send_notification(notification) end
|
||||||
|
|
||||||
---@class NtfyConfig
|
---@class NtfyConfig
|
||||||
---@field url string?
|
---@field url (string)?
|
||||||
---@field topic string
|
---@field topic string
|
||||||
local NtfyConfig
|
local NtfyConfig
|
||||||
|
|
||||||
@@ -287,7 +287,7 @@ function Presence:overall_presence() end
|
|||||||
|
|
||||||
---@class PresenceConfig
|
---@class PresenceConfig
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field callback fun(_: Presence, _: boolean) | fun(_: Presence, _: boolean)[]?
|
---@field callback (fun(_: Presence, _: boolean) | fun(_: Presence, _: boolean)[])?
|
||||||
---@field client AsyncClient
|
---@field client AsyncClient
|
||||||
local PresenceConfig
|
local PresenceConfig
|
||||||
|
|
||||||
@@ -321,16 +321,16 @@ function devices.Washer.new(config) end
|
|||||||
---@field identifier string
|
---@field identifier string
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field threshold number
|
---@field threshold number
|
||||||
---@field done_callback fun(_: Washer) | fun(_: Washer)[]?
|
---@field done_callback (fun(_: Washer) | fun(_: Washer)[])?
|
||||||
---@field client AsyncClient
|
---@field client AsyncClient
|
||||||
local WasherConfig
|
local WasherConfig
|
||||||
|
|
||||||
---@class WolConfig
|
---@class WolConfig
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field room string?
|
---@field room (string)?
|
||||||
---@field topic string
|
---@field topic string
|
||||||
---@field mac_address string
|
---@field mac_address string
|
||||||
---@field broadcast_ip string?
|
---@field broadcast_ip (string)?
|
||||||
---@field client AsyncClient
|
---@field client AsyncClient
|
||||||
local WolConfig
|
local WolConfig
|
||||||
|
|
||||||
|
|||||||
@@ -3,20 +3,26 @@
|
|||||||
|
|
||||||
---@class FulfillmentConfig
|
---@class FulfillmentConfig
|
||||||
---@field openid_url string
|
---@field openid_url string
|
||||||
---@field ip string?
|
---@field ip (string)?
|
||||||
---@field port integer?
|
---@field port (integer)?
|
||||||
local FulfillmentConfig
|
local FulfillmentConfig
|
||||||
|
|
||||||
---@class Config
|
---@class Config
|
||||||
---@field fulfillment FulfillmentConfig
|
---@field fulfillment FulfillmentConfig
|
||||||
---@field modules Modules?
|
---@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): SetupTable?
|
---@alias SetupFunction fun(mqtt_client: AsyncClient): Module | DeviceInterface[] | nil
|
||||||
---@alias SetupTable (DeviceInterface | { setup: SetupFunction? } | SetupTable)[]
|
|
||||||
---@alias Modules SetupFunction | SetupTable
|
---@alias Schedule table<string, fun() | fun()[]>
|
||||||
|
|
||||||
|
---@class Module
|
||||||
|
---@field setup (SetupFunction)?
|
||||||
|
---@field devices (DeviceInterface)[]?
|
||||||
|
---@field schedule Schedule?
|
||||||
|
---@field [number] (Module)[]?
|
||||||
|
local Module
|
||||||
|
|
||||||
---@class MqttConfig
|
---@class MqttConfig
|
||||||
---@field host string
|
---@field host string
|
||||||
@@ -24,7 +30,7 @@ local Config
|
|||||||
---@field client_name string
|
---@field client_name string
|
||||||
---@field username string
|
---@field username string
|
||||||
---@field password string
|
---@field password string
|
||||||
---@field tls boolean?
|
---@field tls (boolean)?
|
||||||
local MqttConfig
|
local MqttConfig
|
||||||
|
|
||||||
---@class AsyncClient
|
---@class AsyncClient
|
||||||
|
|||||||
18
docker-bake.hcl
Normal file
18
docker-bake.hcl
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
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}"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@ 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};
|
||||||
@@ -140,13 +139,12 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
let mqtt_client = mqtt::start(config.mqtt, &device_manager.event_channel());
|
let mqtt_client = mqtt::start(config.mqtt, &device_manager.event_channel());
|
||||||
|
|
||||||
if let Some(modules) = config.modules {
|
let resolved = config.modules.resolve(&lua, &mqtt_client).await?;
|
||||||
for device in modules.setup(&lua, &mqtt_client).await? {
|
for device in resolved.devices {
|
||||||
device_manager.add(device).await;
|
device_manager.add(device).await;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
start_scheduler(config.schedule).await?;
|
resolved.scheduler.start().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,10 +1,8 @@
|
|||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use automation::config::{Config, FulfillmentConfig, Modules};
|
use automation::config::generate_definitions;
|
||||||
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;
|
||||||
@@ -27,24 +25,6 @@ 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 += &Modules::generate_full().expect("Setups 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();
|
||||||
|
|
||||||
@@ -59,7 +39,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
write_definitions("config.lua", &config_definitions())?;
|
write_definitions("config.lua", &generate_definitions())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
272
src/config.rs
272
src/config.rs
@@ -1,5 +1,6 @@
|
|||||||
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;
|
||||||
@@ -9,6 +10,8 @@ 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")]
|
||||||
@@ -34,90 +37,219 @@ pub struct FulfillmentConfig {
|
|||||||
pub port: u16,
|
pub port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct SetupFunction(mlua::Function);
|
||||||
|
|
||||||
|
impl Typed for SetupFunction {
|
||||||
|
fn type_name() -> String {
|
||||||
|
"SetupFunction".into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_header() -> Option<String> {
|
||||||
|
Some(format!(
|
||||||
|
"---@alias {} fun(mqtt_client: {}): {} | DeviceInterface[] | nil\n",
|
||||||
|
Self::type_name(),
|
||||||
|
WrappedAsyncClient::type_name(),
|
||||||
|
Module::type_name()
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromLua for SetupFunction {
|
||||||
|
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||||
|
Ok(Self(FromLua::from_lua(value, lua)?))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for SetupFunction {
|
||||||
|
type Target = mlua::Function;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Modules(mlua::Value);
|
struct Schedule(HashMap<String, ActionCallback<()>>);
|
||||||
|
|
||||||
impl Modules {
|
impl Typed for Schedule {
|
||||||
pub async fn setup(
|
fn type_name() -> String {
|
||||||
self,
|
"Schedule".into()
|
||||||
lua: &mlua::Lua,
|
}
|
||||||
client: &WrappedAsyncClient,
|
|
||||||
) -> mlua::Result<Vec<Box<dyn Device>>> {
|
|
||||||
let mut devices = Vec::new();
|
|
||||||
let initial_table = match self.0 {
|
|
||||||
mlua::Value::Table(table) => table,
|
|
||||||
mlua::Value::Function(f) => f.call_async(client.clone()).await?,
|
|
||||||
_ => Err(mlua::Error::runtime(format!(
|
|
||||||
"Expected table or function, instead found: {}",
|
|
||||||
self.0.type_name()
|
|
||||||
)))?,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut queue: VecDeque<mlua::Table> = [initial_table].into();
|
fn generate_header() -> Option<String> {
|
||||||
loop {
|
Some(format!(
|
||||||
let Some(table) = queue.pop_front() else {
|
"---@alias {} {}\n",
|
||||||
break;
|
Self::type_name(),
|
||||||
};
|
HashMap::<String, ActionCallback<()>>::type_name(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for pair in table.pairs() {
|
impl FromLua for Schedule {
|
||||||
let (name, value): (String, _) = pair?;
|
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||||
|
Ok(Self(FromLua::from_lua(value, lua)?))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match value {
|
impl IntoIterator for Schedule {
|
||||||
mlua::Value::Table(table) => queue.push_back(table),
|
type Item = <HashMap<String, ActionCallback<()>> as IntoIterator>::Item;
|
||||||
mlua::Value::UserData(_)
|
|
||||||
if let Ok(device) = Box::from_lua(value.clone(), lua) =>
|
type IntoIter = <HashMap<String, ActionCallback<()>> as IntoIterator>::IntoIter;
|
||||||
{
|
|
||||||
devices.push(device);
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
}
|
self.0.into_iter()
|
||||||
mlua::Value::Function(f) if name == "setup" => {
|
}
|
||||||
let value: mlua::Value = f.call_async(client.clone()).await?;
|
}
|
||||||
if let Some(table) = value.as_table() {
|
|
||||||
queue.push_back(table.clone());
|
#[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
|
||||||
|
impl Typed for Module {
|
||||||
|
fn type_name() -> String {
|
||||||
|
"Module".into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_header() -> Option<String> {
|
||||||
|
Some(format!("---@class {}\n", Self::type_name()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_members() -> Option<String> {
|
||||||
|
Some(format!(
|
||||||
|
r#"---@field setup {}
|
||||||
|
---@field devices {}?
|
||||||
|
---@field schedule {}?
|
||||||
|
---@field [number] {}?
|
||||||
|
"#,
|
||||||
|
Option::<SetupFunction>::type_name(),
|
||||||
|
Vec::<Box<dyn Device>>::type_name(),
|
||||||
|
Schedule::type_name(),
|
||||||
|
Vec::<Module>::type_name(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_footer() -> Option<String> {
|
||||||
|
let type_name = <Self as Typed>::type_name();
|
||||||
|
Some(format!("local {type_name}\n"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromLua for Module {
|
||||||
|
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||||
|
// When calling require it might return a result from the searcher indicating how the
|
||||||
|
// module was found, we want to ignore these entries.
|
||||||
|
// TODO: Find a better solution for this
|
||||||
|
if value.is_string() {
|
||||||
|
return Ok(Default::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(devices)
|
let mlua::Value::Table(table) = value else {
|
||||||
|
return Err(mlua::Error::runtime(format!(
|
||||||
|
"Expected module table, instead found: {}",
|
||||||
|
value.type_name()
|
||||||
|
)));
|
||||||
|
};
|
||||||
|
|
||||||
|
let setup = table.get("setup")?;
|
||||||
|
let devices = table.get("devices").unwrap_or_default();
|
||||||
|
let schedule = table.get("schedule").unwrap_or_default();
|
||||||
|
|
||||||
|
let mut modules = Vec::new();
|
||||||
|
|
||||||
|
for module in table.sequence_values::<Module>() {
|
||||||
|
modules.push(module?);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Module {
|
||||||
|
setup,
|
||||||
|
devices,
|
||||||
|
schedule,
|
||||||
|
modules,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct Modules(Vec<Module>);
|
||||||
|
|
||||||
|
impl Typed for Modules {
|
||||||
|
fn type_name() -> String {
|
||||||
|
Vec::<Module>::type_name()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromLua for Modules {
|
impl FromLua for Modules {
|
||||||
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
|
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||||
Ok(Modules(value))
|
Ok(Self(FromLua::from_lua(value, lua)?))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Typed for Modules {
|
impl Modules {
|
||||||
fn type_name() -> String {
|
pub async fn resolve(
|
||||||
"Modules".into()
|
self,
|
||||||
}
|
lua: &mlua::Lua,
|
||||||
|
client: &WrappedAsyncClient,
|
||||||
|
) -> mlua::Result<Resolved> {
|
||||||
|
let mut devices = Vec::new();
|
||||||
|
let mut scheduler = Scheduler::default();
|
||||||
|
|
||||||
fn generate_header() -> Option<String> {
|
let mut modules: VecDeque<_> = self.0.into();
|
||||||
let type_name = Self::type_name();
|
loop {
|
||||||
let client_type = WrappedAsyncClient::type_name();
|
let Some(module) = modules.pop_front() else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
Some(format!(
|
modules.extend(module.modules);
|
||||||
r#"---@alias SetupFunction fun(mqtt_client: {client_type}): SetupTable?
|
|
||||||
---@alias SetupTable (DeviceInterface | {{ setup: SetupFunction? }} | SetupTable)[]
|
if let Some(setup) = module.setup {
|
||||||
---@alias {type_name} SetupFunction | SetupTable
|
let result: mlua::Value = setup.call_async(client.clone()).await?;
|
||||||
"#,
|
|
||||||
))
|
if result.is_nil() {
|
||||||
|
// We ignore nil results
|
||||||
|
} else if let Ok(d) = <Vec<_> as FromLua>::from_lua(result.clone(), lua)
|
||||||
|
&& !d.is_empty()
|
||||||
|
{
|
||||||
|
// This is a shortcut for the common pattern of setup functions that only
|
||||||
|
// return devices
|
||||||
|
devices.extend(d);
|
||||||
|
} else if let Ok(module) = FromLua::from_lua(result.clone(), lua) {
|
||||||
|
modules.push_back(module);
|
||||||
|
} else {
|
||||||
|
return Err(mlua::Error::runtime(
|
||||||
|
"Setup function returned data in an unexpected format",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
devices.extend(module.devices);
|
||||||
|
for (cron, f) in module.schedule {
|
||||||
|
scheduler.add_job(cron, f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Resolved { devices, scheduler })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct Resolved {
|
||||||
|
pub devices: Vec<Box<dyn Device>>,
|
||||||
|
pub scheduler: Scheduler,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, LuaDeviceConfig, Typed)]
|
#[derive(Debug, LuaDeviceConfig, Typed)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub fulfillment: FulfillmentConfig,
|
pub fulfillment: FulfillmentConfig,
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub modules: Option<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 {
|
||||||
@@ -132,3 +264,25 @@ 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,29 +1,37 @@
|
|||||||
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};
|
||||||
|
|
||||||
pub async fn start_scheduler(
|
#[derive(Debug, Default)]
|
||||||
schedule: HashMap<String, ActionCallback<()>>,
|
pub struct Scheduler {
|
||||||
) -> Result<(), JobSchedulerError> {
|
jobs: Vec<(String, ActionCallback<()>)>,
|
||||||
let scheduler = JobScheduler::new().await?;
|
}
|
||||||
|
|
||||||
for (s, f) in schedule {
|
impl Scheduler {
|
||||||
let job = {
|
pub fn add_job(&mut self, cron: String, f: ActionCallback<()>) {
|
||||||
move |_uuid, _lock| -> Pin<Box<dyn Future<Output = ()> + Send>> {
|
self.jobs.push((cron, f));
|
||||||
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
|
pub async fn start(self) -> Result<(), JobSchedulerError> {
|
||||||
|
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