Compare commits

6 Commits

Author SHA1 Message Date
7a9f464e61 feat(config)!: Move new_mqtt_client out of global automation table into separate module
All checks were successful
Build and deploy / build (push) Successful in 10m30s
Build and deploy / Deploy container (push) Successful in 45s
The function `new_mqtt_client` was the last remaining entry in the
global `automation` table. The function was renamed to `new` and placed
in the new `mqtt` module. As `automation` is now empty, it has been
removed.
2025-09-04 04:28:03 +02:00
3d5f6c308c feat(config)!: Move device_manager out of global automation table into separate module
Moved `automation.device_manager` into a separate module called
`device_manager`
2025-09-04 04:28:03 +02:00
c6a6265d6c feat(config)!: Move util out of global automation table into separate module
Move `automation.util` into a separate module called `utils`.
2025-09-04 04:28:02 +02:00
d6816bc693 feat(config)!: Fulfillment config is now returned at the end of the config
Previously the fulfillment config was set by setting
`automation.fulfillment`, this will no longer work in the future when
the global automation gets split into modules.
2025-09-04 04:28:02 +02:00
c8d5df753f style: Sort crates by name 2025-09-04 04:28:02 +02:00
96cb814495 style: Enforce conventional commits formatting 2025-09-04 04:28:02 +02:00
8 changed files with 200 additions and 203 deletions

View File

@@ -1,3 +1,9 @@
default_install_hook_types:
- pre-commit
- commit-msg
default_stages:
- pre-commit
repos: repos:
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0 rev: v6.0.0
@@ -11,6 +17,13 @@ repos:
- id: check-added-large-files - id: check-added-large-files
- id: check-merge-conflict - id: check-merge-conflict
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v4.2.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
args: []
- repo: https://github.com/JohnnyMorganz/StyLua - repo: https://github.com/JohnnyMorganz/StyLua
rev: v2.1.0 rev: v2.1.0
hooks: hooks:

View File

