Migrate ZHA to new entity naming standard (#74846)

pull/74929/head
David F. Mulcahey 2022-07-10 16:17:59 -04:00 committed by GitHub
parent 8285f42d26
commit 6f28e4bfee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 1210 additions and 1207 deletions

View File

@ -45,6 +45,7 @@ class BaseZhaEntity(LogMixin, entity.Entity):
"""A base class for ZHA entities."""
unique_id_suffix: str | None = None
_attr_has_entity_name = True
def __init__(self, unique_id: str, zha_device: ZHADevice, **kwargs: Any) -> None:
"""Init ZHA entity."""
@ -173,11 +174,13 @@ class ZhaEntity(BaseZhaEntity, RestoreEntity):
) -> None:
"""Init ZHA entity."""
super().__init__(unique_id, zha_device, **kwargs)
ieeetail = "".join([f"{o:02x}" for o in zha_device.ieee[:4]])
ch_names = ", ".join(sorted(ch.name for ch in channels))
self._name: str = f"{zha_device.name} {ieeetail} {ch_names}"
if self.unique_id_suffix:
self._name += f" {self.unique_id_suffix}"
self._name: str = (
self.__class__.__name__.lower()
.replace("zha", "")
.replace("entity", "")
.replace("sensor", "")
.capitalize()
)
self.cluster_channels: dict[str, ZigbeeChannel] = {}
for channel in channels:
self.cluster_channels[channel.name] = channel
@ -260,7 +263,9 @@ class ZhaGroupEntity(BaseZhaEntity):
super().__init__(unique_id, zha_device, **kwargs)
self._available = False
self._group = zha_device.gateway.groups.get(group_id)
self._name = f"{self._group.name}_zha_group_0x{group_id:04x}"
self._name = (
f"{self._group.name}_zha_group_0x{group_id:04x}".lower().capitalize()
)
self._group_id: int = group_id
self._entity_ids: list[str] = entity_ids
self._async_unsub_state_changed: CALLBACK_TYPE | None = None

View File

