Adapt tplink to use has_entity_name (#85577)

* Adapt tplink to use has_entity_name

* Set the name for individual smartstrip sockets

* Fix tests
This commit is contained in:
Teemu R 2023-01-10 09:40:29 +01:00 committed by GitHub
parent 4a22b463d1
commit ca0fe488ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 10 deletions

View file

@ -39,7 +39,7 @@ class CoordinatedTPLinkEntity(CoordinatorEntity[TPLinkDataUpdateCoordinator]):
"""Initialize the switch."""
super().__init__(coordinator)
self.device: SmartDevice = device
self._attr_name = self.device.alias
self._attr_has_entity_name = True
self._attr_unique_id = self.device.device_id
@property

View file

@ -154,14 +154,7 @@ class SmartPlugSensor(CoordinatedTPLinkEntity, SensorEntity):
self._attr_unique_id = (
f"{legacy_device_id(self.device)}_{self.entity_description.key}"
)
@property
def name(self) -> str:
"""Return the name of the Smart Plug.
Overridden to include the description.
"""
return f"{self.device.alias} {self.entity_description.name}"
self._attr_name = self.entity_description.name
@property
def native_value(self) -> float | None:

View file

@ -57,7 +57,7 @@ class SmartPlugLedSwitch(CoordinatedTPLinkEntity, SwitchEntity):
"""Initialize the LED switch."""
super().__init__(device, coordinator)
self._attr_name = f"{device.alias} LED"
self._attr_name = "LED"
self._attr_unique_id = f"{self.device.mac}_led"
@property
@ -93,6 +93,9 @@ class SmartPlugSwitch(CoordinatedTPLinkEntity, SwitchEntity):
super().__init__(device, coordinator)
# For backwards compat with pyHS100
self._attr_unique_id = legacy_device_id(device)
# Define names for single sockets
if device.is_strip_socket:
self._attr_name = device.alias
@async_refresh_after
async def async_turn_on(self, **kwargs: Any) -> None:

View file

@ -149,6 +149,7 @@ def _mocked_plug() -> SmartPlug:
plug.is_dimmer = False
plug.is_strip = False
plug.is_plug = True
plug.is_strip_socket = False
plug.device_id = MAC_ADDRESS
plug.hw_info = {"sw_ver": "1.0.0", "hw_ver": "1.0.0"}
plug.turn_off = AsyncMock()