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