feat(config)!: Move device_manager out of global automation table into separate module
Moved `automation.device_manager` into a separate module called `device_manager`
This commit is contained in:
85
config.lua
85
config.lua
@@ -1,3 +1,4 @@
|
|||||||
|
local device_manager = require("device_manager")
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
|
|
||||||
print(_VERSION)
|
print(_VERSION)
|
||||||
@@ -34,7 +35,7 @@ local mqtt_client = automation.new_mqtt_client({
|
|||||||
local ntfy = Ntfy.new({
|
local ntfy = Ntfy.new({
|
||||||
topic = utils.get_env("NTFY_TOPIC"),
|
topic = utils.get_env("NTFY_TOPIC"),
|
||||||
})
|
})
|
||||||
automation.device_manager:add(ntfy)
|
device_manager:add(ntfy)
|
||||||
|
|
||||||
local low_battery = {}
|
local low_battery = {}
|
||||||
local function check_battery(device, battery)
|
local function check_battery(device, battery)
|
||||||
@@ -46,7 +47,7 @@ local function check_battery(device, battery)
|
|||||||
low_battery[id] = nil
|
low_battery[id] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
automation.device_manager:schedule("0 0 21 */1 * *", function()
|
device_manager:schedule("0 0 21 */1 * *", function()
|
||||||
-- Don't send notifications if there are now devices with low battery
|
-- Don't send notifications if there are now devices with low battery
|
||||||
if next(low_battery) == nil then
|
if next(low_battery) == nil then
|
||||||
print("No devices with low battery")
|
print("No devices with low battery")
|
||||||
@@ -84,7 +85,7 @@ local presence_system = Presence.new({
|
|||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
automation.device_manager:add(presence_system)
|
device_manager:add(presence_system)
|
||||||
on_presence:add(function(presence)
|
on_presence:add(function(presence)
|
||||||
ntfy:send_notification({
|
ntfy:send_notification({
|
||||||
title = "Presence",
|
title = "Presence",
|
||||||
@@ -124,7 +125,7 @@ local on_light = {
|
|||||||
self[#self + 1] = f
|
self[#self + 1] = f
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
automation.device_manager:add(LightSensor.new({
|
device_manager:add(LightSensor.new({
|
||||||
identifier = "living_light_sensor",
|
identifier = "living_light_sensor",
|
||||||
topic = mqtt_z2m("living/light"),
|
topic = mqtt_z2m("living/light"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -157,7 +158,7 @@ local hue_bridge = HueBridge.new({
|
|||||||
darkness = 43,
|
darkness = 43,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
automation.device_manager:add(hue_bridge)
|
device_manager:add(hue_bridge)
|
||||||
on_light:add(function(light)
|
on_light:add(function(light)
|
||||||
hue_bridge:set_flag("darkness", not light)
|
hue_bridge:set_flag("darkness", not light)
|
||||||
end)
|
end)
|
||||||
@@ -172,7 +173,7 @@ local kitchen_lights = HueGroup.new({
|
|||||||
group_id = 7,
|
group_id = 7,
|
||||||
scene_id = "7MJLG27RzeRAEVJ",
|
scene_id = "7MJLG27RzeRAEVJ",
|
||||||
})
|
})
|
||||||
automation.device_manager:add(kitchen_lights)
|
device_manager:add(kitchen_lights)
|
||||||
local living_lights = HueGroup.new({
|
local living_lights = HueGroup.new({
|
||||||
identifier = "living_lights",
|
identifier = "living_lights",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
@@ -180,7 +181,7 @@ local living_lights = HueGroup.new({
|
|||||||
group_id = 1,
|
group_id = 1,
|
||||||
scene_id = "SNZw7jUhQ3cXSjkj",
|
scene_id = "SNZw7jUhQ3cXSjkj",
|
||||||
})
|
})
|
||||||
automation.device_manager:add(living_lights)
|
device_manager:add(living_lights)
|
||||||
local living_lights_relax = HueGroup.new({
|
local living_lights_relax = HueGroup.new({
|
||||||
identifier = "living_lights",
|
identifier = "living_lights",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
@@ -188,9 +189,9 @@ local living_lights_relax = HueGroup.new({
|
|||||||
group_id = 1,
|
group_id = 1,
|
||||||
scene_id = "eRJ3fvGHCcb6yNw",
|
scene_id = "eRJ3fvGHCcb6yNw",
|
||||||
})
|
})
|
||||||
automation.device_manager:add(living_lights_relax)
|
device_manager:add(living_lights_relax)
|
||||||
|
|
||||||
automation.device_manager:add(HueSwitch.new({
|
device_manager:add(HueSwitch.new({
|
||||||
name = "Switch",
|
name = "Switch",
|
||||||
room = "Living",
|
room = "Living",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -207,7 +208,7 @@ automation.device_manager:add(HueSwitch.new({
|
|||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
automation.device_manager:add(WakeOnLAN.new({
|
device_manager:add(WakeOnLAN.new({
|
||||||
name = "Zeus",
|
name = "Zeus",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
topic = mqtt_automation("appliance/living_room/zeus"),
|
topic = mqtt_automation("appliance/living_room/zeus"),
|
||||||
@@ -223,7 +224,7 @@ local living_mixer = OutletOnOff.new({
|
|||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
})
|
})
|
||||||
turn_off_when_away(living_mixer)
|
turn_off_when_away(living_mixer)
|
||||||
automation.device_manager:add(living_mixer)
|
device_manager:add(living_mixer)
|
||||||
local living_speakers = OutletOnOff.new({
|
local living_speakers = OutletOnOff.new({
|
||||||
name = "Speakers",
|
name = "Speakers",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
@@ -231,9 +232,9 @@ local living_speakers = OutletOnOff.new({
|
|||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
})
|
})
|
||||||
turn_off_when_away(living_speakers)
|
turn_off_when_away(living_speakers)
|
||||||
automation.device_manager:add(living_speakers)
|
device_manager:add(living_speakers)
|
||||||
|
|
||||||
automation.device_manager:add(IkeaRemote.new({
|
device_manager:add(IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -282,13 +283,13 @@ local kettle = OutletPower.new({
|
|||||||
callback = kettle_timeout(),
|
callback = kettle_timeout(),
|
||||||
})
|
})
|
||||||
turn_off_when_away(kettle)
|
turn_off_when_away(kettle)
|
||||||
automation.device_manager:add(kettle)
|
device_manager:add(kettle)
|
||||||
|
|
||||||
local function set_kettle(_, on)
|
local function set_kettle(_, on)
|
||||||
kettle:set_on(on)
|
kettle:set_on(on)
|
||||||
end
|
end
|
||||||
|
|
||||||
automation.device_manager:add(IkeaRemote.new({
|
device_manager:add(IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Bedroom",
|
room = "Bedroom",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -298,7 +299,7 @@ automation.device_manager:add(IkeaRemote.new({
|
|||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
automation.device_manager:add(IkeaRemote.new({
|
device_manager:add(IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Kitchen",
|
room = "Kitchen",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -329,9 +330,9 @@ local bathroom_light = LightOnOff.new({
|
|||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
callback = off_timeout(debug and 60 or 45 * 60),
|
callback = off_timeout(debug and 60 or 45 * 60),
|
||||||
})
|
})
|
||||||
automation.device_manager:add(bathroom_light)
|
device_manager:add(bathroom_light)
|
||||||
|
|
||||||
automation.device_manager:add(Washer.new({
|
device_manager:add(Washer.new({
|
||||||
identifier = "bathroom_washer",
|
identifier = "bathroom_washer",
|
||||||
topic = mqtt_z2m("bathroom/washer"),
|
topic = mqtt_z2m("bathroom/washer"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -346,7 +347,7 @@ automation.device_manager:add(Washer.new({
|
|||||||
end,
|
end,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
automation.device_manager:add(OutletOnOff.new({
|
device_manager:add(OutletOnOff.new({
|
||||||
name = "Charger",
|
name = "Charger",
|
||||||
room = "Workbench",
|
room = "Workbench",
|
||||||
topic = mqtt_z2m("workbench/charger"),
|
topic = mqtt_z2m("workbench/charger"),
|
||||||
@@ -361,7 +362,7 @@ local workbench_outlet = OutletOnOff.new({
|
|||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
})
|
})
|
||||||
turn_off_when_away(workbench_outlet)
|
turn_off_when_away(workbench_outlet)
|
||||||
automation.device_manager:add(workbench_outlet)
|
device_manager:add(workbench_outlet)
|
||||||
|
|
||||||
local workbench_light = LightColorTemperature.new({
|
local workbench_light = LightColorTemperature.new({
|
||||||
name = "Light",
|
name = "Light",
|
||||||
@@ -370,10 +371,10 @@ local workbench_light = LightColorTemperature.new({
|
|||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
})
|
})
|
||||||
turn_off_when_away(workbench_light)
|
turn_off_when_away(workbench_light)
|
||||||
automation.device_manager:add(workbench_light)
|
device_manager:add(workbench_light)
|
||||||
|
|
||||||
local delay_color_temp = Timeout.new()
|
local delay_color_temp = Timeout.new()
|
||||||
automation.device_manager:add(IkeaRemote.new({
|
device_manager:add(IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Workbench",
|
room = "Workbench",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -403,7 +404,7 @@ local hallway_top_light = HueGroup.new({
|
|||||||
group_id = 83,
|
group_id = 83,
|
||||||
scene_id = "QeufkFDICEHWeKJ7",
|
scene_id = "QeufkFDICEHWeKJ7",
|
||||||
})
|
})
|
||||||
automation.device_manager:add(HueSwitch.new({
|
device_manager:add(HueSwitch.new({
|
||||||
name = "SwitchBottom",
|
name = "SwitchBottom",
|
||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -413,7 +414,7 @@ automation.device_manager:add(HueSwitch.new({
|
|||||||
end,
|
end,
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
automation.device_manager:add(HueSwitch.new({
|
device_manager:add(HueSwitch.new({
|
||||||
name = "SwitchTop",
|
name = "SwitchTop",
|
||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -484,7 +485,7 @@ local hallway_storage = LightBrightness.new({
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
turn_off_when_away(hallway_storage)
|
turn_off_when_away(hallway_storage)
|
||||||
automation.device_manager:add(hallway_storage)
|
device_manager:add(hallway_storage)
|
||||||
|
|
||||||
local hallway_bottom_lights = HueGroup.new({
|
local hallway_bottom_lights = HueGroup.new({
|
||||||
identifier = "hallway_bottom_lights",
|
identifier = "hallway_bottom_lights",
|
||||||
@@ -493,7 +494,7 @@ local hallway_bottom_lights = HueGroup.new({
|
|||||||
group_id = 81,
|
group_id = 81,
|
||||||
scene_id = "3qWKxGVadXFFG4o",
|
scene_id = "3qWKxGVadXFFG4o",
|
||||||
})
|
})
|
||||||
automation.device_manager:add(hallway_bottom_lights)
|
device_manager:add(hallway_bottom_lights)
|
||||||
|
|
||||||
hallway_light_automation.group = {
|
hallway_light_automation.group = {
|
||||||
set_on = function(on)
|
set_on = function(on)
|
||||||
@@ -528,7 +529,7 @@ setmetatable(frontdoor_presence, {
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
automation.device_manager:add(IkeaRemote.new({
|
device_manager:add(IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -554,7 +555,7 @@ local hallway_frontdoor = ContactSensor.new({
|
|||||||
end,
|
end,
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
})
|
})
|
||||||
automation.device_manager:add(hallway_frontdoor)
|
device_manager:add(hallway_frontdoor)
|
||||||
hallway_light_automation.door = hallway_frontdoor
|
hallway_light_automation.door = hallway_frontdoor
|
||||||
|
|
||||||
local hallway_trash = ContactSensor.new({
|
local hallway_trash = ContactSensor.new({
|
||||||
@@ -568,7 +569,7 @@ local hallway_trash = ContactSensor.new({
|
|||||||
end,
|
end,
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
})
|
})
|
||||||
automation.device_manager:add(hallway_trash)
|
device_manager:add(hallway_trash)
|
||||||
hallway_light_automation.trash = hallway_trash
|
hallway_light_automation.trash = hallway_trash
|
||||||
|
|
||||||
local guest_light = LightOnOff.new({
|
local guest_light = LightOnOff.new({
|
||||||
@@ -578,14 +579,14 @@ local guest_light = LightOnOff.new({
|
|||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
})
|
})
|
||||||
turn_off_when_away(guest_light)
|
turn_off_when_away(guest_light)
|
||||||
automation.device_manager:add(guest_light)
|
device_manager:add(guest_light)
|
||||||
|
|
||||||
local bedroom_air_filter = AirFilter.new({
|
local bedroom_air_filter = AirFilter.new({
|
||||||
name = "Air Filter",
|
name = "Air Filter",
|
||||||
room = "Bedroom",
|
room = "Bedroom",
|
||||||
url = "http://10.0.0.103",
|
url = "http://10.0.0.103",
|
||||||
})
|
})
|
||||||
automation.device_manager:add(bedroom_air_filter)
|
device_manager:add(bedroom_air_filter)
|
||||||
|
|
||||||
local bedroom_lights = HueGroup.new({
|
local bedroom_lights = HueGroup.new({
|
||||||
identifier = "bedroom_lights",
|
identifier = "bedroom_lights",
|
||||||
@@ -594,7 +595,7 @@ local bedroom_lights = HueGroup.new({
|
|||||||
group_id = 3,
|
group_id = 3,
|
||||||
scene_id = "PvRs-lGD4VRytL9",
|
scene_id = "PvRs-lGD4VRytL9",
|
||||||
})
|
})
|
||||||
automation.device_manager:add(bedroom_lights)
|
device_manager:add(bedroom_lights)
|
||||||
local bedroom_lights_relax = HueGroup.new({
|
local bedroom_lights_relax = HueGroup.new({
|
||||||
identifier = "bedroom_lights",
|
identifier = "bedroom_lights",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
@@ -602,9 +603,9 @@ local bedroom_lights_relax = HueGroup.new({
|
|||||||
group_id = 3,
|
group_id = 3,
|
||||||
scene_id = "60tfTyR168v2csz",
|
scene_id = "60tfTyR168v2csz",
|
||||||
})
|
})
|
||||||
automation.device_manager:add(bedroom_lights_relax)
|
device_manager:add(bedroom_lights_relax)
|
||||||
|
|
||||||
automation.device_manager:add(HueSwitch.new({
|
device_manager:add(HueSwitch.new({
|
||||||
name = "Switch",
|
name = "Switch",
|
||||||
room = "Bedroom",
|
room = "Bedroom",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
@@ -618,7 +619,7 @@ automation.device_manager:add(HueSwitch.new({
|
|||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
automation.device_manager:add(ContactSensor.new({
|
device_manager:add(ContactSensor.new({
|
||||||
name = "Balcony",
|
name = "Balcony",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
sensor_type = "Door",
|
sensor_type = "Door",
|
||||||
@@ -626,21 +627,21 @@ automation.device_manager:add(ContactSensor.new({
|
|||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
automation.device_manager:add(ContactSensor.new({
|
device_manager:add(ContactSensor.new({
|
||||||
name = "Window",
|
name = "Window",
|
||||||
room = "Living Room",
|
room = "Living Room",
|
||||||
topic = mqtt_z2m("living/window"),
|
topic = mqtt_z2m("living/window"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
automation.device_manager:add(ContactSensor.new({
|
device_manager:add(ContactSensor.new({
|
||||||
name = "Window",
|
name = "Window",
|
||||||
room = "Bedroom",
|
room = "Bedroom",
|
||||||
topic = mqtt_z2m("bedroom/window"),
|
topic = mqtt_z2m("bedroom/window"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
automation.device_manager:add(ContactSensor.new({
|
device_manager:add(ContactSensor.new({
|
||||||
name = "Window",
|
name = "Window",
|
||||||
room = "Guest Room",
|
room = "Guest Room",
|
||||||
topic = mqtt_z2m("guest/window"),
|
topic = mqtt_z2m("guest/window"),
|
||||||
@@ -655,9 +656,9 @@ local storage_light = LightBrightness.new({
|
|||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
})
|
})
|
||||||
turn_off_when_away(storage_light)
|
turn_off_when_away(storage_light)
|
||||||
automation.device_manager:add(storage_light)
|
device_manager:add(storage_light)
|
||||||
|
|
||||||
automation.device_manager:add(ContactSensor.new({
|
device_manager:add(ContactSensor.new({
|
||||||
name = "Door",
|
name = "Door",
|
||||||
room = "Storage",
|
room = "Storage",
|
||||||
sensor_type = "Door",
|
sensor_type = "Door",
|
||||||
@@ -673,10 +674,10 @@ automation.device_manager:add(ContactSensor.new({
|
|||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
automation.device_manager:schedule("0 0 19 * * *", function()
|
device_manager:schedule("0 0 19 * * *", function()
|
||||||
bedroom_air_filter:set_on(true)
|
bedroom_air_filter:set_on(true)
|
||||||
end)
|
end)
|
||||||
automation.device_manager:schedule("0 0 20 * * *", function()
|
device_manager:schedule("0 0 20 * * *", function()
|
||||||
bedroom_air_filter:set_on(false)
|
bedroom_air_filter:set_on(false)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
@@ -130,9 +130,10 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
automation.set("new_mqtt_client", new_mqtt_client)?;
|
automation.set("new_mqtt_client", new_mqtt_client)?;
|
||||||
automation.set("device_manager", device_manager.clone())?;
|
|
||||||
lua.globals().set("automation", automation)?;
|
lua.globals().set("automation", automation)?;
|
||||||
|
|
||||||
|
lua.register_module("device_manager", device_manager.clone())?;
|
||||||
|
|
||||||
let utils = lua.create_table()?;
|
let utils = lua.create_table()?;
|
||||||
let get_env = lua.create_function(|_lua, name: String| {
|
let get_env = lua.create_function(|_lua, name: String| {
|
||||||
std::env::var(name).map_err(mlua::ExternalError::into_lua_err)
|
std::env::var(name).map_err(mlua::ExternalError::into_lua_err)
|
||||||
|
|||||||
Reference in New Issue
Block a user