Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions kasa/smart/smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@

ComponentsRaw: TypeAlias = dict[str, list[dict[str, int | str]]]

DEVICE_TYPE_TO_LABEL = {
DeviceType.Plug: "Plug",
DeviceType.Strip: "Power Strip",
DeviceType.StripSocket: "Power Strip",
DeviceType.Dimmer: "Wall Switch",
DeviceType.WallSwitch: "Wall Switch",
DeviceType.Fan: "Fan",
DeviceType.Bulb: "Bulb",
DeviceType.LightStrip: "Light Strip",
DeviceType.Camera: "Camera",
DeviceType.Doorbell: "Doorbell",
DeviceType.Chime: "Chime",
DeviceType.Vacuum: "Vacuum",
DeviceType.Hub: "Hub",
DeviceType.Sensor: "Sensor",
DeviceType.Thermostat: "Hub-Connected Thermostat",
}


# Device must go last as the other interfaces also inherit Device
# and python needs a consistent method resolution order.
Expand Down Expand Up @@ -598,6 +616,8 @@ def alias(self) -> str | None:
"""Returns the device alias or nickname."""
if self._info and (nickname := self._info.get("nickname")):
return base64.b64decode(nickname).decode()
elif label := DEVICE_TYPE_TO_LABEL.get(self._device_type):
return f"{label} ({self.device_id})"
else:
return None

Expand Down
6 changes: 5 additions & 1 deletion tests/smart/test_smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ async def test_device_type_no_update(discovery_mock, caplog: pytest.LogCaptureFi

discovery_result = copy.deepcopy(discovery_mock.discovery_data["result"])

print("TESTERINO", discovery_result)

disco_id = discovery_result["device_id"]

disco_model = discovery_result["device_model"]
short_model, _, _ = disco_model.partition("(")
dev.update_from_discover_info(discovery_result)
Expand All @@ -90,7 +94,7 @@ async def test_device_type_no_update(discovery_mock, caplog: pytest.LogCaptureFi
assert dev.device_type is DeviceType.Plug
assert (
repr(dev)
== f"<DeviceType.Plug at {DISCOVERY_MOCK_IP} - None ({short_model}) - update() needed>"
== f"<DeviceType.Plug at {DISCOVERY_MOCK_IP} - Plug ({disco_id}) ({short_model}) - update() needed>"
)
assert "Unknown device type, falling back to plug" in caplog.text

Expand Down
Loading