FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Make __repr__ work on discovery info by rytilahti · Pull Request #1233 · python-kasa/python-kasa · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 5 additions & 3 deletions kasa/device.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,11 @@ async def factory_reset(self) -> None:
"""

def __repr__(self):
if self._last_update is None:
return f"<{self.device_type} at {self.host} - update() needed>"
return f"<{self.device_type} at {self.host} - {self.alias} ({self.model})>"
update_needed = " - update() needed" if not self._last_update else ""
return (
f"<{self.device_type} at {self.host} -"
f" {self.alias} ({self.model}){update_needed}>"
)

_deprecated_device_type_attributes = {
# is_type
Expand Down
4 changes: 3 additions & 1 deletion kasa/smart/smartdevice.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,10 @@ def device_type(self) -> DeviceType:
if self._device_type is not DeviceType.Unknown:
return self._device_type

# Fallback to device_type (from disco info)
type_str = self._info.get("type", self._info.get("device_type"))
self._device_type = self._get_device_type_from_components(
list(self._components.keys()), self._info["type"]
list(self._components.keys()), type_str
)

return self._device_type
Expand Down
24 changes: 24 additions & 0 deletions kasa/tests/test_discovery.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -706,3 +706,27 @@ async def _update(self, *args, **kwargs):
assert dev.config.uses_http is (transport_class != XorTransport)
if transport_class != XorTransport:
assert dev.protocol._transport._http_client.client == session


async def test_discovery_device_repr(discovery_mock, mocker):
"""Test that repr works when only discovery data is available."""
host = "foobar"
ip = "127.0.0.1"

discovery_mock.ip = ip
device_class = Discover._get_device_class(discovery_mock.discovery_data)
update_mock = mocker.patch.object(device_class, "update")

dev = await Discover.discover_single(host, credentials=Credentials())
assert update_mock.call_count == 0

repr_ = repr(dev)
assert dev.host in repr_
assert str(dev.device_type) in repr_
assert dev.model in repr_

# For IOT devices, _last_update is filled from the discovery data
if dev._last_update:
assert "update() needed" not in repr_
else:
assert "update() needed" in repr_

Back | FazBrowse Home | New Git URL