Return alias as None for new discovery devices before update (#627)

* Trim the length of the unavailable device alias

* Update to use short mac as auth required alias

* Update to return alias as none
This commit is contained in:
Steven B 2024-01-11 15:12:02 +00:00 committed by GitHub
parent fd2170c82c
commit 5b8280a8d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 13 deletions

View File

@ -1,6 +1,5 @@
"""Discovery module for TP-Link Smart Home devices."""
import asyncio
import base64
import binascii
import ipaddress
import logging
@ -35,9 +34,6 @@ _LOGGER = logging.getLogger(__name__)
OnDiscoveredCallable = Callable[[SmartDevice], Awaitable[None]]
DeviceDict = Dict[str, SmartDevice]
UNAVAILABLE_ALIAS = "Authentication required"
UNAVAILABLE_NICKNAME = base64.b64encode(UNAVAILABLE_ALIAS.encode()).decode()
class _DiscoverProtocol(asyncio.DatagramProtocol):
"""Implementation of the discovery protocol handler.
@ -463,9 +459,7 @@ class Discover:
device = device_class(config.host, protocol=protocol)
di = discovery_result.get_dict()
di["model"] = discovery_result.device_model
di["alias"] = UNAVAILABLE_ALIAS
di["nickname"] = UNAVAILABLE_NICKNAME
di["model"], _, _ = discovery_result.device_model.partition("(")
device.update_from_discover_info(di)
return device

View File

@ -442,11 +442,10 @@ class SmartDevice:
return self.is_strip
@property # type: ignore
@requires_update
def alias(self) -> str:
def alias(self) -> Optional[str]:
"""Return device name (alias)."""
sys_info = self._sys_info
return str(sys_info["alias"])
return sys_info.get("alias") if sys_info else None
async def set_alias(self, alias: str) -> None:
"""Set the device name (alias)."""

View File

@ -102,9 +102,12 @@ class TapoDevice(SmartDevice):
return str(self._info.get("model"))
@property
def alias(self) -> str:
def alias(self) -> Optional[str]:
"""Returns the device alias or nickname."""
return base64.b64decode(str(self._info.get("nickname"))).decode()
if self._info and (nickname := self._info.get("nickname")):
return base64.b64decode(nickname).decode()
else:
return None
@property
def time(self) -> datetime:

View File

@ -108,6 +108,8 @@ async def test_discover_single(discovery_mock, custom_port, mocker):
assert x._discovery_info is not None
assert x.port == custom_port or x.port == discovery_mock.default_port
assert update_mock.call_count == 0
if discovery_mock.default_port == 80:
assert x.alias is None
ct = ConnectionType.from_values(
discovery_mock.device_type,

View File

@ -70,7 +70,7 @@ async def test_children_on_since(dev):
@strip
async def test_get_plug_by_name(dev: SmartStrip):
name = dev.children[0].alias
assert dev.get_plug_by_name(name) == dev.children[0]
assert dev.get_plug_by_name(name) == dev.children[0] # type: ignore[arg-type]
with pytest.raises(SmartDeviceException):
dev.get_plug_by_name("NONEXISTING NAME")