button work

This commit is contained in:
John Lancaster
2024-03-06 20:17:38 -06:00
parent 0939c21554
commit 17670b983e
4 changed files with 40 additions and 15 deletions

0
__init__.py Normal file
View File

View File

@@ -1,17 +1,25 @@
import json import json
from dataclasses import dataclass
from appdaemon.plugins.mqtt.mqttapi import Mqtt from appdaemon.plugins.mqtt.mqttapi import Mqtt
from console import setup_logging from console import console, init_logging
from room_control import RoomController from room_control import RoomController
@dataclass(init=False)
class Button(Mqtt): class Button(Mqtt):
button: str
rich: bool = False
async def initialize(self): async def initialize(self):
if self.args.get('rich', False): if level := self.args.get('rich', False):
setup_logging(self) self.rich = True
init_logging(self, level)
self.app: RoomController = await self.get_app(self.args['app']) self.app: RoomController = await self.get_app(self.args['app'])
self.setup_buttons(self.args['button'])
self.button = self.args['button']
self.setup_buttons(self.button)
def setup_buttons(self, buttons): def setup_buttons(self, buttons):
if isinstance(buttons, list): if isinstance(buttons, list):
@@ -22,9 +30,12 @@ class Button(Mqtt):
def setup_button(self, name: str): def setup_button(self, name: str):
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'"{topic}" controls app {self.app.name}') if self.rich:
self.log(f'MQTT topic [blue]{topic}[/] controls app [green]{self.app.name}[/]')
else:
self.log(f'MQTT topic "{topic}" controls app {self.app.name}')
def handle_button(self, event_name, data, kwargs): def handle_button(self, event_name, data, kwargs):
try: try:
@@ -39,8 +50,11 @@ class Button(Mqtt):
self.handle_action(action) self.handle_action(action)
def handle_action(self, action: str): def handle_action(self, action: str):
if isinstance(action, str):
action_str = f' [yellow]{action.upper()}[/] ' if self.rich else f' {action.upper()} '
if action == 'single': if action == 'single':
self.log(f' {action.upper()} '.center(50, '=')) self.log(action_str.center(80, '='))
state = self.get_state(self.args['ref_entity']) state = self.get_state(self.args['ref_entity'])
kwargs = {'kwargs': {'cause': f'button single click: toggle while {state}'}} kwargs = {'kwargs': {'cause': f'button single click: toggle while {state}'}}
if state == 'on': if state == 'on':

View File

@@ -1,26 +1,39 @@
import logging import logging
from appdaemon.adapi import ADAPI from appdaemon.adapi import ADAPI
from appdaemon.logging import AppNameFormatter
from rich.console import Console from rich.console import Console
from rich.highlighter import NullHighlighter
from rich.logging import RichHandler from rich.logging import RichHandler
from rich.theme import Theme
console = Console(
width=150,
theme=Theme({'appname': 'italic bright_cyan'}),
)
console = Console(width=150)
handler = RichHandler( handler = RichHandler(
console=console, console=console,
highlighter=NullHighlighter(),
markup=True, markup=True,
show_path=False, show_path=False,
omit_repeated_times=False,
log_time_format='%Y-%m-%d %I:%M:%S %p', log_time_format='%Y-%m-%d %I:%M:%S %p',
) )
def setup_logging(self: ADAPI, level): handler.setFormatter(AppNameFormatter(fmt='[appname]{appname}[/] {message}', style='{'))
def init_logging(self: ADAPI, level):
if not any(isinstance(h, RichHandler) for h in self.logger.handlers): if not any(isinstance(h, RichHandler) for h in self.logger.handlers):
self.logger.propagate = False self.logger.propagate = False
self.logger.setLevel(level) self.logger.setLevel(level)
self.logger.addHandler(handler) self.logger.addHandler(handler)
self.log(f'Added rich handler for [bold green]{self.logger.name}[/]') self.log(f'Added rich handler for [bold green]{self.logger.name}[/]')
# self.log(f'Formatter [bold green]{self.logger.handlers[0].formatter}[/]') # self.log(f'Formatter for RichHandler: {handler.formatter}')
def deinit_logging(self: ADAPI): def deinit_logging(self: ADAPI):
self.logger.setLevel(logging.NOTSET) self.logger.setLevel(logging.NOTSET)

View File

@@ -9,7 +9,7 @@ from appdaemon.entity import Entity
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
from astral import SunDirection from astral import SunDirection
from console import console, setup_logging, deinit_logging from console import console, init_logging, deinit_logging
from rich.table import Table from rich.table import Table
@@ -152,7 +152,7 @@ class RoomController(Hass, Mqtt):
def initialize(self): def initialize(self):
if (level := self.args.get('rich', False)): if (level := self.args.get('rich', False)):
setup_logging(self, level) init_logging(self, level)
self.rich_logging = True self.rich_logging = True
self.log(f'Initializing {self}') self.log(f'Initializing {self}')
@@ -211,9 +211,7 @@ class RoomController(Hass, Mqtt):
if self.rich_logging: if self.rich_logging:
table = self._room_config.rich_table(self.name) table = self._room_config.rich_table(self.name)
console.print(table) console.log(table, highlight=False)
# for state in self.states:
# self.log(f'State: {state.time.strftime("%I:%M:%S %p")} {state.scene}')
self.states = sorted(self.states, key=lambda s: s.time, reverse=True) self.states = sorted(self.states, key=lambda s: s.time, reverse=True)