Compare commits
8 Commits
2f181e35f3
...
a2e65c2d1a
| Author | SHA1 | Date | |
|---|---|---|---|
|
a2e65c2d1a
|
|||
|
e26fd9f132
|
|||
|
eb0c80c4ce
|
|||
|
352654107a
|
|||
|
3a7f2f9bd7
|
|||
|
3be11b0c6a
|
|||
|
e880efe4cf
|
|||
|
e2fb680cd6
|
@@ -28,7 +28,13 @@ impl mlua::UserData for WrappedAsyncClient {
|
|||||||
methods.add_async_method(
|
methods.add_async_method(
|
||||||
"send_message",
|
"send_message",
|
||||||
async |_lua, this, (topic, message): (String, mlua::Value)| {
|
async |_lua, this, (topic, message): (String, mlua::Value)| {
|
||||||
let message = serde_json::to_string(&message).unwrap();
|
// serde_json converts nil => "null", but we actually want nil to send an empty
|
||||||
|
// message
|
||||||
|
let message = if message.is_nil() {
|
||||||
|
"".into()
|
||||||
|
} else {
|
||||||
|
serde_json::to_string(&message).unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
debug!("message = {message}");
|
debug!("message = {message}");
|
||||||
|
|
||||||
|
|||||||
59
config.lua
59
config.lua
@@ -425,12 +425,15 @@ device_manager:add(HueSwitch.new({
|
|||||||
local hallway_light_automation = {
|
local hallway_light_automation = {
|
||||||
timeout = Timeout.new(),
|
timeout = Timeout.new(),
|
||||||
forced = false,
|
forced = false,
|
||||||
switch_callback = function(self, on)
|
switch_callback = function(self)
|
||||||
|
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,
|
||||||
door_callback = function(self, open)
|
door_callback = function(self)
|
||||||
|
return function(_, open)
|
||||||
if open then
|
if open then
|
||||||
self.timeout:cancel()
|
self.timeout:cancel()
|
||||||
|
|
||||||
@@ -442,8 +445,10 @@ local hallway_light_automation = {
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
trash_callback = function(self, open)
|
trash_callback = function(self)
|
||||||
|
return function(_, open)
|
||||||
if open then
|
if open then
|
||||||
self.group.set_on(true)
|
self.group.set_on(true)
|
||||||
else
|
else
|
||||||
@@ -455,20 +460,23 @@ local hallway_light_automation = {
|
|||||||
self.group.set_on(false)
|
self.group.set_on(false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
light_callback = function(self, on)
|
light_callback = function(self)
|
||||||
|
return function(_, state)
|
||||||
if
|
if
|
||||||
on
|
state.on
|
||||||
and (self.trash == nil or self.trash:open_percent()) == 0
|
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)
|
||||||
then
|
then
|
||||||
-- If the door and trash are not open, that means the light got turned on manually
|
-- If the door and trash are not open, that means the light got turned on manually
|
||||||
self.timeout:cancel()
|
self.timeout:cancel()
|
||||||
self.forced = true
|
self.forced = true
|
||||||
elseif not on then
|
elseif not state.on then
|
||||||
-- The light is never forced when it is off
|
-- The light is never forced when it is off
|
||||||
self.forced = false
|
self.forced = false
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -477,9 +485,7 @@ local hallway_storage = LightBrightness.new({
|
|||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
topic = mqtt_z2m("hallway/storage"),
|
topic = mqtt_z2m("hallway/storage"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
callback = function(_, state)
|
callback = hallway_light_automation:light_callback(),
|
||||||
hallway_light_automation:light_callback(state.state)
|
|
||||||
end,
|
|
||||||
})
|
})
|
||||||
turn_off_when_away(hallway_storage)
|
turn_off_when_away(hallway_storage)
|
||||||
device_manager:add(hallway_storage)
|
device_manager:add(hallway_storage)
|
||||||
@@ -504,13 +510,12 @@ hallway_light_automation.group = {
|
|||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
local frontdoor_presence = {
|
local function presence(duration)
|
||||||
timeout = Timeout.new(),
|
local timeout = Timeout.new()
|
||||||
}
|
|
||||||
setmetatable(frontdoor_presence, {
|
return function(_, open)
|
||||||
__call = function(self, open)
|
|
||||||
if open then
|
if open then
|
||||||
self.timeout:cancel()
|
timeout:cancel()
|
||||||
|
|
||||||
if not presence_system:overall_presence() then
|
if not presence_system:overall_presence() then
|
||||||
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), {
|
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), {
|
||||||
@@ -519,21 +524,19 @@ setmetatable(frontdoor_presence, {
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
self.timeout:start(debug and 10 or 15 * 60, function()
|
timeout:start(duration, function()
|
||||||
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), {})
|
mqtt_client:send_message(mqtt_automation("presence/contact/frontdoor"), nil)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end,
|
end
|
||||||
})
|
end
|
||||||
|
|
||||||
device_manager:add(IkeaRemote.new({
|
device_manager:add(IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Hallway",
|
room = "Hallway",
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
topic = mqtt_z2m("hallway/remote"),
|
topic = mqtt_z2m("hallway/remote"),
|
||||||
callback = function(_, on)
|
callback = hallway_light_automation:switch_callback(),
|
||||||
hallway_light_automation:switch_callback(on)
|
|
||||||
end,
|
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
}))
|
}))
|
||||||
local hallway_frontdoor = ContactSensor.new({
|
local hallway_frontdoor = ContactSensor.new({
|
||||||
@@ -546,10 +549,10 @@ local hallway_frontdoor = ContactSensor.new({
|
|||||||
topic = mqtt_automation("presence/contact/frontdoor"),
|
topic = mqtt_automation("presence/contact/frontdoor"),
|
||||||
timeout = debug and 10 or 15 * 60,
|
timeout = debug and 10 or 15 * 60,
|
||||||
},
|
},
|
||||||
callback = function(_, open)
|
callback = {
|
||||||
hallway_light_automation:door_callback(open)
|
presence(debug and 10 or 15 * 60),
|
||||||
frontdoor_presence(open)
|
hallway_light_automation:door_callback(),
|
||||||
end,
|
},
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
})
|
})
|
||||||
device_manager:add(hallway_frontdoor)
|
device_manager:add(hallway_frontdoor)
|
||||||
@@ -561,9 +564,7 @@ local hallway_trash = ContactSensor.new({
|
|||||||
sensor_type = "Drawer",
|
sensor_type = "Drawer",
|
||||||
topic = mqtt_z2m("hallway/trash"),
|
topic = mqtt_z2m("hallway/trash"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
callback = function(_, open)
|
callback = hallway_light_automation:trash_callback(),
|
||||||
hallway_light_automation:trash_callback(open)
|
|
||||||
end,
|
|
||||||
battery_callback = check_battery,
|
battery_callback = check_battery,
|
||||||
})
|
})
|
||||||
device_manager:add(hallway_trash)
|
device_manager:add(hallway_trash)
|
||||||
|
|||||||
Reference in New Issue
Block a user