@@ -5,34 +5,22 @@ edition = "2024"
[workspace] [workspace]
members = [ members = [
"automation_macro",
"automation_cast", "automation_cast",
"google_home/google_home",
"google_home/google_home_macro",
"automation_devices", "automation_devices",
"automation_lib", "automation_lib",
"automation_macro",
"google_home/google_home",
"google_home/google_home_macro",
] ]
[workspace.dependencies] [workspace.dependencies]
mlua = { version = "0.11.3", features = [ air_filter_types = { git = "https://git.huizinga.dev/Dreaded_X/airfilter", tag = "v0.4.4" }
"lua54",
"vendored",
"macros",
"serialize",
"async",
"send",
] }
automation_macro = { path = "./automation_macro" }
automation_cast = { path = "./automation_cast" }
automation_lib = { path = "./automation_lib" }
automation_devices = { path = "./automation_devices" }
google_home = { path = "./google_home/google_home" }
google_home_macro = { path = "./google_home/google_home_macro" }
tokio = { version = "1", features = ["rt-multi-thread"] }
rumqttc = "0.24.0"
tracing = "0.1.41"
anyhow = "1.0.99" anyhow = "1.0.99"
async-trait = "0.1.89" async-trait = "0.1.89"
automation_cast = { path = "./automation_cast" }
automation_devices = { path = "./automation_devices" }
automation_lib = { path = "./automation_lib" }
automation_macro = { path = "./automation_macro" }
axum = "0.8.4" axum = "0.8.4"
bytes = "1.10.1" bytes = "1.10.1"
dotenvy = "0.15.7" dotenvy = "0.15.7"
@@ -42,45 +30,57 @@ eui48 = { version = "1.1.0", features = [
"serde", "serde",
], default-features = false } ], default-features = false }
futures = "0.3.31" futures = "0.3.31"
google_home = { path = "./google_home/google_home" }
google_home_macro = { path = "./google_home/google_home_macro" }
hostname = "0.4.1" hostname = "0.4.1"
indexmap = { version = "2.11.0", features = ["serde"] } indexmap = { version = "2.11.0", features = ["serde"] }
itertools = "0.14.0" itertools = "0.14.0"
json_value_merge = "2.0.1" json_value_merge = "2.0.1"
mlua = { version = "0.11.3", features = [
"lua54",
"vendored",
"macros",
"serialize",
"async",
"send",
] }
proc-macro2 = "1.0.101" proc-macro2 = "1.0.101"
quote = "1.0.40" quote = "1.0.40"
reqwest = { version = "0.12.23", features = [ reqwest = { version = "0.12.23", features = [
"json", "json",
"rustls-tls", "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.24.0"
serde = { version = "1.0.219", features = ["derive"] } serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.143" serde_json = "1.0.143"
serde_repr = "0.1.20" serde_repr = "0.1.20"
syn = { version = "2.0.106", features = ["extra-traits", "full"] } syn = { version = "2.0.106", features = ["extra-traits", "full"] }
thiserror = "2.0.16" thiserror = "2.0.16"
tokio = { version = "1", features = ["rt-multi-thread"] }
tokio-cron-scheduler = "0.14.0" tokio-cron-scheduler = "0.14.0"
tracing = "0.1.41"
tracing-subscriber = "0.3.20" tracing-subscriber = "0.3.20"
uuid = "1.18.1" uuid = "1.18.1"
wakey = "0.3.0" wakey = "0.3.0"
air_filter_types = { git = "https://git.huizinga.dev/Dreaded_X/airfilter", tag = "v0.4.4" }
[dependencies] [dependencies]
async-trait = { workspace = true }
automation_lib = { workspace = true }
automation_devices = { workspace = true }
google_home = { workspace = true }
mlua = { workspace = true }
tokio = { workspace = true }
hostname = { workspace = true }
rumqttc = { workspace = true }
axum = { workspace = true }
tracing = { workspace = true }
anyhow = { workspace = true } anyhow = { workspace = true }
async-trait = { workspace = true }
automation_devices = { workspace = true }
automation_lib = { workspace = true }
axum = { workspace = true }
dotenvy = { workspace = true } dotenvy = { workspace = true }
tracing-subscriber = { workspace = true } google_home = { workspace = true }
serde = { workspace = true } hostname = { workspace = true }
thiserror = { workspace = true } mlua = { workspace = true }
serde_json = { workspace = true }
reqwest = { workspace = true } reqwest = { workspace = true }
rumqttc = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
[patch.crates-io] [patch.crates-io]
wakey = { git = "https://git.huizinga.dev/Dreaded_X/wakey" } wakey = { git = "https://git.huizinga.dev/Dreaded_X/wakey" }

View File

@@ -3,6 +3,4 @@ name = "automation_cast"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]

View File

@@ -4,22 +4,22 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
air_filter_types = { workspace = true }
anyhow = { workspace = true }
async-trait = { workspace = true }
automation_lib = { workspace = true } automation_lib = { workspace = true }
automation_macro = { workspace = true } automation_macro = { workspace = true }
bytes = { workspace = true }
dyn-clone = { workspace = true }
eui48 = { workspace = true }
google_home = { workspace = true } google_home = { workspace = true }
mlua = { workspace = true } mlua = { workspace = true }
async-trait = { workspace = true } reqwest = { workspace = true }
dyn-clone = { workspace = true }
rumqttc = { workspace = true } rumqttc = { workspace = true }
tokio = { workspace = true }
serde_repr = { workspace = true }
tracing = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
reqwest = { workspace = true } # Use rustls, since the other packages also use rustls serde_json = { workspace = true }
anyhow = { workspace = true } serde_repr = { workspace = true }
bytes = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
eui48 = { workspace = true } tokio = { workspace = true }
tracing = { workspace = true }
wakey = { workspace = true } wakey = { workspace = true }
air_filter_types = { workspace = true }

View File

@@ -4,19 +4,19 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
async-trait = { workspace = true }
automation_cast = { workspace = true } automation_cast = { workspace = true }
bytes = { workspace = true }
dyn-clone = { workspace = true }
futures = { workspace = true }
google_home = { workspace = true } google_home = { workspace = true }
indexmap = { workspace = true }
mlua = { workspace = true }
rumqttc = { workspace = true } rumqttc = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
bytes = { workspace = true }
async-trait = { workspace = true }
futures = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
indexmap = { workspace = true } tokio = { workspace = true }
tokio-cron-scheduler = { workspace = true } tokio-cron-scheduler = { workspace = true }
mlua = { workspace = true } tracing = { workspace = true }
uuid = { workspace = true } uuid = { workspace = true }
dyn-clone = { workspace = true }

View File

@@ -1,9 +1,12 @@
local device_manager = require("device_manager")
local utils = require("utils")
print(_VERSION) print(_VERSION)
local host = automation.util.get_hostname() local host = utils.get_hostname()
print("Running @" .. host) print("Running @" .. host)
local debug, value = pcall(automation.util.get_env, "DEBUG") local debug, value = pcall(utils.get_env, "DEBUG")
if debug and value ~= "true" then if debug and value ~= "true" then
debug = false debug = false
end end
@@ -16,23 +19,23 @@ local function mqtt_automation(topic)
return "automation/" .. topic return "automation/" .. topic
end end
automation.fulfillment = { local fulfillment = {
openid_url = "https://login.huizinga.dev/api/oidc", openid_url = "https://login.huizinga.dev/api/oidc",
} }
local mqtt_client = automation.new_mqtt_client({ local mqtt_client = require("mqtt").new({
host = ((host == "zeus" or host == "hephaestus") and "olympus.lan.huizinga.dev") or "mosquitto", host = ((host == "zeus" or host == "hephaestus") and "olympus.lan.huizinga.dev") or "mosquitto",
port = 8883, port = 8883,
client_name = "automation-" .. host, client_name = "automation-" .. host,
username = "mqtt", username = "mqtt",
password = automation.util.get_env("MQTT_PASSWORD"), password = utils.get_env("MQTT_PASSWORD"),
tls = host == "zeus" or host == "hephaestus", tls = host == "zeus" or host == "hephaestus",
}) })
local ntfy = Ntfy.new({ local ntfy = Ntfy.new({
topic = automation.util.get_env("NTFY_TOPIC"), topic = utils.get_env("NTFY_TOPIC"),
}) })
automation.device_manager:add(ntfy) device_manager:add(ntfy)
local low_battery = {} local low_battery = {}
local function check_battery(device, battery) local function check_battery(device, battery)
@@ -44,7 +47,7 @@ local function check_battery(device, battery)
low_battery[id] = nil low_battery[id] = nil
end end
end end
automation.device_manager:schedule("0 0 21 */1 * *", function() device_manager:schedule("0 0 21 */1 * *", function()
-- 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")
@@ -82,7 +85,7 @@ local presence_system = Presence.new({
end end
end, end,
}) })
automation.device_manager:add(presence_system) device_manager:add(presence_system)
on_presence:add(function(presence) on_presence:add(function(presence)
ntfy:send_notification({ ntfy:send_notification({
title = "Presence", title = "Presence",
@@ -105,7 +108,7 @@ end)
on_presence:add(function(presence) on_presence:add(function(presence)
mqtt_client:send_message(mqtt_automation("debug") .. "/presence", { mqtt_client:send_message(mqtt_automation("debug") .. "/presence", {
state = presence, state = presence,
updated = automation.util.get_epoch(), updated = utils.get_epoch(),
}) })
end) end)
@@ -122,7 +125,7 @@ local on_light = {
self[#self + 1] = f self[#self + 1] = f
end, end,
} }
automation.device_manager:add(LightSensor.new({ device_manager:add(LightSensor.new({
identifier = "living_light_sensor", identifier = "living_light_sensor",
topic = mqtt_z2m("living/light"), topic = mqtt_z2m("living/light"),
client = mqtt_client, client = mqtt_client,
@@ -139,12 +142,12 @@ automation.device_manager:add(LightSensor.new({
on_light:add(function(light) on_light:add(function(light)
mqtt_client:send_message(mqtt_automation("debug") .. "/darkness", { mqtt_client:send_message(mqtt_automation("debug") .. "/darkness", {
state = not light, state = not light,
updated = automation.util.get_epoch(), updated = utils.get_epoch(),
}) })
end) end)
local hue_ip = "10.0.0.102" local hue_ip = "10.0.0.102"
local hue_token = automation.util.get_env("HUE_TOKEN") local hue_token = utils.get_env("HUE_TOKEN")
local hue_bridge = HueBridge.new({ local hue_bridge = HueBridge.new({
identifier = "hue_bridge", identifier = "hue_bridge",
@@ -155,7 +158,7 @@ local hue_bridge = HueBridge.new({
darkness = 43, darkness = 43,
}, },
}) })
automation.device_manager:add(hue_bridge) device_manager:add(hue_bridge)
on_light:add(function(light) on_light:add(function(light)
hue_bridge:set_flag("darkness", not light) hue_bridge:set_flag("darkness", not light)
end) end)
@@ -170,7 +173,7 @@ local kitchen_lights = HueGroup.new({
group_id = 7, group_id = 7,
scene_id = "7MJLG27RzeRAEVJ", scene_id = "7MJLG27RzeRAEVJ",
}) })
automation.device_manager:add(kitchen_lights) device_manager:add(kitchen_lights)
local living_lights = HueGroup.new({ local living_lights = HueGroup.new({
identifier = "living_lights", identifier = "living_lights",
ip = hue_ip, ip = hue_ip,
@@ -178,7 +181,7 @@ local living_lights = HueGroup.new({
group_id = 1, group_id = 1,
scene_id = "SNZw7jUhQ3cXSjkj", scene_id = "SNZw7jUhQ3cXSjkj",
}) })
automation.device_manager:add(living_lights) device_manager:add(living_lights)
local living_lights_relax = HueGroup.new({ local living_lights_relax = HueGroup.new({
identifier = "living_lights", identifier = "living_lights",
ip = hue_ip, ip = hue_ip,
@@ -186,9 +189,9 @@ local living_lights_relax = HueGroup.new({
group_id = 1, group_id = 1,
scene_id = "eRJ3fvGHCcb6yNw", scene_id = "eRJ3fvGHCcb6yNw",
}) })
automation.device_manager:add(living_lights_relax) device_manager:add(living_lights_relax)
automation.device_manager:add(HueSwitch.new({ device_manager:add(HueSwitch.new({
name = "Switch", name = "Switch",
room = "Living", room = "Living",
client = mqtt_client, client = mqtt_client,
@@ -205,7 +208,7 @@ automation.device_manager:add(HueSwitch.new({
battery_callback = check_battery, battery_callback = check_battery,
})) }))
automation.device_manager:add(WakeOnLAN.new({ device_manager:add(WakeOnLAN.new({
name = "Zeus", name = "Zeus",
room = "Living Room", room = "Living Room",
topic = mqtt_automation("appliance/living_room/zeus"), topic = mqtt_automation("appliance/living_room/zeus"),
@@ -221,7 +224,7 @@ local living_mixer = OutletOnOff.new({
client = mqtt_client, client = mqtt_client,
}) })
turn_off_when_away(living_mixer) turn_off_when_away(living_mixer)
automation.device_manager:add(living_mixer) device_manager:add(living_mixer)
local living_speakers = OutletOnOff.new({ local living_speakers = OutletOnOff.new({
name = "Speakers", name = "Speakers",
room = "Living Room", room = "Living Room",
@@ -229,9 +232,9 @@ local living_speakers = OutletOnOff.new({
client = mqtt_client, client = mqtt_client,
}) })
turn_off_when_away(living_speakers) turn_off_when_away(living_speakers)
automation.device_manager:add(living_speakers) device_manager:add(living_speakers)
automation.device_manager:add(IkeaRemote.new({ device_manager:add(IkeaRemote.new({
name = "Remote", name = "Remote",
room = "Living Room", room = "Living Room",
client = mqtt_client, client = mqtt_client,
@@ -280,13 +283,13 @@ local kettle = OutletPower.new({
callback = kettle_timeout(), callback = kettle_timeout(),
}) })
turn_off_when_away(kettle) turn_off_when_away(kettle)
automation.device_manager:add(kettle) device_manager:add(kettle)
local function set_kettle(_, on) local function set_kettle(_, on)
kettle:set_on(on) kettle:set_on(on)
end end
automation.device_manager:add(IkeaRemote.new({ device_manager:add(IkeaRemote.new({
name = "Remote", name = "Remote",
room = "Bedroom", room = "Bedroom",
client = mqtt_client, client = mqtt_client,
@@ -296,7 +299,7 @@ automation.device_manager:add(IkeaRemote.new({
battery_callback = check_battery, battery_callback = check_battery,
})) }))
automation.device_manager:add(IkeaRemote.new({ device_manager:add(IkeaRemote.new({
name = "Remote", name = "Remote",
room = "Kitchen", room = "Kitchen",
client = mqtt_client, client = mqtt_client,
@@ -327,9 +330,9 @@ local bathroom_light = LightOnOff.new({
client = mqtt_client, client = mqtt_client,
callback = off_timeout(debug and 60 or 45 * 60), callback = off_timeout(debug and 60 or 45 * 60),
}) })
automation.device_manager:add(bathroom_light) device_manager:add(bathroom_light)
automation.device_manager:add(Washer.new({ device_manager:add(Washer.new({
identifier = "bathroom_washer", identifier = "bathroom_washer",
topic = mqtt_z2m("bathroom/washer"), topic = mqtt_z2m("bathroom/washer"),
client = mqtt_client, client = mqtt_client,
@@ -344,7 +347,7 @@ automation.device_manager:add(Washer.new({
end, end,
})) }))
automation.device_manager:add(OutletOnOff.new({ device_manager:add(OutletOnOff.new({
name = "Charger", name = "Charger",
room = "Workbench", room = "Workbench",
topic = mqtt_z2m("workbench/charger"), topic = mqtt_z2m("workbench/charger"),
@@ -359,7 +362,7 @@ local workbench_outlet = OutletOnOff.new({
client = mqtt_client, client = mqtt_client,
}) })
turn_off_when_away(workbench_outlet) turn_off_when_away(workbench_outlet)
automation.device_manager:add(workbench_outlet) device_manager:add(workbench_outlet)
local workbench_light = LightColorTemperature.new({ local workbench_light = LightColorTemperature.new({
name = "Light", name = "Light",
@@ -368,10 +371,10 @@ local workbench_light = LightColorTemperature.new({
client = mqtt_client, client = mqtt_client,
}) })
turn_off_when_away(workbench_light) turn_off_when_away(workbench_light)
automation.device_manager:add(workbench_light) device_manager:add(workbench_light)
local delay_color_temp = Timeout.new() local delay_color_temp = Timeout.new()
automation.device_manager:add(IkeaRemote.new({ device_manager:add(IkeaRemote.new({
name = "Remote", name = "Remote",
room = "Workbench", room = "Workbench",
client = mqtt_client, client = mqtt_client,
@@ -401,7 +404,7 @@ local hallway_top_light = HueGroup.new({
group_id = 83, group_id = 83,
scene_id = "QeufkFDICEHWeKJ7", scene_id = "QeufkFDICEHWeKJ7",
}) })
automation.device_manager:add(HueSwitch.new({ device_manager:add(HueSwitch.new({
name = "SwitchBottom", name = "SwitchBottom",
room = "Hallway", room = "Hallway",
client = mqtt_client, client = mqtt_client,
@@ -411,7 +414,7 @@ automation.device_manager:add(HueSwitch.new({
end, end,
battery_callback = check_battery, battery_callback = check_battery,
})) }))
automation.device_manager:add(HueSwitch.new({ device_manager:add(HueSwitch.new({
name = "SwitchTop", name = "SwitchTop",
room = "Hallway", room = "Hallway",
client = mqtt_client, client = mqtt_client,
@@ -482,7 +485,7 @@ local hallway_storage = LightBrightness.new({
end, end,
}) })
turn_off_when_away(hallway_storage) turn_off_when_away(hallway_storage)
automation.device_manager:add(hallway_storage) device_manager:add(hallway_storage)
local hallway_bottom_lights = HueGroup.new({ local hallway_bottom_lights = HueGroup.new({
identifier = "hallway_bottom_lights", identifier = "hallway_bottom_lights",
@@ -491,7 +494,7 @@ local hallway_bottom_lights = HueGroup.new({
group_id = 81, group_id = 81,
scene_id = "3qWKxGVadXFFG4o", scene_id = "3qWKxGVadXFFG4o",
}) })
automation.device_manager:add(hallway_bottom_lights) device_manager:add(hallway_bottom_lights)
hallway_light_automation.group = { hallway_light_automation.group = {
set_on = function(on) set_on = function(on)
@@ -515,7 +518,7 @@ setmetatable(frontdoor_presence, {
if not presence_system:overall_presence() then if not presence_system:overall_presence() then
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), { mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), {
state = true, state = true,
updated = automation.util.get_epoch(), updated = utils.get_epoch(),
}) })
end end
else else
@@ -526,7 +529,7 @@ setmetatable(frontdoor_presence, {
end, end,
}) })
automation.device_manager:add(IkeaRemote.new({ device_manager:add(IkeaRemote.new({
name = "Remote", name = "Remote",
room = "Hallway", room = "Hallway",
client = mqtt_client, client = mqtt_client,
@@ -552,7 +555,7 @@ local hallway_frontdoor = ContactSensor.new({
end, end,
battery_callback = check_battery, battery_callback = check_battery,
}) })
automation.device_manager:add(hallway_frontdoor) device_manager:add(hallway_frontdoor)
hallway_light_automation.door = hallway_frontdoor hallway_light_automation.door = hallway_frontdoor
local hallway_trash = ContactSensor.new({ local hallway_trash = ContactSensor.new({
@@ -566,7 +569,7 @@ local hallway_trash = ContactSensor.new({
end, end,
battery_callback = check_battery, battery_callback = check_battery,
}) })
automation.device_manager:add(hallway_trash) device_manager:add(hallway_trash)
hallway_light_automation.trash = hallway_trash hallway_light_automation.trash = hallway_trash
local guest_light = LightOnOff.new({ local guest_light = LightOnOff.new({
@@ -576,14 +579,14 @@ local guest_light = LightOnOff.new({
client = mqtt_client, client = mqtt_client,
}) })
turn_off_when_away(guest_light) turn_off_when_away(guest_light)
automation.device_manager:add(guest_light) device_manager:add(guest_light)
local bedroom_air_filter = AirFilter.new({ local bedroom_air_filter = AirFilter.new({
name = "Air Filter", name = "Air Filter",
room = "Bedroom", room = "Bedroom",
url = "http://10.0.0.103", url = "http://10.0.0.103",
}) })
automation.device_manager:add(bedroom_air_filter) device_manager:add(bedroom_air_filter)
local bedroom_lights = HueGroup.new({ local bedroom_lights = HueGroup.new({
identifier = "bedroom_lights", identifier = "bedroom_lights",
@@ -592,7 +595,7 @@ local bedroom_lights = HueGroup.new({
group_id = 3, group_id = 3,
scene_id = "PvRs-lGD4VRytL9", scene_id = "PvRs-lGD4VRytL9",
}) })
automation.device_manager:add(bedroom_lights) device_manager:add(bedroom_lights)
local bedroom_lights_relax = HueGroup.new({ local bedroom_lights_relax = HueGroup.new({
identifier = "bedroom_lights", identifier = "bedroom_lights",
ip = hue_ip, ip = hue_ip,
@@ -600,9 +603,9 @@ local bedroom_lights_relax = HueGroup.new({
group_id = 3, group_id = 3,
scene_id = "60tfTyR168v2csz", scene_id = "60tfTyR168v2csz",
}) })
automation.device_manager:add(bedroom_lights_relax) device_manager:add(bedroom_lights_relax)
automation.device_manager:add(HueSwitch.new({ device_manager:add(HueSwitch.new({
name = "Switch", name = "Switch",
room = "Bedroom", room = "Bedroom",
client = mqtt_client, client = mqtt_client,
@@ -616,7 +619,7 @@ automation.device_manager:add(HueSwitch.new({
battery_callback = check_battery, battery_callback = check_battery,
})) }))
automation.device_manager:add(ContactSensor.new({ device_manager:add(ContactSensor.new({
name = "Balcony", name = "Balcony",
room = "Living Room", room = "Living Room",
sensor_type = "Door", sensor_type = "Door",
@@ -624,21 +627,21 @@ automation.device_manager:add(ContactSensor.new({
client = mqtt_client, client = mqtt_client,
battery_callback = check_battery, battery_callback = check_battery,
})) }))
automation.device_manager:add(ContactSensor.new({ device_manager:add(ContactSensor.new({
name = "Window", name = "Window",
room = "Living Room", room = "Living Room",
topic = mqtt_z2m("living/window"), topic = mqtt_z2m("living/window"),
client = mqtt_client, client = mqtt_client,
battery_callback = check_battery, battery_callback = check_battery,
})) }))
automation.device_manager:add(ContactSensor.new({ device_manager:add(ContactSensor.new({
name = "Window", name = "Window",
room = "Bedroom", room = "Bedroom",
topic = mqtt_z2m("bedroom/window"), topic = mqtt_z2m("bedroom/window"),
client = mqtt_client, client = mqtt_client,
battery_callback = check_battery, battery_callback = check_battery,
})) }))
automation.device_manager:add(ContactSensor.new({ device_manager:add(ContactSensor.new({
name = "Window", name = "Window",
room = "Guest Room", room = "Guest Room",
topic = mqtt_z2m("guest/window"), topic = mqtt_z2m("guest/window"),
@@ -653,9 +656,9 @@ local storage_light = LightBrightness.new({
client = mqtt_client, client = mqtt_client,
}) })
turn_off_when_away(storage_light) turn_off_when_away(storage_light)
automation.device_manager:add(storage_light) device_manager:add(storage_light)
automation.device_manager:add(ContactSensor.new({ device_manager:add(ContactSensor.new({
name = "Door", name = "Door",
room = "Storage", room = "Storage",
sensor_type = "Door", sensor_type = "Door",
@@ -671,9 +674,11 @@ automation.device_manager:add(ContactSensor.new({
battery_callback = check_battery, battery_callback = check_battery,
})) }))
automation.device_manager:schedule("0 0 19 * * *", function() device_manager:schedule("0 0 19 * * *", function()
bedroom_air_filter:set_on(true) bedroom_air_filter:set_on(true)
end) end)
automation.device_manager:schedule("0 0 20 * * *", function() device_manager:schedule("0 0 20 * * *", function()
bedroom_air_filter:set_on(false) bedroom_air_filter:set_on(false)
end) end)
return fulfillment

View File

@@ -3,15 +3,13 @@ name = "google_home"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
async-trait = { workspace = true }
automation_cast = { workspace = true } automation_cast = { workspace = true }
futures = { workspace = true }
google_home_macro = { workspace = true } google_home_macro = { workspace = true }
json_value_merge = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
tokio = { workspace = true } tokio = { workspace = true }
async-trait = { workspace = true }
futures = { workspace = true }
json_value_merge = { workspace = true }

View File

@@ -6,7 +6,6 @@ use std::path::Path;
use std::process; use std::process;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use anyhow::anyhow;
use automation_lib::config::{FulfillmentConfig, MqttConfig}; use automation_lib::config::{FulfillmentConfig, MqttConfig};
use automation_lib::device_manager::DeviceManager; use automation_lib::device_manager::DeviceManager;
use automation_lib::helpers; use automation_lib::helpers;
@@ -77,7 +76,6 @@ async fn app() -> anyhow::Result<()> {
// Setup the device handler // Setup the device handler
let device_manager = DeviceManager::new().await; let device_manager = DeviceManager::new().await;
let fulfillment_config = {
let lua = mlua::Lua::new(); let lua = mlua::Lua::new();
lua.set_warning_function(|_lua, text, _cont| { lua.set_warning_function(|_lua, text, _cont| {
@@ -118,9 +116,9 @@ async fn app() -> anyhow::Result<()> {
})?; })?;
lua.globals().set("print", print)?; lua.globals().set("print", print)?;
let automation = lua.create_table()?; let mqtt = lua.create_table()?;
let event_channel = device_manager.event_channel(); let event_channel = device_manager.event_channel();
let new_mqtt_client = lua.create_function(move |lua, config: mlua::Value| { let mqtt_new = lua.create_function(move |lua, config: mlua::Value| {
let config: MqttConfig = lua.from_value(config)?; let config: MqttConfig = lua.from_value(config)?;
// Create a mqtt client // Create a mqtt client
@@ -130,31 +128,31 @@ async fn app() -> anyhow::Result<()> {
Ok(WrappedAsyncClient(client)) Ok(WrappedAsyncClient(client))
})?; })?;
mqtt.set("new", mqtt_new)?;
lua.register_module("mqtt", mqtt)?;
automation.set("new_mqtt_client", new_mqtt_client)?; lua.register_module("device_manager", device_manager.clone())?;
automation.set("device_manager", device_manager.clone())?;
let util = lua.create_table()?; let utils = lua.create_table()?;
let get_env = lua.create_function(|_lua, name: String| { let get_env = lua.create_function(|_lua, name: String| {
std::env::var(name).map_err(mlua::ExternalError::into_lua_err) std::env::var(name).map_err(mlua::ExternalError::into_lua_err)
})?; })?;
util.set("get_env", get_env)?; utils.set("get_env", get_env)?;
let get_hostname = lua.create_function(|_lua, ()| { let get_hostname = lua.create_function(|_lua, ()| {
hostname::get() hostname::get()
.map(|name| name.to_str().unwrap_or("unknown").to_owned()) .map(|name| name.to_str().unwrap_or("unknown").to_owned())
.map_err(mlua::ExternalError::into_lua_err) .map_err(mlua::ExternalError::into_lua_err)
})?; })?;
util.set("get_hostname", get_hostname)?; utils.set("get_hostname", get_hostname)?;
let get_epoch = lua.create_function(|_lua, ()| { let get_epoch = lua.create_function(|_lua, ()| {
Ok(SystemTime::now() Ok(SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.expect("Time is after UNIX EPOCH") .expect("Time is after UNIX EPOCH")
.as_millis()) .as_millis())
})?; })?;
util.set("get_epoch", get_epoch)?; utils.set("get_epoch", get_epoch)?;
automation.set("util", util)?;
lua.globals().set("automation", automation)?; lua.register_module("utils", utils)?;
automation_devices::register_with_lua(&lua)?; automation_devices::register_with_lua(&lua)?;
helpers::register_with_lua(&lua)?; helpers::register_with_lua(&lua)?;
@@ -162,24 +160,9 @@ async fn app() -> anyhow::Result<()> {
// TODO: Make this not hardcoded // TODO: Make this not hardcoded
let config_filename = std::env::var("AUTOMATION_CONFIG").unwrap_or("./config.lua".into()); let config_filename = std::env::var("AUTOMATION_CONFIG").unwrap_or("./config.lua".into());
let config_path = Path::new(&config_filename); let config_path = Path::new(&config_filename);
match lua.load(config_path).exec_async().await {
Err(error) => {
println!("{error}");
Err(error)
}
result => result,
}?;
let automation: mlua::Table = lua.globals().get("automation")?; let fulfillment_config: mlua::Value = lua.load(config_path).eval_async().await?;
let fulfillment_config: Option<mlua::Value> = automation.get("fulfillment")?;
if let Some(fulfillment_config) = fulfillment_config {
let fulfillment_config: FulfillmentConfig = lua.from_value(fulfillment_config)?; let fulfillment_config: FulfillmentConfig = lua.from_value(fulfillment_config)?;
debug!("automation.fulfillment = {fulfillment_config:?}");
fulfillment_config
} else {
return Err(anyhow!("Fulfillment is not configured"));
}
};
// 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));