Skip to content

Commit

Permalink
Nicer logging in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
jasujm committed Feb 2, 2024
1 parent 826adeb commit f4f20d3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Unreleased
Date
2024-02-02

Added
* Nicer logging in CLI

Changed
* Refresh device states when MQTT connection is resumed
* Try to recreate MQTT clients after connection is lost. This is an additional
Expand Down
3 changes: 3 additions & 0 deletions pyupgw/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from pyupgw import Client, Device, Gateway, HvacDevice, SystemMode, create_client

from ._logging import setup_logging
from .commands import get as get_impl
from .commands import list_devices as list_impl
from .commands import update as update_impl
Expand Down Expand Up @@ -48,6 +49,8 @@ def cli(ctx: click.Context, username: str | None, password: str | None):
implemented on a best-effort basis. No warranty of any kind is provided.
"""

setup_logging()

username = username or os.getenv("PYUPGW_USERNAME")
password = password or os.getenv("PYUPGW_PASSWORD")

Expand Down
19 changes: 19 additions & 0 deletions pyupgw/cli/_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""CLI logging"""

import logging

from rich.console import Console
from rich.logging import RichHandler

_LOGGING_HANDLER = RichHandler()


def setup_logging(handlers=None, **kwargs):
"""Setup logging"""
handlers = [*(handlers or []), _LOGGING_HANDLER]
logging.basicConfig(handlers=handlers, **kwargs)


def set_logging_console(console: Console):
"""Set new logging console"""
_LOGGING_HANDLER.console = console
3 changes: 3 additions & 0 deletions pyupgw/cli/tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from pyupgw import Client, Device, HvacDevice, SystemMode, create_client

from ._logging import set_logging_console

HELP_PANEL = Panel(
"(:arrow_up::arrow_down:) Select device, (O)ff, (H)eat, (+-) Adjust temperature, (Enter) Confirm"
)
Expand Down Expand Up @@ -183,6 +185,7 @@ async def _tui_main(term: Terminal, username: str, password: str):
async with create_client(username, password) as client:
async with Application(client, term) as app:
with Live(app.get_renderable()) as live:
set_logging_console(live.console)
while not app.done():
await app.tick(live.console)
live.update(app.get_renderable(), refresh=True)
Expand Down

0 comments on commit f4f20d3

Please sign in to comment.