more cleanup

This commit is contained in:
root
2023-07-29 21:34:25 -05:00
parent 7a08bd4f8e
commit 162bd80409

View File

@@ -23,6 +23,8 @@ class MotionLight(Hass):
self.sync_state() self.sync_state()
self.run_daily(callback=self.refresh_state_times, start='00:00:00') self.run_daily(callback=self.refresh_state_times, start='00:00:00')
self.app_entities = self.gather_app_entities()
if (button := self.args.get('button')): if (button := self.args.get('button')):
if not isinstance(button, list): if not isinstance(button, list):
button = [button] button = [button]
@@ -141,7 +143,7 @@ class MotionLight(Hass):
return states return states
def current_state(self, time: time = None): def current_state(self, time: time = None):
if self.sleeping_active: if self.sleep_bool:
if (state := self.args.get('sleep_state')): if (state := self.args.get('sleep_state')):
return state return state
else: else:
@@ -155,8 +157,9 @@ class MotionLight(Hass):
def current_scene(self, time: time = None): def current_scene(self, time: time = None):
return self.current_state(time=time)['scene'] return self.current_state(time=time)['scene']
@property def gather_app_entities(self) -> List[str]:
def app_entities(self): """Returns a list of all the entities involved in any of the states
"""
def gen(): def gen():
for settings in deepcopy(self.args['scene']): for settings in deepcopy(self.args['scene']):
# dt = self.parse_time(settings.pop('time')) # dt = self.parse_time(settings.pop('time'))
@@ -196,17 +199,13 @@ class MotionLight(Hass):
except Exception: except Exception:
return timedelta() return timedelta()
@property
def sleeping_active(self) -> bool:
if 'sleep' in self.args:
return self.get_state(self.args['sleep']) == 'on'
else:
return False
@property @property
def sleep_bool(self) -> bool: def sleep_bool(self) -> bool:
if (sleep_var := self.args.get('sleep')): if (sleep_var := self.args.get('sleep')):
return self.get_state(sleep_var) == 'on' return self.get_state(sleep_var) == 'on'
else:
# self.log('WARNING')
return False
@sleep_bool.setter @sleep_bool.setter
def sleep_bool(self, val) -> bool: def sleep_bool(self, val) -> bool:
@@ -284,23 +283,11 @@ class MotionLight(Hass):
self.log(f'Turned on scene: {scene}') self.log(f'Turned on scene: {scene}')
elif isinstance(scene, dict): elif isinstance(scene, dict):
# makes setting the state to 'on' optional in the yaml definition
for entity, settings in scene.items(): for entity, settings in scene.items():
if 'state' not in settings: if 'state' not in settings:
scene[entity]['state'] = 'on' scene[entity]['state'] = 'on'
daylights = [
(entity, settings['state'])
for entity, settings in scene.items()
if isinstance(settings['state'], str) and settings['state'].startswith('daylight')
]
for entity, app_name in daylights:
# risky:
scene.pop(entity)
app: Continuous = self.get_app(app_name)
self.log(f'Adjusting with app...')
app.adjust()
self.call_service('scene/apply', entities=scene, transition=0) self.call_service('scene/apply', entities=scene, transition=0)
self.log(f'Applied scene: {scene}') self.log(f'Applied scene: {scene}')
@@ -330,6 +317,10 @@ class MotionLight(Hass):
self.log(f'Turned off {entity}') self.log(f'Turned off {entity}')
def refresh_state_times(self, *args, **kwargs): def refresh_state_times(self, *args, **kwargs):
"""Resets the `self.states` attribute to a newly parsed version of the states.
Parsed states have an absolute time for a certain day.
"""
# re-parse the state strings into times for the current day # re-parse the state strings into times for the current day
self.states = self.parse_states() self.states = self.parse_states()
@@ -352,9 +343,6 @@ class MotionLight(Hass):
if self.entity_state: if self.entity_state:
self.deactivate() self.deactivate()
else: else:
# use this instead of self.activate() to avoid it being called again by the state change event
# ^ no longer relevant with self.activate() commented out in self.callback_light_on()
# self.entity_state = True
self.activate() self.activate()
# event 1001 is a long press start # event 1001 is a long press start