Compare commits
2 Commits
a95574b731
...
b14387ce3c
| Author | SHA1 | Date | |
|---|---|---|---|
|
b14387ce3c
|
|||
|
f0fd110ff8
|
82
config.lua
82
config.lua
@@ -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 = {}
|
|
||||||
--- @param f fun(presence: boolean)
|
|
||||||
function on_presence:add(f)
|
|
||||||
self[#self + 1] = f
|
self[#self + 1] = f
|
||||||
end
|
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 = {}
|
|
||||||
--- @param f fun(light: boolean)
|
|
||||||
function on_light:add(f)
|
|
||||||
self[#self + 1] = f
|
self[#self + 1] = f
|
||||||
end
|
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,19 +455,14 @@ 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 fun(_, on: boolean)
|
|
||||||
function hallway_light_automation:switch_callback()
|
|
||||||
return function(_, on)
|
return function(_, on)
|
||||||
self.timeout:cancel()
|
self.timeout:cancel()
|
||||||
self.group.set_on(on)
|
self.group.set_on(on)
|
||||||
self.forced = on
|
self.forced = on
|
||||||
end
|
end
|
||||||
end
|
end,
|
||||||
---@return fun(_, open: boolean)
|
door_callback = function(self)
|
||||||
function hallway_light_automation:door_callback()
|
|
||||||
return function(_, open)
|
return function(_, open)
|
||||||
if open then
|
if open then
|
||||||
self.timeout:cancel()
|
self.timeout:cancel()
|
||||||
@@ -507,9 +476,8 @@ function hallway_light_automation:door_callback()
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end,
|
||||||
---@return fun(_, open: boolean)
|
trash_callback = function(self)
|
||||||
function hallway_light_automation:trash_callback()
|
|
||||||
return function(_, open)
|
return function(_, open)
|
||||||
if open then
|
if open then
|
||||||
self.group.set_on(true)
|
self.group.set_on(true)
|
||||||
@@ -523,9 +491,8 @@ function hallway_light_automation:trash_callback()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end,
|
||||||
---@return fun(_, state: { on: boolean })
|
light_callback = function(self)
|
||||||
function hallway_light_automation:light_callback()
|
|
||||||
return function(_, state)
|
return function(_, state)
|
||||||
if
|
if
|
||||||
state.on
|
state.on
|
||||||
@@ -540,7 +507,8 @@ function hallway_light_automation:light_callback()
|
|||||||
self.forced = false
|
self.forced = false
|
||||||
end
|
end
|
||||||
end
|
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()
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
|
||||||
Reference in New Issue
Block a user