broke out starting/stopping adjustment callbacks

This commit is contained in:
John Lancaster
2023-04-20 23:53:47 -05:00
parent 92fa257829
commit 51534fc3ed

View File

@@ -64,6 +64,7 @@ class ControllerRoomLights(ControllerEntities):
if 'sleep' in self.args: if 'sleep' in self.args:
self.get_entity(self.args['sleep']).set_state(state='on' if val else 'off') self.get_entity(self.args['sleep']).set_state(state='on' if val else 'off')
@dataclass(init=False) @dataclass(init=False)
class ControllerMotion(ControllerEntities): class ControllerMotion(ControllerEntities):
room: ControllerRoomLights room: ControllerRoomLights
@@ -162,7 +163,6 @@ class ControllerDaylight(Hass):
entities: List[str] entities: List[str]
latitude: float latitude: float
longitude: float longitude: float
enable: bool = field(init=False, default=True)
def initialize(self): def initialize(self):
# convert room to App # convert room to App
@@ -183,7 +183,14 @@ class ControllerDaylight(Hass):
) )
# self.log(self.adjuster) # self.log(self.adjuster)
self.listen_state(callback=self.handle_state_change, entity_id=[e.entity_id for e in self.entities]) self.listen_state(callback=self.handle_state_change,
entity_id=[e.entity_id for e in self.entities])
# self.listen_state(callback=self.handle_brightness_change,
# entity_id=[e.entity_id for e in self.entities],
# attribute='brightness')
# self.listen_state(callback=self.handle_brightness_change,
# entity_id=[e.entity_id for e in self.entities],
# attribute='color_temp')
ents = [e.friendly_name for e in self.entities] ents = [e.friendly_name for e in self.entities]
if len(ents) > 1: if len(ents) > 1:
ents[-1] = f'and {ents[-1]}' ents[-1] = f'and {ents[-1]}'
@@ -192,20 +199,23 @@ class ControllerDaylight(Hass):
self.run_every(callback=self.update_sensors, start='now', interval=5.0) self.run_every(callback=self.update_sensors, start='now', interval=5.0)
for entity in self.entities:
if entity.get_state() == 'on':
self.start_adjustments(entity.entity_id)
def handle_state_change(self, entity=None, attribute=None, old=None, new=None, kwargs=None): def handle_state_change(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
self.log(f'{entity}: {old} -> {new}')
if new == 'on': if new == 'on':
self.adjustment_handle = self.run_every( if old == 'off':
callback=self.ongoing_adjustment, self.start_adjustments(entity)
start='now',
interval=10,
entity=entity
)
self.log(f'Started adjustments')
else: else:
if hasattr(self, 'adjustment_handle'): self.stop_adjustments()
self.cancel_timer(self.adjustment_handle)
del self.adjustment_handle def handle_brightness_change(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
self.log(f'Cancelled adjustments') if not self.matching_state(entity):
self.log(f'{entity}.{attribute}: {old} -> {new}')
self.log(f'State does not match adjuster settings, disabling adjustments')
self.stop_adjustments()
def matching_state(self, entity_id: str) -> bool: def matching_state(self, entity_id: str) -> bool:
"""Checks whether the current state of the light matches the settings from the DaylightAdjuster """Checks whether the current state of the light matches the settings from the DaylightAdjuster
@@ -221,11 +231,37 @@ class ControllerDaylight(Hass):
try: try:
state = {s: state[s] for s in settings.keys()} state = {s: state[s] for s in settings.keys()}
except KeyError: except KeyError:
for s in settings.keys():
if s not in state:
self.log(f'{s} not in {state}')
return False return False
else: else:
valid = all((state[s] == val) for s, val in settings.items()) valid = all((state[s] == val) for s, val in settings.items())
if not valid:
for s, val in settings.items():
if state[s] != val:
self.log(f'{entity_id}.{s}: {state[s]} != {val}')
return valid return valid
@property
def enabled(self) -> bool:
return hasattr(self.adjustment_handle)
def start_adjustments(self, entity: str):
self.adjustment_handle = self.run_every(
callback=self.ongoing_adjustment,
start='now',
interval=10,
entity=entity
)
self.log(f'Started adjustments')
def stop_adjustments(self):
if hasattr(self, 'adjustment_handle'):
self.cancel_timer(self.adjustment_handle)
del self.adjustment_handle
self.log(f'Cancelled adjustments')
def ongoing_adjustment(self, kwargs): def ongoing_adjustment(self, kwargs):
self.log(f'Ongoing adjustment') self.log(f'Ongoing adjustment')
settings = self.adjuster.current_settings settings = self.adjuster.current_settings
@@ -240,5 +276,5 @@ class ControllerDaylight(Hass):
self.set_state( self.set_state(
entity_id=id, state=val, entity_id=id, state=val,
attributes={ attributes={
'friendly_name': f'Daylight, {key}, {self.name}', 'friendly_name': f'Daylight, {key}, {self.name}',
'state_class': 'measurement'}) 'state_class': 'measurement'})