pre-merge (kinda fucked up before)

This commit is contained in:
John Lancaster
2024-01-28 09:04:54 -06:00
parent dee31fe6f1
commit af8d13bfce
15 changed files with 162 additions and 102 deletions

View File

@@ -8,9 +8,8 @@ from appdaemon.plugins.mqtt.mqttapi import Mqtt
class SleepSetter(Hass, Mqtt):
def initialize(self):
assert self.entity_exists(entity_id=self.variable), f'{self.variable} does not exist'
self.variable_entity.listen_state(callback=self.handle_state)
self.listen_state(callback=self.handle_state, entity_id=self.variable)
self.setup_buttons()
self.log(f'{self.variable} can be set using {self.button}, currently {self.state}')
def setup_buttons(self):
if isinstance(self.button, list):
@@ -23,7 +22,7 @@ class SleepSetter(Hass, Mqtt):
topic = f'zigbee2mqtt/{name}'
self.mqtt_subscribe(topic, namespace='mqtt')
self.listen_event(self.handle_button, "MQTT_MESSAGE", topic=topic, namespace='mqtt', button=name)
self.log(f'Listening for sleep setting on {name}')
self.log(f'Subscribed: {topic}')
@property
def button(self) -> str:
@@ -56,15 +55,13 @@ class SleepSetter(Hass, Mqtt):
@property
def sun_elevation(self) -> float:
try:
return float(self.get_state('sun.sun', 'elevation'))
except:
self.log(f'Failed to return sun elevation')
return
state = self.get_state('sun.sun', 'elevation')
assert isinstance(state, float)
return state
def handle_state(self, entity, attribute, old, new, kwargs):
self.log(f'{entity}: {old} -> {new}')
if self.state and self.sun_elevation < float(self.args['elevation_limit']):
self.log(f'new state: {self.state}')
if self.state:
self.all_off()
try:
self.call_service('scene/turn_on', entity_id=self.scene)
@@ -86,22 +83,18 @@ class SleepSetter(Hass, Mqtt):
return
else:
self.handle_action(action)
def handle_action(self, action: str):
if action == '':
return
elif action == 'hold':
if action == 'hold':
self.log(f' {action.upper()} '.center(50, '='))
self.state = True
elif action == 'double':
self.log(f' {action.upper()} '.center(50, '='))
self.state = not self.state
if (on_apps := self.args.get('on_apps', None)) is not None:
for app_name in on_apps:
try:
self.get_app(app_name).activate(cause='sleep setter')
except:
return
else:
self.log(f'Activated {app_name}')
self.on_apps()
def all_off(self):
self.log(f'Deactivating apps')
@@ -119,3 +112,13 @@ class SleepSetter(Hass, Mqtt):
except:
self.log(f'Failed to turn off {entity}')
continue
def on_apps(self):
if (on_apps := self.args.get('on_apps', None)) is not None:
for app_name in on_apps:
try:
self.get_app(app_name).activate(kwargs={'cause': 'sleep setter'})
except:
return
else:
self.log(f'Activated {app_name}')