diff --git a/kasa/discover.py b/kasa/discover.py index 9bdae46b..36daeaa9 100755 --- a/kasa/discover.py +++ b/kasa/discover.py @@ -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 diff --git a/kasa/smartdevice.py b/kasa/smartdevice.py index 144c2894..4249807f 100755 --- a/kasa/smartdevice.py +++ b/kasa/smartdevice.py @@ -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).""" diff --git a/kasa/tapo/tapodevice.py b/kasa/tapo/tapodevice.py index 86f93912..8edec611 100644 --- a/kasa/tapo/tapodevice.py +++ b/kasa/tapo/tapodevice.py @@ -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: diff --git a/kasa/tests/test_discovery.py b/kasa/tests/test_discovery.py index 51aedfb7..f0cce517 100644 --- a/kasa/tests/test_discovery.py +++ b/kasa/tests/test_discovery.py @@ -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, diff --git a/kasa/tests/test_strip.py b/kasa/tests/test_strip.py index eb0c848c..451b7e34 100644 --- a/kasa/tests/test_strip.py +++ b/kasa/tests/test_strip.py @@ -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")