diff --git a/esphome/components/api/client.py b/esphome/components/api/client.py index 819055ccf4..2001a7b6c3 100644 --- a/esphome/components/api/client.py +++ b/esphome/components/api/client.py @@ -4,10 +4,11 @@ from datetime import datetime from typing import Optional from aioesphomeapi import APIClient, ReconnectLogic, APIConnectionError, LogLevel +from aioesphomeapi.api_pb2 import SubscribeLogsResponse import zeroconf from esphome.const import CONF_KEY, CONF_PORT, CONF_PASSWORD, __version__ -from esphome.util import safe_print +from esphome.core import CORE from . import CONF_ENCRYPTION _LOGGER = logging.getLogger(__name__) @@ -29,11 +30,16 @@ async def async_run_logs(config, address): noise_psk=noise_psk, ) first_connect = True + dashboard = CORE.dashboard - def on_log(msg): - time_ = datetime.now().time().strftime("[%H:%M:%S]") - text = msg.message.decode("utf8", "backslashreplace") - safe_print(time_ + text) + def on_log(msg: SubscribeLogsResponse) -> None: + """Handle a new log message.""" + time_ = datetime.now() + message: bytes = msg.message + text = message.decode("utf8", "backslashreplace") + if dashboard: + text = text.replace("\033", "\\033") + print(f"[{time_.hour:02}:{time_.minute:02}:{time_.second:02}]{text}") async def on_connect(): nonlocal first_connect