cube updates

This commit is contained in:
John Lancaster
2024-03-04 19:23:07 -06:00
parent 2c77c3aa66
commit a79d441668
2 changed files with 40 additions and 19 deletions

View File

@@ -1,33 +1,50 @@
import json import json
import sys
from copy import deepcopy from copy import deepcopy
from pathlib import Path
from appdaemon.plugins.hass.hassapi import Hass from appdaemon.plugins.hass.hassapi import Hass
from appdaemon.plugins.mqtt.mqttapi import Mqtt from appdaemon.plugins.mqtt.mqttapi import Mqtt
rc_path = (Path(__file__).resolve().parents[1] / 'room_control').as_posix()
sys.path.insert(0, rc_path)
from console import console, init_logging, deinit_logging
class AqaraCube(Hass, Mqtt): class AqaraCube(Hass, Mqtt):
def initialize(self): def initialize(self):
if (level := self.args.get('rich', False)):
init_logging(self, level)
topic = f'zigbee2mqtt/{self.args["cube"]}' topic = f'zigbee2mqtt/{self.args["cube"]}'
self.mqtt_subscribe(topic, namespace='mqtt') self.mqtt_subscribe(topic, namespace='mqtt')
self.listen_event(self.handle_event, "MQTT_MESSAGE", topic=topic, namespace='mqtt') self.listen_event(self.handle_event, 'MQTT_MESSAGE', topic=topic, namespace='mqtt')
self.log(f'Listening for cube events on: {topic}') self.log(f'Listening for cube events on: {topic}')
self.app = self.get_app(self.args['app']) self.app = self.get_app(self.args['app'])
self.log(f'Connected to other app: {self.app.name}') self.log(f'Connected to other app: {self.app.name}')
def handle_event(self, event_name, data, kwargs): def terminate(self):
topic = data['topic'] self.log('[bold red]Terminating[/]', level='DEBUG')
deinit_logging(self)
self.log('Success', level='DEBUG')
def handle_event(self, event_name, data, cb_args):
topic_prefix, cube_name = data['topic'].split('/')
self.log(f'{event_name} on [bright_black]{topic_prefix}/[/][blue]{cube_name}[/]', data, level='DEBUG')
payload = json.loads(data['payload']) payload = json.loads(data['payload'])
action = payload['action'] action = payload['action']
if action == '': if action == '' or action == 'wakeup':
return return
if (arg := self.args.get(action, None)) is not None:
self.action_handler(action=action, description=arg)
elif (handler := getattr(self, f'handle_{action}', None)):
handler(payload)
else: else:
self.log(f'Unhandled cube action: {action}') self.log(json.dumps(payload, indent=2), level='DEBUG')
if (arg := self.args.get(action, False)):
self.action_handler(action=action, description=arg)
elif handler := getattr(self, f'handle_{action}', None):
handler(payload)
def action_handler(self, action: str, description: str): def action_handler(self, action: str, description: str):
self.log(f'{self.args["cube"]}: {action}: {description}') self.log(f'{self.args["cube"]}: {action}: {description}')
@@ -43,6 +60,9 @@ class AqaraCube(Hass, Mqtt):
cause = f'{self.args["cube"]} {action}' cause = f'{self.args["cube"]} {action}'
self.app.toggle_activate(kwargs={'cause': cause}) self.app.toggle_activate(kwargs={'cause': cause})
else:
self.log(f'Unhandled action: {action}', level='WARNING')
# def handle_rotate_right(self, payload): # def handle_rotate_right(self, payload):
# self.log(f'{self.args["cube"]}: Rotate right') # self.log(f'{self.args["cube"]}: Rotate right')

View File

@@ -1,8 +1,9 @@
cube1: cube1:
module: cube module: cube
class: AqaraCube class: AqaraCube
rich: INFO
cube: Cube 1 cube: Cube 1
app: bedroom app: bedroom
flip90: toggle flip90: toggle
flip180: scene.bedsport flip180: scene.bedsport
shake: activate # shake: activate