diff --git a/apps/controller.py b/apps/controller.py index 323822b..a3dec6b 100644 --- a/apps/controller.py +++ b/apps/controller.py @@ -9,7 +9,7 @@ from daylight_adjuster import DaylightAdjuster @dataclass(init=False) -class ControllerBase(Hass): +class ControllerEntities(Hass): entities: List[Entity] def initialize(self): @@ -17,7 +17,7 @@ class ControllerBase(Hass): for arg, val in self.args.items(): if arg not in ['class', 'module']: setattr(self, arg, val) - self.log(f'Set {arg} to {val}') + # self.log(f'Set {arg} to {val}') for entity in self.entities: assert self.entity_exists(entity), f'{entity} does not exist' @@ -28,7 +28,7 @@ class ControllerBase(Hass): @dataclass(init=False) -class ControllerRoom(ControllerBase): +class ControllerRoomLights(ControllerEntities): def initialize(self): super().initialize() self.register_service(f'{self.name}/activate', self.activate) @@ -50,8 +50,8 @@ class ControllerRoom(ControllerBase): @dataclass(init=False) -class ControllerMotion(ControllerBase): - room: ControllerRoom +class ControllerMotion(ControllerEntities): + room: ControllerRoomLights off_duration: timedelta def initialize(self): @@ -59,7 +59,7 @@ class ControllerMotion(ControllerBase): # self.log('Motion Controller init') # convert room to App - self.room: ControllerRoom = self.get_app(self.room) + self.room: ControllerRoomLights = self.get_app(self.room) # convert off_duration try: @@ -119,14 +119,14 @@ class ControllerMotion(ControllerBase): @dataclass(init=False) class ControllerButton(Hass): - room: ControllerRoom + room: ControllerRoomLights buttons: List[str] def initialize(self): self.buttons = self.args['buttons'] # convert room to App - self.room: ControllerRoom = self.get_app(self.args['room']) + self.room: ControllerRoomLights = self.get_app(self.args['room']) for button in self.buttons: self.listen_event( @@ -147,7 +147,7 @@ class ControllerButton(Hass): @dataclass(init=False) -class ControllerDaylight(ControllerBase): +class ControllerDaylight(ControllerEntities): latitude: float longitude: float @@ -171,20 +171,23 @@ class ControllerDaylight(ControllerBase): interval=10, entity=entity ) + self.log(f'Started adjustments') else: - self.log(f'Cancelling adjustments') 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'] + settings = self.adjuster.current_settings + state = {s: state[s] for s in settings.keys()} + valid = all((state[s] == val) for s, val in settings.items()) + return valid def ongoing_adjustment(self, kwargs): settings = self.adjuster.current_settings - - state = self.get_state(entity_id=kwargs['entity'], attribute='all')['attributes'] - state = {s: state[s] for s in settings.keys()} - - valid = all((state[s] == val) for s, val in settings.items()) + valid = self.matching_state(entity_id=kwargs['entity']) if not valid: - self.log(f'Current state: {state}') self.turn_on(entity_id=kwargs['entity'], **settings) self.log(f'Adjusted {self.friendly_name(kwargs["entity"])} with {settings}') - else: - self.log(f'Already valid') + # else: + # self.log(f'Already valid')