broke out matching_state

This commit is contained in:
John Lancaster
2023-04-15 16:32:50 -05:00
parent f2482b872a
commit efb30e3ddc

View File

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