diff --git a/apps/controller.py b/apps/controller.py index a3dec6b..f4a3708 100644 --- a/apps/controller.py +++ b/apps/controller.py @@ -24,22 +24,23 @@ class ControllerEntities(Hass): self.entities = [self.get_entity(e) for e in self.entities] - self.log(f'Initialized controller for {[e.friendly_name for e in self.entities]}') - @dataclass(init=False) class ControllerRoomLights(ControllerEntities): def initialize(self): super().initialize() + self.log(f'Initialized light controller for {[e.friendly_name for e in self.entities]}') self.register_service(f'{self.name}/activate', self.activate) self.register_service(f'{self.name}/deactivate', self.deactivate) def activate(self, namespace: str = None, domain: str = None, service=None, kwargs=None): + self.log(self.entities) for entity in self.entities: self.log(f'Turning on {entity.name}') entity.turn_on() def deactivate(self, namespace: str = None, domain: str = None, service=None, kwargs=None): + self.log(self.entities) for entity in self.entities: self.log(f'Turning off {entity.name}') entity.turn_off() @@ -69,7 +70,8 @@ class ControllerMotion(ControllerEntities): self.off_duration = timedelta() self.sync_state() - self.listen_state(self.sync_state, [e.entity_id for e in self.entities]) + self.listen_motion_off() + self.listen_motion_on() @property def current_state(self) -> bool: @@ -84,17 +86,14 @@ class ControllerMotion(ControllerEntities): self.log(f'Syncing state, current state: {self.current_state}') if self.current_state: self.room.activate() - self.listen_motion_off() else: self.room.deactivate() - self.listen_motion_on() def listen_motion_on(self): self.listen_state( callback=self.callback_motion_on, entity_id=[e.entity_id for e in self.entities], new='on', - oneshot=True ) self.log(f'Waiting for motion on {[e.friendly_name for e in self.entities]} to turn on room {self.room.name}') @@ -104,7 +103,6 @@ class ControllerMotion(ControllerEntities): entity_id=[e.entity_id for e in self.entities], new='off', duration=self.off_duration.total_seconds(), - oneshot=True ) self.log(f'Waiting for motion off {[e.friendly_name for e in self.entities]} for {self.off_duration}') @@ -160,10 +158,14 @@ class ControllerDaylight(ControllerEntities): resolution=500 ) self.log(self.adjuster) + self.listen_state(callback=self.handle_state_change, entity_id=[e.entity_id for e in self.entities]) self.log(f'Listening for state {[e.friendly_name for e in self.entities]}') - def handle_state_change(self, entity, attribute, old, new, kwargs): + for entity in self.entities: + self.handle_state_change(entity=entity, new=entity.get_state()) + + def handle_state_change(self, entity=None, attribute=None, old=None, new=None, kwargs=None): if new == 'on': self.adjustment_handle = self.run_every( callback=self.ongoing_adjustment, @@ -173,8 +175,9 @@ class ControllerDaylight(ControllerEntities): ) self.log(f'Started adjustments') else: - self.cancel_timer(self.adjustment_handle) - self.log(f'Cancelled adjustments') + if hasattr(self, 'adjustment_handle'): + self.cancel_timer(self.adjustment_handle) + self.log(f'Cancelled adjustments') def matching_state(self, entity_id: str): state = self.get_state(entity_id=entity_id, attribute='all')['attributes']