Compare commits

..

2 Commits

Author SHA1 Message Date
b14387ce3c chore: Reordered pre-commit hooks
All checks were successful
Build and deploy / build (push) Successful in 11m27s
Build and deploy / Deploy container (push) Has been skipped
2025-10-15 04:04:31 +02:00
f0fd110ff8 feat: Added lua definition files
Also added a pre-commit hook to ensure that the definitions files are
up-to-date.
2025-10-15 04:03:28 +02:00
2 changed files with 66 additions and 112 deletions

View File

@@ -2,21 +2,17 @@ local devices = require("automation:devices")
local device_manager = require("automation:device_manager")
local utils = require("automation:utils")
local secrets = require("automation:secrets")
local debug = require("automation:variables").debug and true or false
local debug = require("automation:variables").debug or false
print(_VERSION)
local host = utils.get_hostname()
print("Running @" .. host)
--- @param topic string
--- @return string
local function mqtt_z2m(topic)
return "zigbee2mqtt/" .. topic
end
--- @param topic string
--- @return string
local function mqtt_automation(topic)
return "automation/" .. topic
end
@@ -34,19 +30,12 @@ local mqtt_client = require("automation:mqtt").new(device_manager, {
tls = host == "zeus" or host == "hephaestus",
})
local ntfy_topic = secrets.ntfy_topic
if ntfy_topic == nil then
error("Ntfy topic is not specified")
end
local ntfy = devices.Ntfy.new({
topic = ntfy_topic,
topic = secrets.ntfy_topic,
})
device_manager:add(ntfy)
--- @type {[string]: number}
local low_battery = {}
--- @param device DeviceInterface
--- @param battery number
local function check_battery(device, battery)
local id = device:get_id()
if battery < 15 then
@@ -77,13 +66,11 @@ device_manager:schedule("0 0 21 */1 * *", function()
})
end)
--- @class OnPresence
--- @field [integer] fun(presence: boolean)
local on_presence = {}
--- @param f fun(presence: boolean)
function on_presence:add(f)
self[#self + 1] = f
end
local on_presence = {
add = function(self, f)
self[#self + 1] = f
end,
}
local presence_system = devices.Presence.new({
topic = mqtt_automation("presence/+/#"),
@@ -123,13 +110,11 @@ on_presence:add(function(presence)
})
end)
--- @class WindowSensor
--- @field [integer] OpenCloseInterface
local window_sensors = {}
--- @param sensor OpenCloseInterface
function window_sensors:add(sensor)
self[#self + 1] = sensor
end
local window_sensors = {
add = function(self, f)
self[#self + 1] = f
end,
}
on_presence:add(function(presence)
if not presence then
local open = {}
@@ -154,7 +139,6 @@ on_presence:add(function(presence)
end
end)
--- @param device OnOffInterface
local function turn_off_when_away(device)
on_presence:add(function(presence)
if not presence then
@@ -163,13 +147,11 @@ local function turn_off_when_away(device)
end)
end
--- @class OnLight
--- @field [integer] fun(light: boolean)
local on_light = {}
--- @param f fun(light: boolean)
function on_light:add(f)
self[#self + 1] = f
end
local on_light = {
add = function(self, f)
self[#self + 1] = f
end,
}
device_manager:add(devices.LightSensor.new({
identifier = "living_light_sensor",
topic = mqtt_z2m("living/light"),
@@ -193,9 +175,6 @@ end)
local hue_ip = "10.0.0.102"
local hue_token = secrets.hue_token
if hue_token == nil then
error("Hue token is not specified")
end
local hue_bridge = devices.HueBridge.new({
identifier = "hue_bridge",
@@ -308,7 +287,6 @@ device_manager:add(devices.IkeaRemote.new({
battery_callback = check_battery,
}))
--- @return fun(self: OnOffInterface, state: {state: boolean, power: number})
local function kettle_timeout()
local timeout = utils.Timeout.new()
@@ -323,7 +301,6 @@ local function kettle_timeout()
end
end
--- @type OutletPower
local kettle = devices.OutletPower.new({
outlet_type = "Kettle",
name = "Kettle",
@@ -335,7 +312,6 @@ local kettle = devices.OutletPower.new({
turn_off_when_away(kettle)
device_manager:add(kettle)
--- @param on boolean
local function set_kettle(_, on)
kettle:set_on(on)
end
@@ -360,8 +336,6 @@ device_manager:add(devices.IkeaRemote.new({
battery_callback = check_battery,
}))
--- @param duration number
--- @return fun(self: OnOffInterface, state: {state: boolean})
local function off_timeout(duration)
local timeout = utils.Timeout.new()
@@ -481,66 +455,60 @@ device_manager:add(devices.HueSwitch.new({
local hallway_light_automation = {
timeout = utils.Timeout.new(),
forced = false,
trash = nil,
door = nil,
}
---@return fun(_, on: boolean)
function hallway_light_automation:switch_callback()
return function(_, on)
self.timeout:cancel()
self.group.set_on(on)
self.forced = on
end
end
---@return fun(_, open: boolean)
function hallway_light_automation:door_callback()
return function(_, open)
if open then
switch_callback = function(self)
return function(_, on)
self.timeout:cancel()
self.group.set_on(true)
elseif not self.forced then
self.timeout:start(debug and 10 or 2 * 60, function()
if self.trash == nil or self.trash:open_percent() == 0 then
self.group.set_on(false)
end
end)
self.group.set_on(on)
self.forced = on
end
end
end
---@return fun(_, open: boolean)
function hallway_light_automation:trash_callback()
return function(_, open)
if open then
self.group.set_on(true)
else
if
not self.timeout:is_waiting()
and (self.door == nil or self.door:open_percent() == 0)
and not self.forced
then
self.group.set_on(false)
end,
door_callback = function(self)
return function(_, open)
if open then
self.timeout:cancel()
self.group.set_on(true)
elseif not self.forced then
self.timeout:start(debug and 10 or 2 * 60, function()
if self.trash == nil or self.trash:open_percent() == 0 then
self.group.set_on(false)
end
end)
end
end
end
end
---@return fun(_, state: { on: boolean })
function hallway_light_automation:light_callback()
return function(_, state)
if
state.on
and (self.trash == nil or self.trash:open_percent()) == 0
and (self.door == nil or self.door:open_percent() == 0)
then
-- If the door and trash are not open, that means the light got turned on manually
self.timeout:cancel()
self.forced = true
elseif not state.on then
-- The light is never forced when it is off
self.forced = false
end,
trash_callback = function(self)
return function(_, open)
if open then
self.group.set_on(true)
else
if
not self.timeout:is_waiting()
and (self.door == nil or self.door:open_percent() == 0)
and not self.forced
then
self.group.set_on(false)
end
end
end
end
end
end,
light_callback = function(self)
return function(_, state)
if
state.on
and (self.trash == nil or self.trash:open_percent()) == 0
and (self.door == nil or self.door:open_percent() == 0)
then
-- If the door and trash are not open, that means the light got turned on manually
self.timeout:cancel()
self.forced = true
elseif not state.on then
-- The light is never forced when it is off
self.forced = false
end
end
end,
}
local hallway_storage = devices.LightBrightness.new({
name = "Storage",
@@ -572,8 +540,6 @@ hallway_light_automation.group = {
end,
}
---@param duration number
---@return fun(_, open: boolean)
local function presence(duration)
local timeout = utils.Timeout.new()

View File

@@ -1,12 +0,0 @@
---@meta
---@class DeviceManager
local DeviceManager
---@param device DeviceInterface
function DeviceManager:add(device) end
---@param cron string
---@param callback fun()
function DeviceManager:schedule(cron, callback) end
return DeviceManager