mirror of
https://github.com/esphome/aioesphomeapi.git
synced 2024-12-22 16:48:04 +01:00
Improve consistency of name logging (#577)
This commit is contained in:
parent
7d24c16ce5
commit
d950d90245
@ -239,7 +239,14 @@ ExecuteServiceDataType = dict[
|
|||||||
|
|
||||||
# pylint: disable=too-many-public-methods
|
# pylint: disable=too-many-public-methods
|
||||||
class APIClient:
|
class APIClient:
|
||||||
__slots__ = ("_params", "_connection", "_cached_name", "_background_tasks", "_loop")
|
__slots__ = (
|
||||||
|
"_params",
|
||||||
|
"_connection",
|
||||||
|
"_cached_name",
|
||||||
|
"_background_tasks",
|
||||||
|
"_loop",
|
||||||
|
"_log_name",
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -283,6 +290,7 @@ class APIClient:
|
|||||||
self._cached_name: str | None = None
|
self._cached_name: str | None = None
|
||||||
self._background_tasks: set[asyncio.Task[Any]] = set()
|
self._background_tasks: set[asyncio.Task[Any]] = set()
|
||||||
self._loop = asyncio.get_event_loop()
|
self._loop = asyncio.get_event_loop()
|
||||||
|
self._set_log_name()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def expected_name(self) -> str | None:
|
def expected_name(self) -> str | None:
|
||||||
@ -296,16 +304,27 @@ class APIClient:
|
|||||||
def address(self) -> str:
|
def address(self) -> str:
|
||||||
return self._params.address
|
return self._params.address
|
||||||
|
|
||||||
@property
|
def _get_log_name(self) -> str:
|
||||||
def _log_name(self) -> str:
|
"""Get the log name of the device."""
|
||||||
if self._cached_name is not None and not self.address.endswith(".local"):
|
address = self.address
|
||||||
return f"{self._cached_name} @ {self.address}"
|
address_is_host = address.endswith(".local")
|
||||||
return self.address
|
if self._cached_name is not None:
|
||||||
|
if address_is_host:
|
||||||
|
return self._cached_name
|
||||||
|
return f"{self._cached_name} @ {address}"
|
||||||
|
if address_is_host:
|
||||||
|
return address[:-6]
|
||||||
|
return address
|
||||||
|
|
||||||
|
def _set_log_name(self) -> None:
|
||||||
|
"""Set the log name of the device."""
|
||||||
|
self._log_name = self._get_log_name()
|
||||||
|
|
||||||
def set_cached_name_if_unset(self, name: str) -> None:
|
def set_cached_name_if_unset(self, name: str) -> None:
|
||||||
"""Set the cached name of the device if not set."""
|
"""Set the cached name of the device if not set."""
|
||||||
if not self._cached_name:
|
if not self._cached_name:
|
||||||
self._cached_name = name
|
self._cached_name = name
|
||||||
|
self._set_log_name()
|
||||||
|
|
||||||
async def connect(
|
async def connect(
|
||||||
self,
|
self,
|
||||||
@ -390,6 +409,7 @@ class APIClient:
|
|||||||
info = DeviceInfo.from_pb(resp)
|
info = DeviceInfo.from_pb(resp)
|
||||||
self._cached_name = info.name
|
self._cached_name = info.name
|
||||||
connection.set_log_name(self._log_name)
|
connection.set_log_name(self._log_name)
|
||||||
|
self._set_log_name()
|
||||||
return info
|
return info
|
||||||
|
|
||||||
async def list_entities_services(
|
async def list_entities_services(
|
||||||
|
@ -78,7 +78,8 @@ class ReconnectLogic(zeroconf.RecordUpdateListener):
|
|||||||
self._log_name = self.name
|
self._log_name = self.name
|
||||||
elif name:
|
elif name:
|
||||||
self.name = name
|
self.name = name
|
||||||
self._log_name = f"{self.name} @ {self._cli.address}"
|
self._log_name = f"{name} @ {self._cli.address}"
|
||||||
|
self._cli.set_cached_name_if_unset(name)
|
||||||
else:
|
else:
|
||||||
self.name = None
|
self.name = None
|
||||||
self._log_name = client.address
|
self._log_name = client.address
|
||||||
@ -276,8 +277,6 @@ class ReconnectLogic(zeroconf.RecordUpdateListener):
|
|||||||
|
|
||||||
async def start(self) -> None:
|
async def start(self) -> None:
|
||||||
"""Start the connecting logic background task."""
|
"""Start the connecting logic background task."""
|
||||||
if self.name:
|
|
||||||
self._cli.set_cached_name_if_unset(self.name)
|
|
||||||
async with self._connected_lock:
|
async with self._connected_lock:
|
||||||
self._is_stopped = False
|
self._is_stopped = False
|
||||||
if self._connection_state != ReconnectLogicState.DISCONNECTED:
|
if self._connection_state != ReconnectLogicState.DISCONNECTED:
|
||||||
|
109
tests/test_reconnect_logic.py
Normal file
109
tests/test_reconnect_logic.py
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from zeroconf.asyncio import AsyncZeroconf
|
||||||
|
|
||||||
|
from aioesphomeapi.client import APIClient
|
||||||
|
from aioesphomeapi.reconnect_logic import ReconnectLogic
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_reconnect_logic_name_from_host():
|
||||||
|
"""Test that the name is set correctly from the host."""
|
||||||
|
cli = APIClient(
|
||||||
|
address="mydevice.local",
|
||||||
|
port=6052,
|
||||||
|
password=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def on_disconnect(expected_disconnect: bool) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def on_connect() -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
rl = ReconnectLogic(
|
||||||
|
client=cli,
|
||||||
|
on_disconnect=on_disconnect,
|
||||||
|
on_connect=on_connect,
|
||||||
|
zeroconf_instance=MagicMock(spec=AsyncZeroconf),
|
||||||
|
)
|
||||||
|
assert rl._log_name == "mydevice"
|
||||||
|
assert cli._log_name == "mydevice"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_reconnect_logic_name_from_host_and_set():
|
||||||
|
"""Test that the name is set correctly from the host."""
|
||||||
|
cli = APIClient(
|
||||||
|
address="mydevice.local",
|
||||||
|
port=6052,
|
||||||
|
password=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def on_disconnect(expected_disconnect: bool) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def on_connect() -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
rl = ReconnectLogic(
|
||||||
|
client=cli,
|
||||||
|
on_disconnect=on_disconnect,
|
||||||
|
on_connect=on_connect,
|
||||||
|
zeroconf_instance=MagicMock(spec=AsyncZeroconf),
|
||||||
|
name="mydevice",
|
||||||
|
)
|
||||||
|
assert rl._log_name == "mydevice"
|
||||||
|
assert cli._log_name == "mydevice"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_reconnect_logic_name_from_address():
|
||||||
|
"""Test that the name is set correctly from the address."""
|
||||||
|
cli = APIClient(
|
||||||
|
address="1.2.3.4",
|
||||||
|
port=6052,
|
||||||
|
password=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def on_disconnect(expected_disconnect: bool) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def on_connect() -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
rl = ReconnectLogic(
|
||||||
|
client=cli,
|
||||||
|
on_disconnect=on_disconnect,
|
||||||
|
on_connect=on_connect,
|
||||||
|
zeroconf_instance=MagicMock(spec=AsyncZeroconf),
|
||||||
|
)
|
||||||
|
assert rl._log_name == "1.2.3.4"
|
||||||
|
assert cli._log_name == "1.2.3.4"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_reconnect_logic_name_from_name():
|
||||||
|
"""Test that the name is set correctly from the address."""
|
||||||
|
cli = APIClient(
|
||||||
|
address="1.2.3.4",
|
||||||
|
port=6052,
|
||||||
|
password=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def on_disconnect(expected_disconnect: bool) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def on_connect() -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
rl = ReconnectLogic(
|
||||||
|
client=cli,
|
||||||
|
on_disconnect=on_disconnect,
|
||||||
|
on_connect=on_connect,
|
||||||
|
zeroconf_instance=MagicMock(spec=AsyncZeroconf),
|
||||||
|
name="mydevice",
|
||||||
|
)
|
||||||
|
assert rl._log_name == "mydevice @ 1.2.3.4"
|
||||||
|
assert cli._log_name == "mydevice @ 1.2.3.4"
|
Loading…
Reference in New Issue
Block a user