@ -7,7 +7,7 @@ import zigpy.zcl
import zigpy.zcl.foundation as zcl_f
import homeassistant.components.zha.core.const as zha_const
from homeassistant.util import slugify
from homeassistant.helpers import entity_registry
def patch_cluster(cluster):
@ -133,7 +133,7 @@ async def find_entity_id(domain, zha_device, hass, qualifier=None):
This is used to get the entity id in order to get the state from the state
machine so that we can test state changes.
"""
entities = await find_entity_ids(domain, zha_device, hass)
entities = find_entity_ids(domain, zha_device, hass)
if not entities:
return None
if qualifier:
@ -144,28 +144,26 @@ async def find_entity_id(domain, zha_device, hass, qualifier=None):
return entities[0]
async def find_entity_ids(domain, zha_device, hass):
def find_entity_ids(domain, zha_device, hass):
"""Find the entity ids under the testing.
This is used to get the entity id in order to get the state from the state
machine so that we can test state changes.
"""
ieeetail = "".join([f"{o:02x}" for o in zha_device.ieee[:4]])
head = f"{domain}.{slugify(f'{zha_device.name} {ieeetail}')}"
enitiy_ids = hass.states.async_entity_ids(domain)
await hass.async_block_till_done()
res = []
for entity_id in enitiy_ids:
if entity_id.startswith(head):
res.append(entity_id)
return res
registry = entity_registry.async_get(hass)
return [
entity.entity_id
for entity in entity_registry.async_entries_for_device(
registry, zha_device.device_id
)
if entity.domain == domain
]
def async_find_group_entity_id(hass, domain, group):
"""Find the group entity id under test."""
entity_id = f"{domain}.{group.name.lower().replace(' ','_')}_zha_group_0x{group.group_id:04x}"
entity_id = f"{domain}.fakemanufacturer_fakemodel_{group.name.lower().replace(' ','_')}_zha_group_0x{group.group_id:04x}"
entity_ids = hass.states.async_entity_ids(domain)

View File

@ -345,7 +345,7 @@ async def test_restore_state(hass, zha_device_restored, zigpy_shade_device):
hass,
(
State(
"cover.fakemanufacturer_fakemodel_e769900a_level_on_off_shade",
"cover.fakemanufacturer_fakemodel_shade",
STATE_OPEN,
{ATTR_CURRENT_POSITION: 50},
),

View File

@ -311,7 +311,7 @@ async def test_device_restore_availability(
zha_device = await zha_device_restored(
zigpy_device, last_seen=time.time() - last_seen_delta
)
entity_id = "switch.fakemanufacturer_fakemodel_e769900a_on_off"
entity_id = "switch.fakemanufacturer_fakemodel_switch"
await hass.async_block_till_done()
# ensure the switch entity was created

View File

@ -86,28 +86,28 @@ async def test_get_actions(hass, device_ias):
"domain": Platform.SELECT,
"type": "select_option",
"device_id": reg_device.id,
"entity_id": "select.fakemanufacturer_fakemodel_e769900a_ias_wd_warningmode",
"entity_id": "select.fakemanufacturer_fakemodel_defaulttoneselect",
"metadata": {"secondary": True},
},
{
"domain": Platform.SELECT,
"type": "select_option",
"device_id": reg_device.id,
"entity_id": "select.fakemanufacturer_fakemodel_e769900a_ias_wd_sirenlevel",
"entity_id": "select.fakemanufacturer_fakemodel_defaultsirenlevelselect",
"metadata": {"secondary": True},
},
{
"domain": Platform.SELECT,
"type": "select_option",
"device_id": reg_device.id,
"entity_id": "select.fakemanufacturer_fakemodel_e769900a_ias_wd_strobelevel",
"entity_id": "select.fakemanufacturer_fakemodel_defaultstrobelevelselect",
"metadata": {"secondary": True},
},
{
"domain": Platform.SELECT,
"type": "select_option",
"device_id": reg_device.id,
"entity_id": "select.fakemanufacturer_fakemodel_e769900a_ias_wd_strobe",
"entity_id": "select.fakemanufacturer_fakemodel_defaultstrobeselect",
"metadata": {"secondary": True},
},
]

View File

@ -439,8 +439,8 @@ def test_single_input_cluster_device_class_by_cluster_class():
@pytest.mark.parametrize(
"override, entity_id",
[
(None, "light.manufacturer_model_77665544_level_light_color_on_off"),
("switch", "switch.manufacturer_model_77665544_on_off"),
(None, "light.manufacturer_model_light"),
("switch", "switch.manufacturer_model_switch"),
],
)
async def test_device_override(

View File

@ -133,7 +133,7 @@ async def test_number(hass, zha_device_joined_restored, zigpy_analog_output_devi
assert hass.states.get(entity_id).attributes.get("unit_of_measurement") == "%"
assert (
hass.states.get(entity_id).attributes.get("friendly_name")
== "FakeManufacturer FakeModel e769900a analog_output PWM1"
== "FakeManufacturer FakeModel Number PWM1"
)
# change value from device
@ -210,7 +210,7 @@ async def test_level_control_number(
Platform.NUMBER,
zha_device,
hass,
qualifier=attr,
qualifier=attr.replace("_", ""),
)
assert entity_id is not None

View File

@ -113,12 +113,11 @@ async def test_select(hass, siren):
entity_registry = er.async_get(hass)
zha_device, cluster = siren
assert cluster is not None
select_name = security.IasWd.Warning.WarningMode.__name__
entity_id = await find_entity_id(
Platform.SELECT,
zha_device,
hass,
qualifier=select_name.lower(),
qualifier="tone",
)
assert entity_id is not None
@ -163,7 +162,7 @@ async def test_select_restore_state(
):
"""Test zha select entity restore state."""
entity_id = "select.fakemanufacturer_fakemodel_e769900a_ias_wd_warningmode"
entity_id = "select.fakemanufacturer_fakemodel_defaulttoneselect"
core_rs(entity_id, state="Burglar")
zigpy_device = zigpy_device_mock(
@ -180,12 +179,11 @@ async def test_select_restore_state(
zha_device = await zha_device_restored(zigpy_device)
cluster = zigpy_device.endpoints[1].ias_wd
assert cluster is not None
select_name = security.IasWd.Warning.WarningMode.__name__
entity_id = await find_entity_id(
Platform.SELECT,
zha_device,
hass,
qualifier=select_name.lower(),
qualifier="tone",
)
assert entity_id is not None

View File

@ -48,7 +48,7 @@ from .common import (
)
from .conftest import SIG_EP_INPUT, SIG_EP_OUTPUT, SIG_EP_PROFILE, SIG_EP_TYPE
ENTITY_ID_PREFIX = "sensor.fakemanufacturer_fakemodel_e769900a_{}"
ENTITY_ID_PREFIX = "sensor.fakemanufacturer_fakemodel_{}"
@pytest.fixture(autouse=True)
@ -312,7 +312,7 @@ async def async_test_device_temperature(hass, cluster, entity_id):
),
(
smartenergy.Metering.cluster_id,
"smartenergy_metering_summation_delivered",
"smartenergy_summation",
async_test_smart_energy_summation,
1,
{
@ -360,7 +360,7 @@ async def async_test_device_temperature(hass, cluster, entity_id):
),
(
general.PowerConfiguration.cluster_id,
"power",
"battery",
async_test_powerconfiguration,
2,
{
@ -414,7 +414,7 @@ async def test_sensor(
zigpy_device.node_desc.mac_capability_flags |= 0b_0000_0100
cluster.PLUGGED_ATTR_READS = read_plug
zha_device = await zha_device_joined_restored(zigpy_device)
entity_id = ENTITY_ID_PREFIX.format(entity_suffix)
entity_id = ENTITY_ID_PREFIX.format(entity_suffix.replace("_", ""))
await async_enable_traffic(hass, [zha_device], enabled=False)
await hass.async_block_till_done()
@ -620,7 +620,7 @@ async def test_electrical_measurement_init(
{"apparent_power", "rms_voltage", "rms_current"},
{
"electrical_measurement",
"electrical_measurement_ac_frequency",
"electrical_measurement_frequency",
"electrical_measurement_power_factor",
},
{
@ -636,7 +636,7 @@ async def test_electrical_measurement_init(
{
"electrical_measurement_apparent_power",
"electrical_measurement_rms_current",
"electrical_measurement_ac_frequency",
"electrical_measurement_frequency",
"electrical_measurement_power_factor",
},
),
@ -648,7 +648,7 @@ async def test_electrical_measurement_init(
"electrical_measurement",
"electrical_measurement_apparent_power",
"electrical_measurement_rms_current",
"electrical_measurement_ac_frequency",
"electrical_measurement_frequency",
"electrical_measurement_power_factor",
},
set(),
@ -659,7 +659,7 @@ async def test_electrical_measurement_init(
"instantaneous_demand",
},
{
"smartenergy_metering_summation_delivered",
"smartenergy_summation",
},
{
"smartenergy_metering",
@ -670,7 +670,7 @@ async def test_electrical_measurement_init(
{"instantaneous_demand", "current_summ_delivered"},
{},
{
"smartenergy_metering_summation_delivered",
"smartenergy_summation",
"smartenergy_metering",
},
),
@ -678,7 +678,7 @@ async def test_electrical_measurement_init(
smartenergy.Metering.cluster_id,
{},
{
"smartenergy_metering_summation_delivered",
"smartenergy_summation",
"smartenergy_metering",
},
{},
@ -696,8 +696,10 @@ async def test_unsupported_attributes_sensor(
):
"""Test zha sensor platform."""
entity_ids = {ENTITY_ID_PREFIX.format(e) for e in entity_ids}
missing_entity_ids = {ENTITY_ID_PREFIX.format(e) for e in missing_entity_ids}
entity_ids = {ENTITY_ID_PREFIX.format(e.replace("_", "")) for e in entity_ids}
missing_entity_ids = {
ENTITY_ID_PREFIX.format(e.replace("_", "")) for e in missing_entity_ids
}
zigpy_device = zigpy_device_mock(
{
@ -718,7 +720,7 @@ async def test_unsupported_attributes_sensor(
await async_enable_traffic(hass, [zha_device], enabled=False)
await hass.async_block_till_done()
present_entity_ids = set(await find_entity_ids(Platform.SENSOR, zha_device, hass))
present_entity_ids = set(find_entity_ids(Platform.SENSOR, zha_device, hass))
assert present_entity_ids == entity_ids
assert missing_entity_ids not in present_entity_ids
@ -811,7 +813,7 @@ async def test_se_summation_uom(
):
"""Test zha smart energy summation."""
entity_id = ENTITY_ID_PREFIX.format("smartenergy_metering_summation_delivered")
entity_id = ENTITY_ID_PREFIX.format("smartenergysummation")
zigpy_device = zigpy_device_mock(
{
1: {
@ -865,7 +867,7 @@ async def test_elec_measurement_sensor_type(
):
"""Test zha electrical measurement sensor type."""
entity_id = ENTITY_ID_PREFIX.format("electrical_measurement")
entity_id = ENTITY_ID_PREFIX.format("electricalmeasurement")
zigpy_dev = elec_measurement_zigpy_dev
zigpy_dev.endpoints[1].electrical_measurement.PLUGGED_ATTR_READS[
"measurement_type"
@ -914,7 +916,7 @@ async def test_elec_measurement_skip_unsupported_attribute(
):
"""Test zha electrical measurement skipping update of unsupported attributes."""
entity_id = ENTITY_ID_PREFIX.format("electrical_measurement")
entity_id = ENTITY_ID_PREFIX.format("electricalmeasurement")
zha_dev = elec_measurement_zha_dev
cluster = zha_dev.device.endpoints[1].electrical_measurement

File diff suppressed because it is too large Load Diff