simplified a bit
This commit is contained in:
@@ -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)
|
||||
@@ -77,19 +74,15 @@ class SleepSetter(Hass, Mqtt):
|
||||
def handle_button(self, event_name, data, kwargs):
|
||||
topic = data['topic']
|
||||
# self.log(f'Button event for: {topic}')
|
||||
if (elev := self.sun_elevation) < 0:
|
||||
try:
|
||||
payload = json.loads(data['payload'])
|
||||
action = payload['action']
|
||||
except json.JSONDecodeError:
|
||||
self.log(f'Error decoding JSON from {data["payload"]}', level='ERROR')
|
||||
except KeyError as e:
|
||||
return
|
||||
else:
|
||||
self.handle_action(action)
|
||||
|
||||
try:
|
||||
payload = json.loads(data['payload'])
|
||||
action = payload['action']
|
||||
except json.JSONDecodeError:
|
||||
self.log(f'Error decoding JSON from {data["payload"]}', level='ERROR')
|
||||
except KeyError as e:
|
||||
return
|
||||
else:
|
||||
self.log(f'Ignoring event because sun elevation {elev} > 0')
|
||||
self.handle_action(action)
|
||||
|
||||
def handle_action(self, action: str):
|
||||
if action == '':
|
||||
@@ -98,14 +91,7 @@ class SleepSetter(Hass, Mqtt):
|
||||
self.state = True
|
||||
elif action == 'double':
|
||||
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')
|
||||
@@ -123,3 +109,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(cause='sleep setter')
|
||||
except:
|
||||
return
|
||||
else:
|
||||
self.log(f'Activated {app_name}')
|
||||
Reference in New Issue
Block a user