feat: Added type annotations to config.lua
In some instances this required some restructuring of the code to be able to properly add the annotations.
This commit is contained in:
156
config.lua
156
config.lua
@@ -2,17 +2,21 @@ 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 or false
|
local debug = require("automation:variables").debug and true 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
|
||||||
@@ -30,12 +34,19 @@ 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 = secrets.ntfy_topic,
|
topic = 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
|
||||||
@@ -66,11 +77,13 @@ device_manager:schedule("0 0 21 */1 * *", function()
|
|||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local on_presence = {
|
--- @class OnPresence
|
||||||
add = function(self, f)
|
--- @field [integer] fun(presence: boolean)
|
||||||
self[#self + 1] = f
|
local on_presence = {}
|
||||||
end,
|
--- @param f fun(presence: boolean)
|
||||||
}
|
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/+/#"),
|
||||||
@@ -110,11 +123,13 @@ on_presence:add(function(presence)
|
|||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local window_sensors = {
|
--- @class WindowSensor
|
||||||
add = function(self, f)
|
--- @field [integer] OpenCloseInterface
|
||||||
self[#self + 1] = f
|
local window_sensors = {}
|
||||||
end,
|
--- @param sensor OpenCloseInterface
|
||||||
}
|
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 = {}
|
||||||
@@ -139,6 +154,7 @@ 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
|
||||||
@@ -147,11 +163,13 @@ local function turn_off_when_away(device)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
local on_light = {
|
--- @class OnLight
|
||||||
add = function(self, f)
|
--- @field [integer] fun(light: boolean)
|
||||||
self[#self + 1] = f
|
local on_light = {}
|
||||||
end,
|
--- @param f fun(light: boolean)
|
||||||
}
|
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"),
|
||||||
@@ -175,6 +193,9 @@ 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",
|
||||||
@@ -287,6 +308,7 @@ 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()
|
||||||
|
|
||||||
@@ -301,6 +323,7 @@ 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",
|
||||||
@@ -312,6 +335,7 @@ 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
|
||||||
@@ -336,6 +360,8 @@ 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()
|
||||||
|
|
||||||
@@ -455,60 +481,66 @@ 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,
|
||||||
switch_callback = function(self)
|
trash = nil,
|
||||||
return function(_, on)
|
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
|
||||||
self.timeout:cancel()
|
self.timeout:cancel()
|
||||||
self.group.set_on(on)
|
|
||||||
self.forced = on
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
door_callback = function(self)
|
|
||||||
return function(_, open)
|
|
||||||
if open then
|
|
||||||
self.timeout:cancel()
|
|
||||||
|
|
||||||
self.group.set_on(true)
|
self.group.set_on(true)
|
||||||
elseif not self.forced then
|
elseif not self.forced then
|
||||||
self.timeout:start(debug and 10 or 2 * 60, function()
|
self.timeout:start(debug and 10 or 2 * 60, function()
|
||||||
if self.trash == nil or self.trash:open_percent() == 0 then
|
if self.trash == nil or self.trash:open_percent() == 0 then
|
||||||
self.group.set_on(false)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
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)
|
self.group.set_on(false)
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
end
|
end
|
||||||
end,
|
end
|
||||||
light_callback = function(self)
|
end
|
||||||
return function(_, state)
|
---@return fun(_, open: boolean)
|
||||||
|
function hallway_light_automation:trash_callback()
|
||||||
|
return function(_, open)
|
||||||
|
if open then
|
||||||
|
self.group.set_on(true)
|
||||||
|
else
|
||||||
if
|
if
|
||||||
state.on
|
not self.timeout:is_waiting()
|
||||||
and (self.trash == nil or self.trash:open_percent()) == 0
|
|
||||||
and (self.door == nil or self.door:open_percent() == 0)
|
and (self.door == nil or self.door:open_percent() == 0)
|
||||||
|
and not self.forced
|
||||||
then
|
then
|
||||||
-- If the door and trash are not open, that means the light got turned on manually
|
self.group.set_on(false)
|
||||||
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
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local hallway_storage = devices.LightBrightness.new({
|
local hallway_storage = devices.LightBrightness.new({
|
||||||
name = "Storage",
|
name = "Storage",
|
||||||
@@ -540,6 +572,8 @@ 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()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user