Migrate HomeWizard to new entity naming style (#74958)

This commit is contained in:
Duco Sebel 2022-07-12 15:56:16 +02:00 committed by GitHub
parent 7283d1b7fb
commit 5fdae0fc5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 66 deletions

View file

@ -31,25 +31,25 @@ _LOGGER = logging.getLogger(__name__)
SENSORS: Final[tuple[SensorEntityDescription, ...]] = (
SensorEntityDescription(
key="smr_version",
name="DSMR Version",
name="DSMR version",
icon="mdi:counter",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="meter_model",
name="Smart Meter Model",
name="Smart meter model",
icon="mdi:gauge",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="wifi_ssid",
name="Wifi SSID",
name="Wi-Fi SSID",
icon="mdi:wifi",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="wifi_strength",
name="Wifi Strength",
name="Wi-Fi strength",
icon="mdi:wifi",
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
@ -58,77 +58,77 @@ SENSORS: Final[tuple[SensorEntityDescription, ...]] = (
),
SensorEntityDescription(
key="total_power_import_t1_kwh",
name="Total Power Import T1",
name="Total power import T1",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="total_power_import_t2_kwh",
name="Total Power Import T2",
name="Total power import T2",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="total_power_export_t1_kwh",
name="Total Power Export T1",
name="Total power export T1",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="total_power_export_t2_kwh",
name="Total Power Export T2",
name="Total power export T2",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="active_power_w",
name="Active Power",
name="Active power",
native_unit_of_measurement=POWER_WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="active_power_l1_w",
name="Active Power L1",
name="Active power L1",
native_unit_of_measurement=POWER_WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="active_power_l2_w",
name="Active Power L2",
name="Active power L2",
native_unit_of_measurement=POWER_WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="active_power_l3_w",
name="Active Power L3",
name="Active power L3",
native_unit_of_measurement=POWER_WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="total_gas_m3",
name="Total Gas",
name="Total gas",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="active_liter_lpm",
name="Active Water Usage",
name="Active water usage",
native_unit_of_measurement="l/min",
icon="mdi:water",
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="total_liter_m3",
name="Total Water Usage",
name="Total water usage",
native_unit_of_measurement=VOLUME_CUBIC_METERS,
icon="mdi:gauge",
state_class=SensorStateClass.TOTAL_INCREASING,
@ -153,6 +153,8 @@ async def async_setup_entry(
class HWEnergySensor(CoordinatorEntity[HWEnergyDeviceUpdateCoordinator], SensorEntity):
"""Representation of a HomeWizard Sensor."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: HWEnergyDeviceUpdateCoordinator,
@ -166,7 +168,6 @@ class HWEnergySensor(CoordinatorEntity[HWEnergyDeviceUpdateCoordinator], SensorE
self.entry = entry
# Config attributes.
self._attr_name = f"{entry.title} {description.name}"
self.data_type = description.key
self._attr_unique_id = f"{entry.unique_id}_{description.key}"

View file

@ -36,6 +36,8 @@ class HWEnergySwitchEntity(
):
"""Representation switchable entity."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: HWEnergyDeviceUpdateCoordinator,
@ -65,9 +67,6 @@ class HWEnergyMainSwitchEntity(HWEnergySwitchEntity):
"""Initialize the switch."""
super().__init__(coordinator, entry, "power_on")
# Config attributes
self._attr_name = f"{entry.title} Switch"
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
await self.coordinator.api.state_set(power_on=True)
@ -101,6 +100,7 @@ class HWEnergySwitchLockEntity(HWEnergySwitchEntity):
It disables any method that can turn of the relay.
"""
_attr_name = "Switch lock"
_attr_device_class = SwitchDeviceClass.SWITCH
_attr_entity_category = EntityCategory.CONFIG
@ -110,9 +110,6 @@ class HWEnergySwitchLockEntity(HWEnergySwitchEntity):
"""Initialize the switch."""
super().__init__(coordinator, entry, "switch_lock")
# Config attributes
self._attr_name = f"{entry.title} Switch Lock"
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn switch-lock on."""
await self.coordinator.api.state_set(switch_lock=True)

View file

@ -61,7 +61,7 @@ async def test_sensor_entity_smr_version(
assert state.state == "50"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) DSMR Version"
== "Product Name (aabbccddeeff) DSMR version"
)
assert ATTR_STATE_CLASS not in state.attributes
assert ATTR_UNIT_OF_MEASUREMENT not in state.attributes
@ -101,7 +101,7 @@ async def test_sensor_entity_meter_model(
assert state.state == "Model X"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Smart Meter Model"
== "Product Name (aabbccddeeff) Smart meter model"
)
assert ATTR_STATE_CLASS not in state.attributes
assert ATTR_UNIT_OF_MEASUREMENT not in state.attributes
@ -128,8 +128,8 @@ async def test_sensor_entity_wifi_ssid(hass, mock_config_entry_data, mock_config
entity_registry = er.async_get(hass)
state = hass.states.get("sensor.product_name_aabbccddeeff_wifi_ssid")
entry = entity_registry.async_get("sensor.product_name_aabbccddeeff_wifi_ssid")
state = hass.states.get("sensor.product_name_aabbccddeeff_wi_fi_ssid")
entry = entity_registry.async_get("sensor.product_name_aabbccddeeff_wi_fi_ssid")
assert entry
assert state
assert entry.unique_id == "aabbccddeeff_wifi_ssid"
@ -137,7 +137,7 @@ async def test_sensor_entity_wifi_ssid(hass, mock_config_entry_data, mock_config
assert state.state == "My Wifi"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Wifi SSID"
== "Product Name (aabbccddeeff) Wi-Fi SSID"
)
assert ATTR_STATE_CLASS not in state.attributes
assert ATTR_UNIT_OF_MEASUREMENT not in state.attributes
@ -166,7 +166,7 @@ async def test_sensor_entity_wifi_strength(
entity_registry = er.async_get(hass)
entry = entity_registry.async_get("sensor.product_name_aabbccddeeff_wifi_strength")
entry = entity_registry.async_get("sensor.product_name_aabbccddeeff_wi_fi_strength")
assert entry
assert entry.unique_id == "aabbccddeeff_wifi_strength"
assert entry.disabled
@ -206,7 +206,7 @@ async def test_sensor_entity_total_power_import_t1_kwh(
assert state.state == "1234.123"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Total Power Import T1"
== "Product Name (aabbccddeeff) Total power import T1"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
@ -248,7 +248,7 @@ async def test_sensor_entity_total_power_import_t2_kwh(
assert state.state == "1234.123"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Total Power Import T2"
== "Product Name (aabbccddeeff) Total power import T2"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
@ -290,7 +290,7 @@ async def test_sensor_entity_total_power_export_t1_kwh(
assert state.state == "1234.123"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Total Power Export T1"
== "Product Name (aabbccddeeff) Total power export T1"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
@ -332,7 +332,7 @@ async def test_sensor_entity_total_power_export_t2_kwh(
assert state.state == "1234.123"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Total Power Export T2"
== "Product Name (aabbccddeeff) Total power export T2"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
@ -370,7 +370,7 @@ async def test_sensor_entity_active_power(
assert state.state == "123.123"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Active Power"
== "Product Name (aabbccddeeff) Active power"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT
@ -410,7 +410,7 @@ async def test_sensor_entity_active_power_l1(
assert state.state == "123.123"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Active Power L1"
== "Product Name (aabbccddeeff) Active power L1"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT
@ -450,7 +450,7 @@ async def test_sensor_entity_active_power_l2(
assert state.state == "456.456"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Active Power L2"
== "Product Name (aabbccddeeff) Active power L2"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT
@ -490,7 +490,7 @@ async def test_sensor_entity_active_power_l3(
assert state.state == "789.789"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Active Power L3"
== "Product Name (aabbccddeeff) Active power L3"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT
@ -526,7 +526,7 @@ async def test_sensor_entity_total_gas(hass, mock_config_entry_data, mock_config
assert state.state == "50"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Total Gas"
== "Product Name (aabbccddeeff) Total gas"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS
@ -566,7 +566,7 @@ async def test_sensor_entity_active_liters(
assert state.state == "12.345"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Active Water Usage"
== "Product Name (aabbccddeeff) Active water usage"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
@ -607,7 +607,7 @@ async def test_sensor_entity_total_liters(
assert state.state == "1234.567"
assert (
state.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Total Water Usage"
== "Product Name (aabbccddeeff) Total water usage"
)
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING

View file

@ -39,7 +39,7 @@ async def test_switch_entity_not_loaded_when_not_available(
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state_power_on = hass.states.get("sensor.product_name_aabbccddeeff_switch")
state_power_on = hass.states.get("sensor.product_name_aabbccddeeff")
state_switch_lock = hass.states.get("sensor.product_name_aabbccddeeff_switch_lock")
assert state_power_on is None
@ -67,10 +67,8 @@ async def test_switch_loads_entities(hass, mock_config_entry_data, mock_config_e
entity_registry = er.async_get(hass)
state_power_on = hass.states.get("switch.product_name_aabbccddeeff_switch")
entry_power_on = entity_registry.async_get(
"switch.product_name_aabbccddeeff_switch"
)
state_power_on = hass.states.get("switch.product_name_aabbccddeeff")
entry_power_on = entity_registry.async_get("switch.product_name_aabbccddeeff")
assert state_power_on
assert entry_power_on
assert entry_power_on.unique_id == "aabbccddeeff_power_on"
@ -78,7 +76,7 @@ async def test_switch_loads_entities(hass, mock_config_entry_data, mock_config_e
assert state_power_on.state == STATE_OFF
assert (
state_power_on.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Switch"
== "Product Name (aabbccddeeff)"
)
assert state_power_on.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_OUTLET
assert ATTR_ICON not in state_power_on.attributes
@ -95,7 +93,7 @@ async def test_switch_loads_entities(hass, mock_config_entry_data, mock_config_e
assert state_switch_lock.state == STATE_OFF
assert (
state_switch_lock.attributes.get(ATTR_FRIENDLY_NAME)
== "Product Name (aabbccddeeff) Switch Lock"
== "Product Name (aabbccddeeff) Switch lock"
)
assert state_switch_lock.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_SWITCH
assert ATTR_ICON not in state_switch_lock.attributes
@ -127,38 +125,30 @@ async def test_switch_power_on_off(hass, mock_config_entry_data, mock_config_ent
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert (
hass.states.get("switch.product_name_aabbccddeeff_switch").state
== STATE_OFF
)
assert hass.states.get("switch.product_name_aabbccddeeff").state == STATE_OFF
# Turn power_on on
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_ON,
{"entity_id": "switch.product_name_aabbccddeeff_switch"},
{"entity_id": "switch.product_name_aabbccddeeff"},
blocking=True,
)
await hass.async_block_till_done()
assert len(api.state_set.mock_calls) == 1
assert (
hass.states.get("switch.product_name_aabbccddeeff_switch").state == STATE_ON
)
assert hass.states.get("switch.product_name_aabbccddeeff").state == STATE_ON
# Turn power_on off
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_OFF,
{"entity_id": "switch.product_name_aabbccddeeff_switch"},
{"entity_id": "switch.product_name_aabbccddeeff"},
blocking=True,
)
await hass.async_block_till_done()
assert (
hass.states.get("switch.product_name_aabbccddeeff_switch").state
== STATE_OFF
)
assert hass.states.get("switch.product_name_aabbccddeeff").state == STATE_OFF
assert len(api.state_set.mock_calls) == 2
@ -254,9 +244,7 @@ async def test_switch_lock_sets_power_on_unavailable(
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert (
hass.states.get("switch.product_name_aabbccddeeff_switch").state == STATE_ON
)
assert hass.states.get("switch.product_name_aabbccddeeff").state == STATE_ON
assert (
hass.states.get("switch.product_name_aabbccddeeff_switch_lock").state
== STATE_OFF
@ -273,7 +261,7 @@ async def test_switch_lock_sets_power_on_unavailable(
await hass.async_block_till_done()
assert len(api.state_set.mock_calls) == 1
assert (
hass.states.get("switch.product_name_aabbccddeeff_switch").state
hass.states.get("switch.product_name_aabbccddeeff").state
== STATE_UNAVAILABLE
)
assert (
@ -290,9 +278,7 @@ async def test_switch_lock_sets_power_on_unavailable(
)
await hass.async_block_till_done()
assert (
hass.states.get("switch.product_name_aabbccddeeff_switch").state == STATE_ON
)
assert hass.states.get("switch.product_name_aabbccddeeff").state == STATE_ON
assert (
hass.states.get("switch.product_name_aabbccddeeff_switch_lock").state
== STATE_OFF