dataclasses and bugfixes
This commit is contained in:
35
button.py
35
button.py
@@ -1,16 +1,13 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from appdaemon.plugins.mqtt.mqttapi import Mqtt
|
||||
from room_control import RoomController
|
||||
|
||||
|
||||
class ButtonController(Mqtt):
|
||||
def initialize(self):
|
||||
task = self.get_app(self.args['app'])
|
||||
self.app: RoomController = asyncio.get_event_loop().run_until_complete(task)
|
||||
class Button(Mqtt):
|
||||
async def initialize(self):
|
||||
self.app: RoomController = await self.get_app(self.args['app'])
|
||||
self.setup_buttons(self.args['button'])
|
||||
# self.log(f'Done')
|
||||
|
||||
def setup_buttons(self, buttons):
|
||||
if isinstance(buttons, list):
|
||||
@@ -25,30 +22,28 @@ class ButtonController(Mqtt):
|
||||
self.listen_event(self.handle_button, "MQTT_MESSAGE", topic=topic, namespace='mqtt', button=name)
|
||||
self.log(f'"{topic}" controls app {self.app.name}')
|
||||
|
||||
async def handle_button(self, event_name, data, kwargs):
|
||||
topic = data['topic']
|
||||
self.log(f'Button event for: {topic}')
|
||||
def handle_button(self, event_name, data, kwargs):
|
||||
try:
|
||||
payload = json.loads(data['payload'])
|
||||
action = payload['action']
|
||||
button = kwargs['button']
|
||||
except json.JSONDecodeError:
|
||||
self.log(f'Error decoding JSON from {data["payload"]}', level='ERROR')
|
||||
except KeyError as e:
|
||||
return
|
||||
else:
|
||||
self.log(f'{button}: {action}')
|
||||
await self.handle_action(action)
|
||||
if action != '':
|
||||
self.handle_action(action)
|
||||
|
||||
async def handle_action(self, action: str):
|
||||
if action == '':
|
||||
return
|
||||
elif action == 'single':
|
||||
cause = 'button single click'
|
||||
state = await self.get_state(entity_id=self.args['ref_entity'])
|
||||
def handle_action(self, action: str):
|
||||
if action == 'single':
|
||||
self.log(f' {action.upper()} '.center(50, '='))
|
||||
state = self.get_state(self.args['ref_entity'])
|
||||
kwargs = {
|
||||
'kwargs': {'cause': f'button single click: toggle while {state}'}
|
||||
}
|
||||
if state == 'on':
|
||||
self.app.deactivate(cause=cause)
|
||||
self.app.deactivate(**kwargs)
|
||||
else:
|
||||
await self.app.activate(cause=cause)
|
||||
self.app.activate(**kwargs)
|
||||
else:
|
||||
pass
|
||||
Reference in New Issue
Block a user