mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-04-26 16:46:23 +00:00
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:
parent
fd2170c82c
commit
5b8280a8d9
@ -1,6 +1,5 @@
|
|||||||
"""Discovery module for TP-Link Smart Home devices."""
|
"""Discovery module for TP-Link Smart Home devices."""
|
||||||
import asyncio
|
import asyncio
|
||||||
import base64
|
|
||||||
import binascii
|
import binascii
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import logging
|
import logging
|
||||||
@ -35,9 +34,6 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
OnDiscoveredCallable = Callable[[SmartDevice], Awaitable[None]]
|
OnDiscoveredCallable = Callable[[SmartDevice], Awaitable[None]]
|
||||||
DeviceDict = Dict[str, SmartDevice]
|
DeviceDict = Dict[str, SmartDevice]
|
||||||
|
|
||||||
UNAVAILABLE_ALIAS = "Authentication required"
|
|
||||||
UNAVAILABLE_NICKNAME = base64.b64encode(UNAVAILABLE_ALIAS.encode()).decode()
|
|
||||||
|
|
||||||
|
|
||||||
class _DiscoverProtocol(asyncio.DatagramProtocol):
|
class _DiscoverProtocol(asyncio.DatagramProtocol):
|
||||||
"""Implementation of the discovery protocol handler.
|
"""Implementation of the discovery protocol handler.
|
||||||
@ -463,9 +459,7 @@ class Discover:
|
|||||||
device = device_class(config.host, protocol=protocol)
|
device = device_class(config.host, protocol=protocol)
|
||||||
|
|
||||||
di = discovery_result.get_dict()
|
di = discovery_result.get_dict()
|
||||||
di["model"] = discovery_result.device_model
|
di["model"], _, _ = discovery_result.device_model.partition("(")
|
||||||
di["alias"] = UNAVAILABLE_ALIAS
|
|
||||||
di["nickname"] = UNAVAILABLE_NICKNAME
|
|
||||||
device.update_from_discover_info(di)
|
device.update_from_discover_info(di)
|
||||||
return device
|
return device
|
||||||
|
|
||||||
|
@ -442,11 +442,10 @@ class SmartDevice:
|
|||||||
return self.is_strip
|
return self.is_strip
|
||||||
|
|
||||||
@property # type: ignore
|
@property # type: ignore
|
||||||
@requires_update
|
def alias(self) -> Optional[str]:
|
||||||
def alias(self) -> str:
|
|
||||||
"""Return device name (alias)."""
|
"""Return device name (alias)."""
|
||||||
sys_info = self._sys_info
|
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:
|
async def set_alias(self, alias: str) -> None:
|
||||||
"""Set the device name (alias)."""
|
"""Set the device name (alias)."""
|
||||||
|
@ -102,9 +102,12 @@ class TapoDevice(SmartDevice):
|
|||||||
return str(self._info.get("model"))
|
return str(self._info.get("model"))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def alias(self) -> str:
|
def alias(self) -> Optional[str]:
|
||||||
"""Returns the device alias or nickname."""
|
"""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
|
@property
|
||||||
def time(self) -> datetime:
|
def time(self) -> datetime:
|
||||||
|
@ -108,6 +108,8 @@ async def test_discover_single(discovery_mock, custom_port, mocker):
|
|||||||
assert x._discovery_info is not None
|
assert x._discovery_info is not None
|
||||||
assert x.port == custom_port or x.port == discovery_mock.default_port
|
assert x.port == custom_port or x.port == discovery_mock.default_port
|
||||||
assert update_mock.call_count == 0
|
assert update_mock.call_count == 0
|
||||||
|
if discovery_mock.default_port == 80:
|
||||||
|
assert x.alias is None
|
||||||
|
|
||||||
ct = ConnectionType.from_values(
|
ct = ConnectionType.from_values(
|
||||||
discovery_mock.device_type,
|
discovery_mock.device_type,
|
||||||
|
@ -70,7 +70,7 @@ async def test_children_on_since(dev):
|
|||||||
@strip
|
@strip
|
||||||
async def test_get_plug_by_name(dev: SmartStrip):
|
async def test_get_plug_by_name(dev: SmartStrip):
|
||||||
name = dev.children[0].alias
|
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):
|
with pytest.raises(SmartDeviceException):
|
||||||
dev.get_plug_by_name("NONEXISTING NAME")
|
dev.get_plug_by_name("NONEXISTING NAME")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user