Created
May 23, 2023 14:27
-
-
Save lrodorigo/5a69776e8679b8b4011a15e054a2890b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from base_handler import * | |
class StateToAlarmHandler(BaseHandler): | |
def initialize(self): | |
self.log(f"StateToAlarmHandler Handler") | |
self.on_entering = { | |
AT_HOME: [], | |
AWAY: [self. activate_alarm_away, self.turn_off_all_lights], | |
SLEEP: [self.activate_alarm_sleep, self.turn_off_all_lights], | |
HOLIDAY: [self. activate_alarm_away, self.turn_off_all_lights] | |
} | |
self.listen_state(self.on_alarm_state_change, ALARMO_CONTROL_PANEL) | |
self.register_house_mode_change() | |
def turn_off_all_lights(self,old_state): | |
self.log("Turning off all lights!") | |
[(self.call_service("light/turn_off", **{"entity_id": x}), self.log(x)) for x in self.get_state("light")] | |
def activate_alarm_sleep(self,old_state): | |
ret = self.call_service("alarmo/arm", **{"entity_id": ALARMO_CONTROL_PANEL, "mode": "night"}) | |
if ret is not None and len(ret) < 1 or ret[0]["state"] != "arming": | |
self.move_to(old_state) | |
return False | |
def activate_alarm_away(self,old_state): | |
self.log(self.list_services()) | |
ret = self.call_service("alarmo/arm", **{"entity_id": ALARMO_CONTROL_PANEL, "mode": "away"}) | |
if ret is not None and len(ret) < 1 or ret[0]["state"] != "arming": | |
self.move_to(old_state) | |
return False | |
def on_alarm_state_change(self, entity, attribute, old, new, kwargs): | |
if new == 'disarmed': | |
self.move_to(AT_HOME) | |
def on_house_mode_changed(self, entity, attribute, old, new, kwargs): | |
for x in self.on_entering.get(new): | |
self.log("OLD STATE : %s" % old) | |
ret = x(old) | |
if ret is not None and (not ret): | |
self.log("BREEEAK") | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment