listening only to individual state changes
This commit is contained in:
@@ -24,22 +24,23 @@ class ControllerEntities(Hass):
|
|||||||
|
|
||||||
self.entities = [self.get_entity(e) for e in self.entities]
|
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)
|
@dataclass(init=False)
|
||||||
class ControllerRoomLights(ControllerEntities):
|
class ControllerRoomLights(ControllerEntities):
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
super().initialize()
|
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}/activate', self.activate)
|
||||||
self.register_service(f'{self.name}/deactivate', self.deactivate)
|
self.register_service(f'{self.name}/deactivate', self.deactivate)
|
||||||
|
|
||||||
def activate(self, namespace: str = None, domain: str = None, service=None, kwargs=None):
|
def activate(self, namespace: str = None, domain: str = None, service=None, kwargs=None):
|
||||||
|
self.log(self.entities)
|
||||||
for entity in self.entities:
|
for entity in self.entities:
|
||||||
self.log(f'Turning on {entity.name}')
|
self.log(f'Turning on {entity.name}')
|
||||||
entity.turn_on()
|
entity.turn_on()
|
||||||
|
|
||||||
def deactivate(self, namespace: str = None, domain: str = None, service=None, kwargs=None):
|
def deactivate(self, namespace: str = None, domain: str = None, service=None, kwargs=None):
|
||||||
|
self.log(self.entities)
|
||||||
for entity in self.entities:
|
for entity in self.entities:
|
||||||
self.log(f'Turning off {entity.name}')
|
self.log(f'Turning off {entity.name}')
|
||||||
entity.turn_off()
|
entity.turn_off()
|
||||||
@@ -69,7 +70,8 @@ class ControllerMotion(ControllerEntities):
|
|||||||
self.off_duration = timedelta()
|
self.off_duration = timedelta()
|
||||||
|
|
||||||
self.sync_state()
|
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
|
@property
|
||||||
def current_state(self) -> bool:
|
def current_state(self) -> bool:
|
||||||
@@ -84,17 +86,14 @@ class ControllerMotion(ControllerEntities):
|
|||||||
self.log(f'Syncing state, current state: {self.current_state}')
|
self.log(f'Syncing state, current state: {self.current_state}')
|
||||||
if self.current_state:
|
if self.current_state:
|
||||||
self.room.activate()
|
self.room.activate()
|
||||||
self.listen_motion_off()
|
|
||||||
else:
|
else:
|
||||||
self.room.deactivate()
|
self.room.deactivate()
|
||||||
self.listen_motion_on()
|
|
||||||
|
|
||||||
def listen_motion_on(self):
|
def listen_motion_on(self):
|
||||||
self.listen_state(
|
self.listen_state(
|
||||||
callback=self.callback_motion_on,
|
callback=self.callback_motion_on,
|
||||||
entity_id=[e.entity_id for e in self.entities],
|
entity_id=[e.entity_id for e in self.entities],
|
||||||
new='on',
|
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}')
|
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],
|
entity_id=[e.entity_id for e in self.entities],
|
||||||
new='off',
|
new='off',
|
||||||
duration=self.off_duration.total_seconds(),
|
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}')
|
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
|
resolution=500
|
||||||
)
|
)
|
||||||
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.log(f'Listening for state {[e.friendly_name 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':
|
if new == 'on':
|
||||||
self.adjustment_handle = self.run_every(
|
self.adjustment_handle = self.run_every(
|
||||||
callback=self.ongoing_adjustment,
|
callback=self.ongoing_adjustment,
|
||||||
@@ -173,8 +175,9 @@ class ControllerDaylight(ControllerEntities):
|
|||||||
)
|
)
|
||||||
self.log(f'Started adjustments')
|
self.log(f'Started adjustments')
|
||||||
else:
|
else:
|
||||||
self.cancel_timer(self.adjustment_handle)
|
if hasattr(self, 'adjustment_handle'):
|
||||||
self.log(f'Cancelled adjustments')
|
self.cancel_timer(self.adjustment_handle)
|
||||||
|
self.log(f'Cancelled adjustments')
|
||||||
|
|
||||||
def matching_state(self, entity_id: str):
|
def matching_state(self, entity_id: str):
|
||||||
state = self.get_state(entity_id=entity_id, attribute='all')['attributes']
|
state = self.get_state(entity_id=entity_id, attribute='all')['attributes']
|
||||||
|
|||||||
Reference in New Issue
Block a user