import logging import re from appdaemon.adapi import ADAPI from appdaemon.logging import AppNameFormatter from rich.console import Console from rich.highlighter import NullHighlighter, RegexHighlighter from rich.logging import RichHandler from rich.theme import Theme console = Console( width=150, theme=Theme({ 'log.time': 'none', 'logging.level.info': 'none', 'room': 'italic bright_cyan', 'component': 'dark_violet', 'entity_id': 'light_slate_blue', 'time': 'yellow', 'z2m': 'bright_black', 'topic': 'chartreuse2', }), log_time_format='%Y-%m-%d %I:%M:%S %p', ) class UnMarkupFormatter(AppNameFormatter): md_regex = re.compile(r'(?P\[.*?\])(?P.*?)(?P\[\/\])') def format(self, record: logging.LogRecord): result = super().format(record) return self.md_regex.sub(r'\g', result) class RoomControllerFormatter(logging.Formatter): def __init__(self, room: str, component: str = None): self.log_fields = {'room': room} fmt = '[room]{room:>12}[/]' if component is not None: fmt += ' [component]{component:<9}[/]' self.log_fields['component'] = component fmt += ' {message}' datefmt = '%Y-%m-%d %I:%M:%S %p' style = '{' validate=True super().__init__(fmt, datefmt, style, validate) # console.print(f'Format: [bold yellow]{fmt}[/]') def format(self, record: logging.LogRecord): parts = record.name.split('.') record.room = parts[1] if len(parts) == 3: record.component = parts[2] return super().format(record) class RCHighlighter(RegexHighlighter): highlights = [ r"(?P(light|switch)\.\w+)", r'(?P