simplified a bit
This commit is contained in:
@@ -8,9 +8,8 @@ from appdaemon.plugins.mqtt.mqttapi import Mqtt
|
|||||||
class SleepSetter(Hass, Mqtt):
|
class SleepSetter(Hass, Mqtt):
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
assert self.entity_exists(entity_id=self.variable), f'{self.variable} does not exist'
|
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.setup_buttons()
|
||||||
self.log(f'{self.variable} can be set using {self.button}, currently {self.state}')
|
|
||||||
|
|
||||||
def setup_buttons(self):
|
def setup_buttons(self):
|
||||||
if isinstance(self.button, list):
|
if isinstance(self.button, list):
|
||||||
@@ -23,7 +22,7 @@ class SleepSetter(Hass, Mqtt):
|
|||||||
topic = f'zigbee2mqtt/{name}'
|
topic = f'zigbee2mqtt/{name}'
|
||||||
self.mqtt_subscribe(topic, namespace='mqtt')
|
self.mqtt_subscribe(topic, namespace='mqtt')
|
||||||
self.listen_event(self.handle_button, "MQTT_MESSAGE", topic=topic, namespace='mqtt', button=name)
|
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
|
@property
|
||||||
def button(self) -> str:
|
def button(self) -> str:
|
||||||
@@ -56,15 +55,13 @@ class SleepSetter(Hass, Mqtt):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def sun_elevation(self) -> float:
|
def sun_elevation(self) -> float:
|
||||||
try:
|
state = self.get_state('sun.sun', 'elevation')
|
||||||
return float(self.get_state('sun.sun', 'elevation'))
|
assert isinstance(state, float)
|
||||||
except:
|
return state
|
||||||
self.log(f'Failed to return sun elevation')
|
|
||||||
return
|
|
||||||
|
|
||||||
def handle_state(self, entity, attribute, old, new, kwargs):
|
def handle_state(self, entity, attribute, old, new, kwargs):
|
||||||
self.log(f'{entity}: {old} -> {new}')
|
self.log(f'new state: {self.state}')
|
||||||
if self.state and self.sun_elevation < float(self.args['elevation_limit']):
|
if self.state:
|
||||||
self.all_off()
|
self.all_off()
|
||||||
try:
|
try:
|
||||||
self.call_service('scene/turn_on', entity_id=self.scene)
|
self.call_service('scene/turn_on', entity_id=self.scene)
|
||||||
@@ -77,7 +74,6 @@ class SleepSetter(Hass, Mqtt):
|
|||||||
def handle_button(self, event_name, data, kwargs):
|
def handle_button(self, event_name, data, kwargs):
|
||||||
topic = data['topic']
|
topic = data['topic']
|
||||||
# self.log(f'Button event for: {topic}')
|
# self.log(f'Button event for: {topic}')
|
||||||
if (elev := self.sun_elevation) < 0:
|
|
||||||
try:
|
try:
|
||||||
payload = json.loads(data['payload'])
|
payload = json.loads(data['payload'])
|
||||||
action = payload['action']
|
action = payload['action']
|
||||||
@@ -88,9 +84,6 @@ class SleepSetter(Hass, Mqtt):
|
|||||||
else:
|
else:
|
||||||
self.handle_action(action)
|
self.handle_action(action)
|
||||||
|
|
||||||
else:
|
|
||||||
self.log(f'Ignoring event because sun elevation {elev} > 0')
|
|
||||||
|
|
||||||
def handle_action(self, action: str):
|
def handle_action(self, action: str):
|
||||||
if action == '':
|
if action == '':
|
||||||
return
|
return
|
||||||
@@ -98,14 +91,7 @@ class SleepSetter(Hass, Mqtt):
|
|||||||
self.state = True
|
self.state = True
|
||||||
elif action == 'double':
|
elif action == 'double':
|
||||||
self.state = not self.state
|
self.state = not self.state
|
||||||
if (on_apps := self.args.get('on_apps', None)) is not None:
|
self.on_apps()
|
||||||
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}')
|
|
||||||
|
|
||||||
def all_off(self):
|
def all_off(self):
|
||||||
self.log(f'Deactivating apps')
|
self.log(f'Deactivating apps')
|
||||||
@@ -123,3 +109,13 @@ class SleepSetter(Hass, Mqtt):
|
|||||||
except:
|
except:
|
||||||
self.log(f'Failed to turn off {entity}')
|
self.log(f'Failed to turn off {entity}')
|
||||||
continue
|
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