feat: WIP
Some checks failed
Build and deploy / Deploy container (push) Blocked by required conditions
Build and deploy / build (push) Has been cancelled

This commit is contained in:
2025-10-10 03:54:27 +02:00
parent ad8db00c8f
commit 6504b1239e
9 changed files with 461 additions and 17 deletions

View File

@@ -2,17 +2,21 @@ 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 or false
local debug = require("automation:variables").debug and true 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
@@ -35,7 +39,10 @@ local ntfy = devices.Ntfy.new({
})
device_manager:add(ntfy)
--- @type {[string]: number}
local low_battery = {}
--- @param device InterfaceDevice
--- @param battery number
local function check_battery(device, battery)
local id = device:get_id()
if battery < 15 then
@@ -66,11 +73,13 @@ device_manager:schedule("0 0 21 */1 * *", function()
})
end)
local on_presence = {
add = function(self, f)
self[#self + 1] = f
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 presence_system = devices.Presence.new({
topic = mqtt_automation("presence/+/#"),
@@ -110,11 +119,13 @@ on_presence:add(function(presence)
})
end)
local window_sensors = {
add = function(self, f)
self[#self + 1] = f
end,
}
--- @class WindowSensor
--- @field [integer] InterfaceOpenClose
local window_sensors = {}
--- @param sensor InterfaceOpenClose
function window_sensors:add(sensor)
self[#self + 1] = sensor
end
on_presence:add(function(presence)
if not presence then
local open = {}
@@ -139,6 +150,7 @@ on_presence:add(function(presence)
end
end)
--- @param device InterfaceOnOff
local function turn_off_when_away(device)
on_presence:add(function(presence)
if not presence then
@@ -147,11 +159,13 @@ local function turn_off_when_away(device)
end)
end
local on_light = {
add = function(self, f)
self[#self + 1] = f
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
device_manager:add(devices.LightSensor.new({
identifier = "living_light_sensor",
topic = mqtt_z2m("living/light"),
@@ -290,6 +304,8 @@ device_manager:add(devices.IkeaRemote.new({
local function kettle_timeout()
local timeout = utils.Timeout.new()
--- @param self InterfaceOnOff
--- @param state StatePower
return function(self, state)
if state.state and state.power < 100 then
timeout:start(3, function()
@@ -301,6 +317,7 @@ local function kettle_timeout()
end
end
--- @type OutletPower
local kettle = devices.OutletPower.new({
outlet_type = "Kettle",
name = "Kettle",
@@ -312,6 +329,7 @@ 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
@@ -336,9 +354,12 @@ device_manager:add(devices.IkeaRemote.new({
battery_callback = check_battery,
}))
--- @param duration number
local function off_timeout(duration)
local timeout = utils.Timeout.new()
--- @param self InterfaceOnOff
--- @param state {state: boolean}
return function(self, state)
if state.state then
timeout:start(duration, function()