This commit is contained in:
root
2023-07-29 18:51:22 -05:00
parent c80b665f95
commit 88c28248fd

View File

@@ -18,11 +18,10 @@ class MotionLight(Hass):
"""
def initialize(self):
self.state_change_handle = self.listen_state(self.state_change, self.entity)
self.states = self.parse_states()
self.state_change_handle = self.listen_state(self.handle_state_change, self.entity)
self.refresh_state_times()
self.sync_state()
self.schedule_transitions()
self.run_daily(callback=self.schedule_transitions, start='00:00:00')
self.run_daily(callback=self.refresh_state_times, start='00:00:00')
if (button := self.args.get('button')):
if not isinstance(button, list):
@@ -32,8 +31,8 @@ class MotionLight(Hass):
self.listen_event(self.handle_button, event='deconz_event', id=button)
if (door := self.args.get('door')):
self.door_entity = self.get_entity(door)
self.log(f'Setting up door: {self.door_entity.friendly_name}')
door_entity = self.get_entity(door)
self.log(f'Setting up door: {door_entity.friendly_name}')
self.listen_state(
callback=self.activate_all_off,
entity_id=door,
@@ -230,8 +229,9 @@ class MotionLight(Hass):
self.callback_light_off()
def listen_motion_on(self):
self.log(
f'Waiting for motion on {self.friendly_name(self.sensor)} to turn on {self.friendly_name(self.entity)}')
"""Sets up the motion on callback to activate the room
"""
self.log(f'Waiting for motion on {self.friendly_name(self.sensor)} to turn on {self.friendly_name(self.entity)}')
self.motion_on_handle = self.listen_state(
callback=self.activate,
entity_id=self.sensor,
@@ -240,8 +240,9 @@ class MotionLight(Hass):
)
def listen_motion_off(self, duration: timedelta):
self.log(
f'Waiting for motion to stop on {self.friendly_name(self.sensor)} for {duration} to turn off {self.friendly_name(self.entity)}')
"""Sets up the motion off callback to deactivate the room
"""
self.log(f'Waiting for motion to stop on {self.friendly_name(self.sensor)} for {duration} to turn off {self.friendly_name(self.entity)}')
self.motion_off_handle = self.listen_state(
callback=self.deactivate,
entity_id=self.sensor,
@@ -250,22 +251,7 @@ class MotionLight(Hass):
oneshot=True
)
# def cancel_motion(self, handle_name):
# if hasattr(self, handle_name):
# handle = getattr(self, handle_name)
# try:
# self.log(f'{handle_name}: {self.info_listen_state(handle)}')
# except ValueError:
# self.log(f'Error getting {handle_name} info')
# else:
# self.cancel_listen_state(handle)
# self.log(f'Cancelled handle: {handle_name}')
# finally:
# delattr(self, handle_name)
# else:
# self.log(f'No attribute: {handle_name}')
def 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):
"""Callback attached to the state change of the light.
"""
if new == 'on':
@@ -281,11 +267,9 @@ class MotionLight(Hass):
self.log('Light on callback')
self.cancel_motion_callback(new='on')
self.listen_motion_off(self.off_duration)
# if self.is_stateful:
# self.activate()
def callback_light_off(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
"""Called when the light turns on
"""Called when the light turns off
"""
self.log('Light off callback')
self.cancel_motion_callback(new='off')
@@ -345,7 +329,11 @@ class MotionLight(Hass):
self.turn_off(entity)
self.log(f'Turned off {entity}')
def schedule_transitions(self, *args, **kwargs):
def refresh_state_times(self, *args, **kwargs):
# re-parse the state strings into times for the current day
self.states = self.parse_states()
# schedule the transitions
for state in self.states:
dt = str(state['time'])[:8]
self.log(f'Scheduling transition at: {dt}')