Migrate TPLink to has entity name (#96246)

This commit is contained in:
Joost Lekkerkerker 2023-07-24 20:49:40 +02:00 committed by GitHub
parent f8705a8074
commit 2dc86364f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 15 deletions

View file

@ -32,13 +32,14 @@ def async_refresh_after(
class CoordinatedTPLinkEntity(CoordinatorEntity[TPLinkDataUpdateCoordinator]):
"""Common base class for all coordinated tplink entities."""
_attr_has_entity_name = True
def __init__(
self, device: SmartDevice, coordinator: TPLinkDataUpdateCoordinator
) -> None:
"""Initialize the switch."""
super().__init__(coordinator)
self.device: SmartDevice = device
self._attr_name = self.device.alias
self._attr_unique_id = self.device.device_id
@property

View file

@ -162,6 +162,7 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
"""Representation of a TPLink Smart Bulb."""
_attr_supported_features = LightEntityFeature.TRANSITION
_attr_name = None
device: SmartBulb

View file

@ -46,6 +46,7 @@ class TPLinkSensorEntityDescription(SensorEntityDescription):
ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
TPLinkSensorEntityDescription(
key=ATTR_CURRENT_POWER_W,
translation_key="current_consumption",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
@ -55,6 +56,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
),
TPLinkSensorEntityDescription(
key=ATTR_TOTAL_ENERGY_KWH,
translation_key="total_consumption",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
@ -64,6 +66,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
),
TPLinkSensorEntityDescription(
key=ATTR_TODAY_ENERGY_KWH,
translation_key="today_consumption",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
@ -75,7 +78,6 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
name="Voltage",
emeter_attr="voltage",
precision=1,
),
@ -84,7 +86,6 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
name="Current",
emeter_attr="current",
precision=2,
),
@ -155,14 +156,6 @@ class SmartPlugSensor(CoordinatedTPLinkEntity, SensorEntity):
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}"
@property
def native_value(self) -> float | None:
"""Return the sensors state."""

View file

@ -25,6 +25,24 @@
"no_devices_found": "[%key:common::config_flow::abort::no_devices_found%]"
}
},
"entity": {
"sensor": {
"current_consumption": {
"name": "Current consumption"
},
"total_consumption": {
"name": "Total consumption"
},
"today_consumption": {
"name": "Today's consumption"
}
},
"switch": {
"led": {
"name": "LED"
}
}
},
"services": {
"sequence_effect": {
"name": "Sequence effect",

View file

@ -35,7 +35,7 @@ async def async_setup_entry(
# Historically we only add the children if the device is a strip
_LOGGER.debug("Initializing strip with %s sockets", len(device.children))
for child in device.children:
entities.append(SmartPlugSwitch(child, coordinator))
entities.append(SmartPlugSwitchChild(device, coordinator, child))
elif device.is_plug:
entities.append(SmartPlugSwitch(device, coordinator))
@ -49,6 +49,7 @@ class SmartPlugLedSwitch(CoordinatedTPLinkEntity, SwitchEntity):
device: SmartPlug
_attr_translation_key = "led"
_attr_entity_category = EntityCategory.CONFIG
def __init__(
@ -57,7 +58,6 @@ class SmartPlugLedSwitch(CoordinatedTPLinkEntity, SwitchEntity):
"""Initialize the LED switch."""
super().__init__(device, coordinator)
self._attr_name = f"{device.alias} LED"
self._attr_unique_id = f"{self.device.mac}_led"
@property
@ -103,3 +103,29 @@ class SmartPlugSwitch(CoordinatedTPLinkEntity, SwitchEntity):
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
await self.device.turn_off()
class SmartPlugSwitchChild(SmartPlugSwitch):
"""Representation of an individual plug of a TPLink Smart Plug strip."""
def __init__(
self,
device: SmartDevice,
coordinator: TPLinkDataUpdateCoordinator,
plug: SmartDevice,
) -> None:
"""Initialize the switch."""
super().__init__(device, coordinator)
self._plug = plug
self._attr_unique_id = legacy_device_id(plug)
self._attr_name = plug.alias
@async_refresh_after
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
await self._plug.turn_on()
@async_refresh_after
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
await self._plug.turn_off()

View file

@ -147,7 +147,7 @@ async def test_strip(hass: HomeAssistant) -> None:
assert hass.states.get("switch.my_strip") is None
for plug_id in range(2):
entity_id = f"switch.plug{plug_id}"
entity_id = f"switch.my_strip_plug{plug_id}"
state = hass.states.get(entity_id)
assert state.state == STATE_ON
@ -176,7 +176,7 @@ async def test_strip_unique_ids(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
for plug_id in range(2):
entity_id = f"switch.plug{plug_id}"
entity_id = f"switch.my_strip_plug{plug_id}"
entity_registry = er.async_get(hass)
assert (
entity_registry.async_get(entity_id).unique_id == f"PLUG{plug_id}DEVICEID"