cleanups
This commit is contained in:
@@ -18,11 +18,10 @@ class MotionLight(Hass):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
self.state_change_handle = self.listen_state(self.state_change, self.entity)
|
self.state_change_handle = self.listen_state(self.handle_state_change, self.entity)
|
||||||
self.states = self.parse_states()
|
self.refresh_state_times()
|
||||||
self.sync_state()
|
self.sync_state()
|
||||||
self.schedule_transitions()
|
self.run_daily(callback=self.refresh_state_times, start='00:00:00')
|
||||||
self.run_daily(callback=self.schedule_transitions, start='00:00:00')
|
|
||||||
|
|
||||||
if (button := self.args.get('button')):
|
if (button := self.args.get('button')):
|
||||||
if not isinstance(button, list):
|
if not isinstance(button, list):
|
||||||
@@ -32,8 +31,8 @@ class MotionLight(Hass):
|
|||||||
self.listen_event(self.handle_button, event='deconz_event', id=button)
|
self.listen_event(self.handle_button, event='deconz_event', id=button)
|
||||||
|
|
||||||
if (door := self.args.get('door')):
|
if (door := self.args.get('door')):
|
||||||
self.door_entity = self.get_entity(door)
|
door_entity = self.get_entity(door)
|
||||||
self.log(f'Setting up door: {self.door_entity.friendly_name}')
|
self.log(f'Setting up door: {door_entity.friendly_name}')
|
||||||
self.listen_state(
|
self.listen_state(
|
||||||
callback=self.activate_all_off,
|
callback=self.activate_all_off,
|
||||||
entity_id=door,
|
entity_id=door,
|
||||||
@@ -230,8 +229,9 @@ class MotionLight(Hass):
|
|||||||
self.callback_light_off()
|
self.callback_light_off()
|
||||||
|
|
||||||
def listen_motion_on(self):
|
def listen_motion_on(self):
|
||||||
self.log(
|
"""Sets up the motion on callback to activate the room
|
||||||
f'Waiting for motion on {self.friendly_name(self.sensor)} to turn on {self.friendly_name(self.entity)}')
|
"""
|
||||||
|
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(
|
self.motion_on_handle = self.listen_state(
|
||||||
callback=self.activate,
|
callback=self.activate,
|
||||||
entity_id=self.sensor,
|
entity_id=self.sensor,
|
||||||
@@ -240,8 +240,9 @@ class MotionLight(Hass):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def listen_motion_off(self, duration: timedelta):
|
def listen_motion_off(self, duration: timedelta):
|
||||||
self.log(
|
"""Sets up the motion off callback to deactivate the room
|
||||||
f'Waiting for motion to stop on {self.friendly_name(self.sensor)} for {duration} to turn off {self.friendly_name(self.entity)}')
|
"""
|
||||||
|
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(
|
self.motion_off_handle = self.listen_state(
|
||||||
callback=self.deactivate,
|
callback=self.deactivate,
|
||||||
entity_id=self.sensor,
|
entity_id=self.sensor,
|
||||||
@@ -250,22 +251,7 @@ class MotionLight(Hass):
|
|||||||
oneshot=True
|
oneshot=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# def cancel_motion(self, handle_name):
|
def handle_state_change(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
|
||||||
# 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):
|
|
||||||
"""Callback attached to the state change of the light.
|
"""Callback attached to the state change of the light.
|
||||||
"""
|
"""
|
||||||
if new == 'on':
|
if new == 'on':
|
||||||
@@ -281,11 +267,9 @@ class MotionLight(Hass):
|
|||||||
self.log('Light on callback')
|
self.log('Light on callback')
|
||||||
self.cancel_motion_callback(new='on')
|
self.cancel_motion_callback(new='on')
|
||||||
self.listen_motion_off(self.off_duration)
|
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):
|
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.log('Light off callback')
|
||||||
self.cancel_motion_callback(new='off')
|
self.cancel_motion_callback(new='off')
|
||||||
@@ -345,7 +329,11 @@ class MotionLight(Hass):
|
|||||||
self.turn_off(entity)
|
self.turn_off(entity)
|
||||||
self.log(f'Turned 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:
|
for state in self.states:
|
||||||
dt = str(state['time'])[:8]
|
dt = str(state['time'])[:8]
|
||||||
self.log(f'Scheduling transition at: {dt}')
|
self.log(f'Scheduling transition at: {dt}')
|
||||||
|
|||||||
Reference in New Issue
Block a user