Use registry fixtures in tests (z) (#118300)

This commit is contained in:
epenet 2024-05-28 18:25:49 +02:00 committed by GitHub
parent 106cb4cfb7
commit 0b2aac8f4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 526 additions and 372 deletions

View file

@ -64,6 +64,7 @@ from tests.common import MockConfigEntry
)
async def test_migrate_unique_ids(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_zamg_coordinator: MagicMock,
entitydata: dict,
old_unique_id: str,
@ -75,7 +76,6 @@ async def test_migrate_unique_ids(
mock_config_entry = MockConfigEntry(**FIXTURE_CONFIG_ENTRY)
mock_config_entry.add_to_hass(hass)
entity_registry = er.async_get(hass)
entity: er.RegistryEntry = entity_registry.async_get_or_create(
**entitydata,
config_entry=mock_config_entry,
@ -110,6 +110,7 @@ async def test_migrate_unique_ids(
)
async def test_dont_migrate_unique_ids(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_zamg_coordinator: MagicMock,
entitydata: dict,
old_unique_id: str,
@ -121,8 +122,6 @@ async def test_dont_migrate_unique_ids(
mock_config_entry = MockConfigEntry(**FIXTURE_CONFIG_ENTRY)
mock_config_entry.add_to_hass(hass)
entity_registry = er.async_get(hass)
# create existing entry with new_unique_id
existing_entity = entity_registry.async_get_or_create(
WEATHER_DOMAIN,
@ -170,6 +169,7 @@ async def test_dont_migrate_unique_ids(
)
async def test_unload_entry(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_zamg_coordinator: MagicMock,
entitydata: dict,
unique_id: str,
@ -178,8 +178,6 @@ async def test_unload_entry(
mock_config_entry = MockConfigEntry(**FIXTURE_CONFIG_ENTRY)
mock_config_entry.add_to_hass(hass)
entity_registry = er.async_get(hass)
entity_registry.async_get_or_create(
WEATHER_DOMAIN,
ZAMG_DOMAIN,

View file

@ -136,10 +136,11 @@ async def tuya_water_valve(
@freeze_time("2021-11-04 17:37:00", tz_offset=-1)
async def test_button(hass: HomeAssistant, contact_sensor) -> None:
async def test_button(
hass: HomeAssistant, entity_registry: er.EntityRegistry, contact_sensor
) -> None:
"""Test ZHA button platform."""
entity_registry = er.async_get(hass)
zha_device, cluster = contact_sensor
assert cluster is not None
entity_id = find_entity_id(DOMAIN, zha_device, hass)
@ -176,10 +177,11 @@ async def test_button(hass: HomeAssistant, contact_sensor) -> None:
assert state.attributes[ATTR_DEVICE_CLASS] == ButtonDeviceClass.IDENTIFY
async def test_frost_unlock(hass: HomeAssistant, tuya_water_valve) -> None:
async def test_frost_unlock(
hass: HomeAssistant, entity_registry: er.EntityRegistry, tuya_water_valve
) -> None:
"""Test custom frost unlock ZHA button."""
entity_registry = er.async_get(hass)
zha_device, cluster = tuya_water_valve
assert cluster is not None
entity_id = find_entity_id(DOMAIN, zha_device, hass, qualifier="frost_lock_reset")

View file

@ -247,12 +247,13 @@ async def test_check_available_no_basic_cluster_handler(
assert "does not have a mandatory basic cluster" in caplog.text
async def test_ota_sw_version(hass: HomeAssistant, ota_zha_device) -> None:
async def test_ota_sw_version(
hass: HomeAssistant, device_registry: dr.DeviceRegistry, ota_zha_device
) -> None:
"""Test device entry gets sw_version updated via OTA cluster handler."""
ota_ch = ota_zha_device._endpoints[1].client_cluster_handlers["1:0x0019"]
dev_registry = dr.async_get(hass)
entry = dev_registry.async_get(ota_zha_device.device_id)
entry = device_registry.async_get(ota_zha_device.device_id)
assert entry.sw_version is None
cluster = ota_ch.cluster
@ -260,7 +261,7 @@ async def test_ota_sw_version(hass: HomeAssistant, ota_zha_device) -> None:
sw_version = 0x2345
cluster.handle_message(hdr, [1, 2, 3, sw_version, None])
await hass.async_block_till_done()
entry = dev_registry.async_get(ota_zha_device.device_id)
entry = device_registry.async_get(ota_zha_device.device_id)
assert int(entry.sw_version, base=16) == sw_version

View file

@ -103,14 +103,17 @@ async def device_inovelli(hass, zigpy_device_mock, zha_device_joined):
return zigpy_device, zha_device
async def test_get_actions(hass: HomeAssistant, device_ias) -> None:
async def test_get_actions(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
device_ias,
) -> None:
"""Test we get the expected actions from a ZHA device."""
ieee_address = str(device_ias[0].ieee)
device_registry = dr.async_get(hass)
reg_device = device_registry.async_get_device(identifiers={(DOMAIN, ieee_address)})
entity_registry = er.async_get(hass)
siren_level_select = entity_registry.async_get(
"select.fakemanufacturer_fakemodel_default_siren_level"
)
@ -165,15 +168,18 @@ async def test_get_actions(hass: HomeAssistant, device_ias) -> None:
assert actions == unordered(expected_actions)
async def test_get_inovelli_actions(hass: HomeAssistant, device_inovelli) -> None:
async def test_get_inovelli_actions(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
device_inovelli,
) -> None:
"""Test we get the expected actions from a ZHA device."""
inovelli_ieee_address = str(device_inovelli[0].ieee)
device_registry = dr.async_get(hass)
inovelli_reg_device = device_registry.async_get_device(
identifiers={(DOMAIN, inovelli_ieee_address)}
)
entity_registry = er.async_get(hass)
inovelli_button = entity_registry.async_get("button.inovelli_vzm31_sn_identify")
inovelli_light = entity_registry.async_get("light.inovelli_vzm31_sn_light")
@ -248,7 +254,9 @@ async def test_get_inovelli_actions(hass: HomeAssistant, device_inovelli) -> Non
assert actions == unordered(expected_actions)
async def test_action(hass: HomeAssistant, device_ias, device_inovelli) -> None:
async def test_action(
hass: HomeAssistant, device_registry: dr.DeviceRegistry, device_ias, device_inovelli
) -> None:
"""Test for executing a ZHA device action."""
zigpy_device, zha_device = device_ias
inovelli_zigpy_device, inovelli_zha_device = device_inovelli
@ -260,7 +268,6 @@ async def test_action(hass: HomeAssistant, device_ias, device_inovelli) -> None:
ieee_address = str(zha_device.ieee)
inovelli_ieee_address = str(inovelli_zha_device.ieee)
device_registry = dr.async_get(hass)
reg_device = device_registry.async_get_device(identifiers={(DOMAIN, ieee_address)})
inovelli_reg_device = device_registry.async_get_device(
identifiers={(DOMAIN, inovelli_ieee_address)}

View file

@ -93,7 +93,9 @@ async def mock_devices(hass, zigpy_device_mock, zha_device_joined_restored):
return zigpy_device, zha_device
async def test_triggers(hass: HomeAssistant, mock_devices) -> None:
async def test_triggers(
hass: HomeAssistant, device_registry: dr.DeviceRegistry, mock_devices
) -> None:
"""Test ZHA device triggers."""
zigpy_device, zha_device = mock_devices
@ -108,10 +110,7 @@ async def test_triggers(hass: HomeAssistant, mock_devices) -> None:
ieee_address = str(zha_device.ieee)
ha_device_registry = dr.async_get(hass)
reg_device = ha_device_registry.async_get_device(
identifiers={("zha", ieee_address)}
)
reg_device = device_registry.async_get_device(identifiers={("zha", ieee_address)})
triggers = await async_get_device_automations(
hass, DeviceAutomationType.TRIGGER, reg_device.id
@ -170,16 +169,15 @@ async def test_triggers(hass: HomeAssistant, mock_devices) -> None:
assert _same_lists(triggers, expected_triggers)
async def test_no_triggers(hass: HomeAssistant, mock_devices) -> None:
async def test_no_triggers(
hass: HomeAssistant, device_registry: dr.DeviceRegistry, mock_devices
) -> None:
"""Test ZHA device with no triggers."""
_, zha_device = mock_devices
ieee_address = str(zha_device.ieee)
ha_device_registry = dr.async_get(hass)
reg_device = ha_device_registry.async_get_device(
identifiers={("zha", ieee_address)}
)
reg_device = device_registry.async_get_device(identifiers={("zha", ieee_address)})
triggers = await async_get_device_automations(
hass, DeviceAutomationType.TRIGGER, reg_device.id
@ -196,7 +194,9 @@ async def test_no_triggers(hass: HomeAssistant, mock_devices) -> None:
]
async def test_if_fires_on_event(hass: HomeAssistant, mock_devices, calls) -> None:
async def test_if_fires_on_event(
hass: HomeAssistant, device_registry: dr.DeviceRegistry, mock_devices, calls
) -> None:
"""Test for remote triggers firing."""
zigpy_device, zha_device = mock_devices
@ -210,10 +210,7 @@ async def test_if_fires_on_event(hass: HomeAssistant, mock_devices, calls) -> No
}
ieee_address = str(zha_device.ieee)
ha_device_registry = dr.async_get(hass)
reg_device = ha_device_registry.async_get_device(
identifiers={("zha", ieee_address)}
)
reg_device = device_registry.async_get_device(identifiers={("zha", ieee_address)})
assert await async_setup_component(
hass,
@ -314,17 +311,18 @@ async def test_device_offline_fires(
async def test_exception_no_triggers(
hass: HomeAssistant, mock_devices, calls, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
mock_devices,
calls,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test for exception when validating device triggers."""
_, zha_device = mock_devices
ieee_address = str(zha_device.ieee)
ha_device_registry = dr.async_get(hass)
reg_device = ha_device_registry.async_get_device(
identifiers={("zha", ieee_address)}
)
reg_device = device_registry.async_get_device(identifiers={("zha", ieee_address)})
await async_setup_component(
hass,
@ -355,7 +353,11 @@ async def test_exception_no_triggers(
async def test_exception_bad_trigger(
hass: HomeAssistant, mock_devices, calls, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
mock_devices,
calls,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test for exception when validating device triggers."""
@ -370,10 +372,7 @@ async def test_exception_bad_trigger(
}
ieee_address = str(zha_device.ieee)
ha_device_registry = dr.async_get(hass)
reg_device = ha_device_registry.async_get_device(
identifiers={("zha", ieee_address)}
)
reg_device = device_registry.async_get_device(identifiers={("zha", ieee_address)})
await async_setup_component(
hass,
@ -405,6 +404,7 @@ async def test_exception_bad_trigger(
async def test_validate_trigger_config_missing_info(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
config_entry: MockConfigEntry,
zigpy_device_mock,
mock_zigpy_connect: ControllerApplication,
@ -421,8 +421,7 @@ async def test_validate_trigger_config_missing_info(
# it be pulled from the current device, making it impossible to validate triggers
await hass.config_entries.async_unload(config_entry.entry_id)
ha_device_registry = dr.async_get(hass)
reg_device = ha_device_registry.async_get_device(
reg_device = device_registry.async_get_device(
identifiers={("zha", str(switch.ieee))}
)
@ -458,6 +457,7 @@ async def test_validate_trigger_config_missing_info(
async def test_validate_trigger_config_unloaded_bad_info(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
config_entry: MockConfigEntry,
zigpy_device_mock,
mock_zigpy_connect: ControllerApplication,
@ -479,8 +479,7 @@ async def test_validate_trigger_config_unloaded_bad_info(
await hass.async_block_till_done()
await hass.config_entries.async_unload(config_entry.entry_id)
ha_device_registry = dr.async_get(hass)
reg_device = ha_device_registry.async_get_device(
reg_device = device_registry.async_get_device(
identifiers={("zha", str(switch.ieee))}
)

View file

@ -1601,7 +1601,12 @@ async def async_test_flash_from_hass(hass, cluster, entity_id, flash):
new=0,
)
async def test_zha_group_light_entity(
hass: HomeAssistant, device_light_1, device_light_2, device_light_3, coordinator
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
device_light_1,
device_light_2,
device_light_3,
coordinator,
) -> None:
"""Test the light entity for a ZHA group."""
zha_gateway = get_zha_gateway(hass)
@ -1782,7 +1787,6 @@ async def test_zha_group_light_entity(
assert device_3_entity_id not in zha_group.member_entity_ids
# make sure the entity registry entry is still there
entity_registry = er.async_get(hass)
assert entity_registry.async_get(group_entity_id) is not None
# add a member back and ensure that the group entity was created again
@ -1829,6 +1833,7 @@ async def test_zha_group_light_entity(
)
async def test_group_member_assume_state(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
zigpy_device_mock,
zha_device_joined,
coordinator,
@ -1916,7 +1921,6 @@ async def test_group_member_assume_state(
assert hass.states.get(group_entity_id).state == STATE_OFF
# remove the group and ensure that there is no entity and that the entity registry is cleaned up
entity_registry = er.async_get(hass)
assert entity_registry.async_get(group_entity_id) is not None
await zha_gateway.async_remove_zigpy_group(zha_group.group_id)
assert hass.states.get(group_entity_id) is None

View file

@ -61,7 +61,7 @@ async def mock_devices(hass, zigpy_device_mock, zha_device_joined):
async def test_zha_logbook_event_device_with_triggers(
hass: HomeAssistant, mock_devices
hass: HomeAssistant, device_registry: dr.DeviceRegistry, mock_devices
) -> None:
"""Test ZHA logbook events with device and triggers."""
@ -78,10 +78,7 @@ async def test_zha_logbook_event_device_with_triggers(
ieee_address = str(zha_device.ieee)
ha_device_registry = dr.async_get(hass)
reg_device = ha_device_registry.async_get_device(
identifiers={("zha", ieee_address)}
)
reg_device = device_registry.async_get_device(identifiers={("zha", ieee_address)})
hass.config.components.add("recorder")
assert await async_setup_component(hass, "logbook", {})
@ -151,16 +148,13 @@ async def test_zha_logbook_event_device_with_triggers(
async def test_zha_logbook_event_device_no_triggers(
hass: HomeAssistant, mock_devices
hass: HomeAssistant, device_registry: dr.DeviceRegistry, mock_devices
) -> None:
"""Test ZHA logbook events with device and without triggers."""
zigpy_device, zha_device = mock_devices
ieee_address = str(zha_device.ieee)
ha_device_registry = dr.async_get(hass)
reg_device = ha_device_registry.async_get_device(
identifiers={("zha", ieee_address)}
)
reg_device = device_registry.async_get_device(identifiers={("zha", ieee_address)})
hass.config.components.add("recorder")
assert await async_setup_component(hass, "logbook", {})

View file

@ -200,6 +200,7 @@ async def test_number(
)
async def test_level_control_number(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
light: ZHADevice,
zha_device_joined,
attr: str,
@ -207,8 +208,6 @@ async def test_level_control_number(
new_value: int,
) -> None:
"""Test ZHA level control number entities - new join."""
entity_registry = er.async_get(hass)
level_control_cluster = light.endpoints[1].level
level_control_cluster.PLUGGED_ATTR_READS = {
attr: initial_value,
@ -325,6 +324,7 @@ async def test_level_control_number(
)
async def test_color_number(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
light: ZHADevice,
zha_device_joined,
attr: str,
@ -332,8 +332,6 @@ async def test_color_number(
new_value: int,
) -> None:
"""Test ZHA color number entities - new join."""
entity_registry = er.async_get(hass)
color_cluster = light.endpoints[1].light_color
color_cluster.PLUGGED_ATTR_READS = {
attr: initial_value,

View file

@ -119,10 +119,10 @@ def core_rs(hass_storage):
return _storage
async def test_select(hass: HomeAssistant, siren) -> None:
async def test_select(
hass: HomeAssistant, entity_registry: er.EntityRegistry, siren
) -> None:
"""Test ZHA select platform."""
entity_registry = er.async_get(hass)
zha_device, cluster = siren
assert cluster is not None
entity_id = find_entity_id(
@ -206,11 +206,9 @@ async def test_select_restore_state(
async def test_on_off_select_new_join(
hass: HomeAssistant, light, zha_device_joined
hass: HomeAssistant, entity_registry: er.EntityRegistry, light, zha_device_joined
) -> None:
"""Test ZHA on off select - new join."""
entity_registry = er.async_get(hass)
on_off_cluster = light.endpoints[1].on_off
on_off_cluster.PLUGGED_ATTR_READS = {
"start_up_on_off": general.OnOff.StartUpOnOff.On
@ -267,11 +265,9 @@ async def test_on_off_select_new_join(
async def test_on_off_select_restored(
hass: HomeAssistant, light, zha_device_restored
hass: HomeAssistant, entity_registry: er.EntityRegistry, light, zha_device_restored
) -> None:
"""Test ZHA on off select - restored."""
entity_registry = er.async_get(hass)
on_off_cluster = light.endpoints[1].on_off
on_off_cluster.PLUGGED_ATTR_READS = {
"start_up_on_off": general.OnOff.StartUpOnOff.On
@ -464,7 +460,9 @@ async def zigpy_device_aqara_sensor_v2(
async def test_on_off_select_attribute_report_v2(
hass: HomeAssistant, zigpy_device_aqara_sensor_v2
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
zigpy_device_aqara_sensor_v2,
) -> None:
"""Test ZHA attribute report parsing for select platform."""
@ -487,7 +485,6 @@ async def test_on_off_select_attribute_report_v2(
)
assert hass.states.get(entity_id).state == AqaraMotionSensitivities.Low.name
entity_registry = er.async_get(hass)
entity_entry = entity_registry.async_get(entity_id)
assert entity_entry
assert entity_entry.entity_category == EntityCategory.CONFIG

View file

@ -41,7 +41,12 @@ DAY3 = datetime(2020, 4, 21, tzinfo=dt_util.UTC)
],
)
async def test_zodiac_day(
hass: HomeAssistant, now: datetime, sign: str, element: str, modality: str
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
now: datetime,
sign: str,
element: str,
modality: str,
) -> None:
"""Test the zodiac sensor."""
await hass.config.async_set_time_zone("UTC")
@ -75,7 +80,6 @@ async def test_zodiac_day(
"virgo",
]
entity_registry = er.async_get(hass)
entry = entity_registry.async_get("sensor.zodiac")
assert entry
assert entry.unique_id == "zodiac"

View file

@ -289,11 +289,13 @@ async def test_core_config_update(hass: HomeAssistant) -> None:
async def test_reload(
hass: HomeAssistant, hass_admin_user: MockUser, hass_read_only_user: MockUser
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_admin_user: MockUser,
hass_read_only_user: MockUser,
) -> None:
"""Test reload service."""
count_start = len(hass.states.async_entity_ids())
ent_reg = er.async_get(hass)
assert await setup.async_setup_component(
hass,
@ -319,7 +321,7 @@ async def test_reload(
assert state_2.attributes["latitude"] == 3
assert state_2.attributes["longitude"] == 4
assert state_3 is None
assert len(ent_reg.entities) == 0
assert len(entity_registry.entities) == 0
with patch(
"homeassistant.config.load_yaml_config_file",
@ -411,18 +413,20 @@ async def test_ws_list(
async def test_ws_delete(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
entity_registry: er.EntityRegistry,
storage_setup,
) -> None:
"""Test WS delete cleans up entity registry."""
assert await storage_setup()
input_id = "from_storage"
input_entity_id = f"{DOMAIN}.{input_id}"
ent_reg = er.async_get(hass)
state = hass.states.get(input_entity_id)
assert state is not None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
client = await hass_ws_client(hass)
@ -434,11 +438,14 @@ async def test_ws_delete(
state = hass.states.get(input_entity_id)
assert state is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
async def test_update(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
entity_registry: er.EntityRegistry,
storage_setup,
) -> None:
"""Test updating min/max updates the state."""
@ -456,12 +463,11 @@ async def test_update(
input_id = "from_storage"
input_entity_id = f"{DOMAIN}.{input_id}"
ent_reg = er.async_get(hass)
state = hass.states.get(input_entity_id)
assert state.attributes["latitude"] == 1
assert state.attributes["longitude"] == 2
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
client = await hass_ws_client(hass)
@ -485,18 +491,20 @@ async def test_update(
async def test_ws_create(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
entity_registry: er.EntityRegistry,
storage_setup,
) -> None:
"""Test create WS."""
assert await storage_setup(items=[])
input_id = "new_input"
input_entity_id = f"{DOMAIN}.{input_id}"
ent_reg = er.async_get(hass)
state = hass.states.get(input_entity_id)
assert state is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
assert entity_registry.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
client = await hass_ws_client(hass)

View file

@ -111,12 +111,13 @@ async def test_if_fires_on_zone_enter(hass: HomeAssistant, calls) -> None:
assert len(calls) == 1
async def test_if_fires_on_zone_enter_uuid(hass: HomeAssistant, calls) -> None:
async def test_if_fires_on_zone_enter_uuid(
hass: HomeAssistant, entity_registry: er.EntityRegistry, calls
) -> None:
"""Test for firing on zone enter when device is specified by entity registry id."""
context = Context()
registry = er.async_get(hass)
entry = registry.async_get_or_create(
entry = entity_registry.async_get_or_create(
"test", "hue", "1234", suggested_object_id="entity"
)
assert entry.entity_id == "test.entity"

View file

@ -126,6 +126,7 @@ async def test_no_driver(
async def test_network_status(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
multisensor_6,
controller_state,
client,
@ -158,8 +159,7 @@ async def test_network_status(
assert result["controller"]["inclusion_state"] == InclusionState.IDLE
# Try API call with device ID
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
identifiers={(DOMAIN, "3245146787-52")},
)
assert device
@ -251,6 +251,7 @@ async def test_network_status(
async def test_subscribe_node_status(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
multisensor_6_state,
client,
integration,
@ -265,8 +266,7 @@ async def test_subscribe_node_status(
driver = client.driver
driver.controller.nodes[node.node_id] = node
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id, identifiers={get_device_id(driver, node)}
)
@ -461,6 +461,7 @@ async def test_node_metadata(
async def test_node_alerts(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
wallmote_central_scene,
integration,
hass_ws_client: WebSocketGenerator,
@ -468,8 +469,7 @@ async def test_node_alerts(
"""Test the node comments websocket command."""
ws_client = await hass_ws_client(hass)
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_device(identifiers={(DOMAIN, "3245146787-35")})
device = device_registry.async_get_device(identifiers={(DOMAIN, "3245146787-35")})
assert device
await ws_client.send_json(
@ -1650,6 +1650,7 @@ async def test_cancel_inclusion_exclusion(
async def test_remove_node(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
integration,
client,
hass_ws_client: WebSocketGenerator,
@ -1686,10 +1687,8 @@ async def test_remove_node(
msg = await ws_client.receive_json()
assert msg["event"]["event"] == "exclusion started"
dev_reg = dr.async_get(hass)
# Create device registry entry for mock node
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, "3245146787-67")},
name="Node 67",
@ -1701,7 +1700,7 @@ async def test_remove_node(
assert msg["event"]["event"] == "node removed"
# Verify device was removed from device registry
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
identifiers={(DOMAIN, "3245146787-67")},
)
assert device is None
@ -1761,6 +1760,7 @@ async def test_remove_node(
async def test_replace_failed_node(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
nortek_thermostat,
integration,
client,
@ -1772,10 +1772,8 @@ async def test_replace_failed_node(
entry = integration
ws_client = await hass_ws_client(hass)
dev_reg = dr.async_get(hass)
# Create device registry entry for mock node
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, "3245146787-67")},
name="Node 67",
@ -1871,7 +1869,7 @@ async def test_replace_failed_node(
# Verify device was removed from device registry
assert (
dev_reg.async_get_device(
device_registry.async_get_device(
identifiers={(DOMAIN, "3245146787-67")},
)
is None
@ -2110,6 +2108,7 @@ async def test_replace_failed_node(
async def test_remove_failed_node(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
nortek_thermostat,
integration,
client,
@ -2153,10 +2152,8 @@ async def test_remove_failed_node(
msg = await ws_client.receive_json()
assert msg["success"]
dev_reg = dr.async_get(hass)
# Create device registry entry for mock node
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, "3245146787-67")},
name="Node 67",
@ -2169,7 +2166,7 @@ async def test_remove_failed_node(
# Verify device was removed from device registry
assert (
dev_reg.async_get_device(
device_registry.async_get_device(
identifiers={(DOMAIN, "3245146787-67")},
)
is None
@ -4674,6 +4671,7 @@ async def test_subscribe_node_statistics(
async def test_hard_reset_controller(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
client,
integration,
listen_block,
@ -4683,8 +4681,7 @@ async def test_hard_reset_controller(
entry = integration
ws_client = await hass_ws_client(hass)
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
identifiers={get_device_id(client.driver, client.driver.controller.nodes[1])}
)

View file

@ -27,7 +27,7 @@ from tests.common import MockConfigEntry
async def test_low_battery_sensor(
hass: HomeAssistant, multisensor_6, integration
hass: HomeAssistant, entity_registry: er.EntityRegistry, multisensor_6, integration
) -> None:
"""Test boolean binary sensor of type low battery."""
state = hass.states.get(LOW_BATTERY_BINARY_SENSOR)
@ -36,8 +36,7 @@ async def test_low_battery_sensor(
assert state.state == STATE_OFF
assert state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.BATTERY
registry = er.async_get(hass)
entity_entry = registry.async_get(LOW_BATTERY_BINARY_SENSOR)
entity_entry = entity_registry.async_get(LOW_BATTERY_BINARY_SENSOR)
assert entity_entry
assert entity_entry.entity_category is EntityCategory.DIAGNOSTIC
@ -104,28 +103,29 @@ async def test_enabled_legacy_sensor(
async def test_disabled_legacy_sensor(
hass: HomeAssistant, multisensor_6, integration
hass: HomeAssistant, entity_registry: er.EntityRegistry, multisensor_6, integration
) -> None:
"""Test disabled legacy boolean binary sensor."""
# this node has Notification CC implemented so legacy binary sensor should be disabled
registry = er.async_get(hass)
entity_id = DISABLED_LEGACY_BINARY_SENSOR
state = hass.states.get(entity_id)
assert state is None
entry = registry.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.disabled
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
# Test enabling legacy entity
updated_entry = registry.async_update_entity(entry.entity_id, disabled_by=None)
updated_entry = entity_registry.async_update_entity(
entry.entity_id, disabled_by=None
)
assert updated_entry != entry
assert updated_entry.disabled is False
async def test_notification_sensor(
hass: HomeAssistant, multisensor_6, integration
hass: HomeAssistant, entity_registry: er.EntityRegistry, multisensor_6, integration
) -> None:
"""Test binary sensor created from Notification CC."""
state = hass.states.get(NOTIFICATION_MOTION_BINARY_SENSOR)
@ -140,8 +140,7 @@ async def test_notification_sensor(
assert state.state == STATE_OFF
assert state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.TAMPER
registry = er.async_get(hass)
entity_entry = registry.async_get(TAMPER_SENSOR)
entity_entry = entity_registry.async_get(TAMPER_SENSOR)
assert entity_entry
assert entity_entry.entity_category is EntityCategory.DIAGNOSTIC
@ -261,17 +260,19 @@ async def test_property_sensor_door_status(
async def test_config_parameter_binary_sensor(
hass: HomeAssistant, climate_adc_t3000, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
climate_adc_t3000,
integration,
) -> None:
"""Test config parameter binary sensor is created."""
binary_sensor_entity_id = "binary_sensor.adc_t3000_system_configuration_override"
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(binary_sensor_entity_id)
entity_entry = entity_registry.async_get(binary_sensor_entity_id)
assert entity_entry
assert entity_entry.disabled
assert entity_entry.entity_category == EntityCategory.DIAGNOSTIC
updated_entry = ent_reg.async_update_entity(
updated_entry = entity_registry.async_update_entity(
binary_sensor_entity_id, disabled_by=None
)
assert updated_entry != entity_entry

View file

@ -51,6 +51,8 @@ async def test_config_entry_diagnostics(
async def test_device_diagnostics(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
client,
multisensor_6,
integration,
@ -58,8 +60,7 @@ async def test_device_diagnostics(
version_state,
) -> None:
"""Test the device level diagnostics data dump."""
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
identifiers={get_device_id(client.driver, multisensor_6)}
)
assert device
@ -69,8 +70,7 @@ async def test_device_diagnostics(
mock_config_entry.add_to_hass(hass)
# Add an entity entry to the device that is not part of this config entry
ent_reg = er.async_get(hass)
ent_reg.async_get_or_create(
entity_registry.async_get_or_create(
"test",
"test_integration",
"test_unique_id",
@ -78,7 +78,7 @@ async def test_device_diagnostics(
config_entry=mock_config_entry,
device_id=device.id,
)
assert ent_reg.async_get("test.unrelated_entity")
assert entity_registry.async_get("test.unrelated_entity")
# Update a value and ensure it is reflected in the node state
event = Event(
@ -118,7 +118,7 @@ async def test_device_diagnostics(
)
assert any(
entity.entity_id == "test.unrelated_entity"
for entity in er.async_entries_for_device(ent_reg, device.id)
for entity in er.async_entries_for_device(entity_registry, device.id)
)
# Explicitly check that the entity that is not part of this config entry is not
# in the dump.
@ -137,10 +137,11 @@ async def test_device_diagnostics(
}
async def test_device_diagnostics_error(hass: HomeAssistant, integration) -> None:
async def test_device_diagnostics_error(
hass: HomeAssistant, device_registry: dr.DeviceRegistry, integration
) -> None:
"""Test the device diagnostics raises exception when an invalid device is used."""
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=integration.entry_id, identifiers={("test", "test")}
)
with pytest.raises(ValueError):
@ -155,21 +156,21 @@ async def test_empty_zwave_value_matcher() -> None:
async def test_device_diagnostics_missing_primary_value(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
client,
multisensor_6,
integration,
hass_client: ClientSessionGenerator,
) -> None:
"""Test that device diagnostics handles an entity with a missing primary value."""
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
identifiers={get_device_id(client.driver, multisensor_6)}
)
assert device
entity_id = "sensor.multisensor_6_air_temperature"
ent_reg = er.async_get(hass)
entry = ent_reg.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
# check that the primary value for the entity exists in the diagnostics
diagnostics_data = await get_diagnostics_for_device(
@ -227,6 +228,7 @@ async def test_device_diagnostics_missing_primary_value(
async def test_device_diagnostics_secret_value(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
client,
multisensor_6_state,
integration,
@ -256,8 +258,9 @@ async def test_device_diagnostics_secret_value(
client.driver.controller.nodes[node.node_id] = node
client.driver.controller.emit("node added", {"node": node})
await hass.async_block_till_done()
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_device(identifiers={get_device_id(client.driver, node)})
device = device_registry.async_get_device(
identifiers={get_device_id(client.driver, node)}
)
assert device
diagnostics_data = await get_diagnostics_for_device(

View file

@ -135,23 +135,28 @@ async def test_merten_507801(
async def test_shelly_001p10_disabled_entities(
hass: HomeAssistant, client, shelly_qnsh_001P10_shutter, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client,
shelly_qnsh_001P10_shutter,
integration,
) -> None:
"""Test that Shelly 001P10 entity created by endpoint 2 is disabled."""
registry = er.async_get(hass)
entity_ids = [
"cover.wave_shutter_2",
]
for entity_id in entity_ids:
state = hass.states.get(entity_id)
assert state is None
entry = registry.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.disabled
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
# Test enabling entity
updated_entry = registry.async_update_entity(entry.entity_id, disabled_by=None)
updated_entry = entity_registry.async_update_entity(
entry.entity_id, disabled_by=None
)
assert updated_entry != entry
assert updated_entry.disabled is False
@ -161,10 +166,13 @@ async def test_shelly_001p10_disabled_entities(
async def test_merten_507801_disabled_enitites(
hass: HomeAssistant, client, merten_507801, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client,
merten_507801,
integration,
) -> None:
"""Test that Merten 507801 entities created by endpoint 2 are disabled."""
registry = er.async_get(hass)
entity_ids = [
"cover.connect_roller_shutter_2",
"select.connect_roller_shutter_local_protection_state_2",
@ -173,26 +181,31 @@ async def test_merten_507801_disabled_enitites(
for entity_id in entity_ids:
state = hass.states.get(entity_id)
assert state is None
entry = registry.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.disabled
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
# Test enabling entity
updated_entry = registry.async_update_entity(entry.entity_id, disabled_by=None)
updated_entry = entity_registry.async_update_entity(
entry.entity_id, disabled_by=None
)
assert updated_entry != entry
assert updated_entry.disabled is False
async def test_zooz_zen72(
hass: HomeAssistant, client, switch_zooz_zen72, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client,
switch_zooz_zen72,
integration,
) -> None:
"""Test that Zooz ZEN72 Indicators are discovered as number entities."""
ent_reg = er.async_get(hass)
assert len(hass.states.async_entity_ids(NUMBER_DOMAIN)) == 1
assert len(hass.states.async_entity_ids(BUTTON_DOMAIN)) == 2 # includes ping
entity_id = "number.z_wave_plus_700_series_dimmer_switch_indicator_value"
entry = ent_reg.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.entity_category == EntityCategory.CONFIG
state = hass.states.get(entity_id)
@ -222,7 +235,7 @@ async def test_zooz_zen72(
client.async_send_command.reset_mock()
entity_id = "button.z_wave_plus_700_series_dimmer_switch_identify"
entry = ent_reg.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.entity_category == EntityCategory.CONFIG
await hass.services.async_call(
@ -244,18 +257,22 @@ async def test_zooz_zen72(
async def test_indicator_test(
hass: HomeAssistant, client, indicator_test, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
client,
indicator_test,
integration,
) -> None:
"""Test that Indicators are discovered properly.
This test covers indicators that we don't already have device fixtures for.
"""
device = dr.async_get(hass).async_get_device(
device = device_registry.async_get_device(
identifiers={get_device_id(client.driver, indicator_test)}
)
assert device
ent_reg = er.async_get(hass)
entities = er.async_entries_for_device(ent_reg, device.id)
entities = er.async_entries_for_device(entity_registry, device.id)
def len_domain(domain):
return len([entity for entity in entities if entity.domain == domain])
@ -267,7 +284,7 @@ async def test_indicator_test(
assert len_domain(SWITCH_DOMAIN) == 1
entity_id = "binary_sensor.this_is_a_fake_device_binary_sensor"
entry = ent_reg.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.entity_category == EntityCategory.DIAGNOSTIC
state = hass.states.get(entity_id)
@ -277,7 +294,7 @@ async def test_indicator_test(
client.async_send_command.reset_mock()
entity_id = "sensor.this_is_a_fake_device_sensor"
entry = ent_reg.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.entity_category == EntityCategory.DIAGNOSTIC
state = hass.states.get(entity_id)
@ -287,7 +304,7 @@ async def test_indicator_test(
client.async_send_command.reset_mock()
entity_id = "switch.this_is_a_fake_device_switch"
entry = ent_reg.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.entity_category == EntityCategory.CONFIG
state = hass.states.get(entity_id)

View file

@ -13,12 +13,13 @@ from homeassistant.helpers import area_registry as ar, device_registry as dr
from tests.common import MockConfigEntry
async def test_async_get_node_status_sensor_entity_id(hass: HomeAssistant) -> None:
async def test_async_get_node_status_sensor_entity_id(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test async_get_node_status_sensor_entity_id for non zwave_js device."""
dev_reg = dr.async_get(hass)
config_entry = MockConfigEntry()
config_entry.add_to_hass(hass)
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={("test", "test")},
)

View file

@ -181,10 +181,13 @@ async def test_new_entity_on_value_added(
async def test_on_node_added_ready(
hass: HomeAssistant, multisensor_6_state, client, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
multisensor_6_state,
client,
integration,
) -> None:
"""Test we handle a node added event with a ready node."""
dev_reg = dr.async_get(hass)
node = Node(client, deepcopy(multisensor_6_state))
event = {"node": node}
air_temperature_device_id = f"{client.driver.controller.home_id}-{node.node_id}"
@ -192,7 +195,7 @@ async def test_on_node_added_ready(
state = hass.states.get(AIR_TEMPERATURE_SENSOR)
assert not state # entity and device not yet added
assert not dev_reg.async_get_device(
assert not device_registry.async_get_device(
identifiers={(DOMAIN, air_temperature_device_id)}
)
@ -203,18 +206,24 @@ async def test_on_node_added_ready(
assert state # entity and device added
assert state.state != STATE_UNAVAILABLE
assert dev_reg.async_get_device(identifiers={(DOMAIN, air_temperature_device_id)})
assert device_registry.async_get_device(
identifiers={(DOMAIN, air_temperature_device_id)}
)
async def test_on_node_added_not_ready(
hass: HomeAssistant, zp3111_not_ready_state, client, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
zp3111_not_ready_state,
client,
integration,
) -> None:
"""Test we handle a node added event with a non-ready node."""
dev_reg = dr.async_get(hass)
device_id = f"{client.driver.controller.home_id}-{zp3111_not_ready_state['nodeId']}"
assert len(hass.states.async_all()) == 1
assert len(dev_reg.devices) == 1
assert len(device_registry.devices) == 1
node_state = deepcopy(zp3111_not_ready_state)
node_state["isSecure"] = False
@ -231,22 +240,24 @@ async def test_on_node_added_not_ready(
client.driver.receive_event(event)
await hass.async_block_till_done()
device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device
# no extended device identifier yet
assert len(device.identifiers) == 1
ent_reg = er.async_get(hass)
entities = er.async_entries_for_device(ent_reg, device.id)
entities = er.async_entries_for_device(entity_registry, device.id)
# the only entities are the node status sensor, last_seen sensor, and ping button
assert len(entities) == 3
async def test_existing_node_ready(
hass: HomeAssistant, client, multisensor_6, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
client,
multisensor_6,
integration,
) -> None:
"""Test we handle a ready node that exists during integration setup."""
dev_reg = dr.async_get(hass)
node = multisensor_6
air_temperature_device_id = f"{client.driver.controller.home_id}-{node.node_id}"
air_temperature_device_id_ext = (
@ -259,22 +270,24 @@ async def test_existing_node_ready(
assert state # entity and device added
assert state.state != STATE_UNAVAILABLE
device = dev_reg.async_get_device(identifiers={(DOMAIN, air_temperature_device_id)})
device = device_registry.async_get_device(
identifiers={(DOMAIN, air_temperature_device_id)}
)
assert device
assert device == dev_reg.async_get_device(
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, air_temperature_device_id_ext)}
)
async def test_existing_node_reinterview(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
client: Client,
multisensor_6_state: dict,
multisensor_6: Node,
integration: MockConfigEntry,
) -> None:
"""Test we handle a node re-interview firing a node ready event."""
dev_reg = dr.async_get(hass)
node = multisensor_6
assert client.driver is not None
air_temperature_device_id = f"{client.driver.controller.home_id}-{node.node_id}"
@ -288,9 +301,11 @@ async def test_existing_node_reinterview(
assert state # entity and device added
assert state.state != STATE_UNAVAILABLE
device = dev_reg.async_get_device(identifiers={(DOMAIN, air_temperature_device_id)})
device = device_registry.async_get_device(
identifiers={(DOMAIN, air_temperature_device_id)}
)
assert device
assert device == dev_reg.async_get_device(
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, air_temperature_device_id_ext)}
)
assert device.sw_version == "1.12"
@ -313,41 +328,48 @@ async def test_existing_node_reinterview(
assert state
assert state.state != STATE_UNAVAILABLE
device = dev_reg.async_get_device(identifiers={(DOMAIN, air_temperature_device_id)})
device = device_registry.async_get_device(
identifiers={(DOMAIN, air_temperature_device_id)}
)
assert device
assert device == dev_reg.async_get_device(
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, air_temperature_device_id_ext)}
)
assert device.sw_version == "1.13"
async def test_existing_node_not_ready(
hass: HomeAssistant, zp3111_not_ready, client, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
zp3111_not_ready,
client,
integration,
) -> None:
"""Test we handle a non-ready node that exists during integration setup."""
dev_reg = dr.async_get(hass)
node = zp3111_not_ready
device_id = f"{client.driver.controller.home_id}-{node.node_id}"
device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device.name == f"Node {node.node_id}"
assert not device.manufacturer
assert not device.model
assert not device.sw_version
device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device
# no extended device identifier yet
assert len(device.identifiers) == 1
ent_reg = er.async_get(hass)
entities = er.async_entries_for_device(ent_reg, device.id)
entities = er.async_entries_for_device(entity_registry, device.id)
# the only entities are the node status sensor, last_seen sensor, and ping button
assert len(entities) == 3
async def test_existing_node_not_replaced_when_not_ready(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
zp3111,
zp3111_not_ready_state,
zp3111_state,
@ -359,8 +381,6 @@ async def test_existing_node_not_replaced_when_not_ready(
The existing node should not be replaced, and no customization should be lost.
"""
dev_reg = dr.async_get(hass)
er_reg = er.async_get(hass)
kitchen_area = area_registry.async_create("Kitchen")
device_id = f"{client.driver.controller.home_id}-{zp3111.node_id}"
@ -369,7 +389,7 @@ async def test_existing_node_not_replaced_when_not_ready(
f"{zp3111.product_type}:{zp3111.product_id}"
)
device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device
assert device.name == "4-in-1 Sensor"
assert not device.name_by_user
@ -377,18 +397,20 @@ async def test_existing_node_not_replaced_when_not_ready(
assert device.model == "ZP3111-5"
assert device.sw_version == "5.1"
assert not device.area_id
assert device == dev_reg.async_get_device(identifiers={(DOMAIN, device_id_ext)})
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, device_id_ext)}
)
motion_entity = "binary_sensor.4_in_1_sensor_motion_detection"
state = hass.states.get(motion_entity)
assert state
assert state.name == "4-in-1 Sensor Motion detection"
dev_reg.async_update_device(
device_registry.async_update_device(
device.id, name_by_user="Custom Device Name", area_id=kitchen_area.id
)
custom_device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
custom_device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert custom_device
assert custom_device.name == "4-in-1 Sensor"
assert custom_device.name_by_user == "Custom Device Name"
@ -396,12 +418,12 @@ async def test_existing_node_not_replaced_when_not_ready(
assert custom_device.model == "ZP3111-5"
assert device.sw_version == "5.1"
assert custom_device.area_id == kitchen_area.id
assert custom_device == dev_reg.async_get_device(
assert custom_device == device_registry.async_get_device(
identifiers={(DOMAIN, device_id_ext)}
)
custom_entity = "binary_sensor.custom_motion_sensor"
er_reg.async_update_entity(
entity_registry.async_update_entity(
motion_entity, new_entity_id=custom_entity, name="Custom Entity Name"
)
await hass.async_block_till_done()
@ -425,9 +447,11 @@ async def test_existing_node_not_replaced_when_not_ready(
client.driver.receive_event(event)
await hass.async_block_till_done()
device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device
assert device == dev_reg.async_get_device(identifiers={(DOMAIN, device_id_ext)})
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, device_id_ext)}
)
assert device.id == custom_device.id
assert device.identifiers == custom_device.identifiers
assert device.name == f"Node {zp3111.node_id}"
@ -453,9 +477,11 @@ async def test_existing_node_not_replaced_when_not_ready(
client.driver.receive_event(event)
await hass.async_block_till_done()
device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device
assert device == dev_reg.async_get_device(identifiers={(DOMAIN, device_id_ext)})
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, device_id_ext)}
)
assert device.id == custom_device.id
assert device.identifiers == custom_device.identifiers
assert device.name == "4-in-1 Sensor"
@ -959,6 +985,7 @@ async def test_remove_entry(
async def test_removed_device(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
client,
climate_radio_thermostat_ct100_plus,
lock_schlage_be469,
@ -971,8 +998,9 @@ async def test_removed_device(
assert len(driver.controller.nodes) == 3
# Make sure there are the same number of devices
dev_reg = dr.async_get(hass)
device_entries = dr.async_entries_for_config_entry(dev_reg, integration.entry_id)
device_entries = dr.async_entries_for_config_entry(
device_registry, integration.entry_id
)
assert len(device_entries) == 3
# Remove a node and reload the entry
@ -981,32 +1009,41 @@ async def test_removed_device(
await hass.async_block_till_done()
# Assert that the node was removed from the device registry
device_entries = dr.async_entries_for_config_entry(dev_reg, integration.entry_id)
device_entries = dr.async_entries_for_config_entry(
device_registry, integration.entry_id
)
assert len(device_entries) == 2
assert (
dev_reg.async_get_device(identifiers={get_device_id(driver, old_node)}) is None
device_registry.async_get_device(identifiers={get_device_id(driver, old_node)})
is None
)
async def test_suggested_area(hass: HomeAssistant, client, eaton_rf9640_dimmer) -> None:
async def test_suggested_area(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
client,
eaton_rf9640_dimmer,
) -> None:
"""Test that suggested area works."""
dev_reg = dr.async_get(hass)
ent_reg = er.async_get(hass)
entry = MockConfigEntry(domain="zwave_js", data={"url": "ws://test.org"})
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
entity = ent_reg.async_get(EATON_RF9640_ENTITY)
assert dev_reg.async_get(entity.device_id).area_id is not None
entity = entity_registry.async_get(EATON_RF9640_ENTITY)
assert device_registry.async_get(entity.device_id).area_id is not None
async def test_node_removed(
hass: HomeAssistant, multisensor_6_state, client, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
multisensor_6_state,
client,
integration,
) -> None:
"""Test that device gets removed when node gets removed."""
dev_reg = dr.async_get(hass)
node = Node(client, deepcopy(multisensor_6_state))
device_id = f"{client.driver.controller.home_id}-{node.node_id}"
event = {
@ -1018,7 +1055,7 @@ async def test_node_removed(
client.driver.controller.receive_event(Event("node added", event))
await hass.async_block_till_done()
old_device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
old_device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert old_device
assert old_device.id
@ -1027,14 +1064,18 @@ async def test_node_removed(
client.driver.controller.emit("node removed", event)
await hass.async_block_till_done()
# Assert device has been removed
assert not dev_reg.async_get(old_device.id)
assert not device_registry.async_get(old_device.id)
async def test_replace_same_node(
hass: HomeAssistant, multisensor_6, multisensor_6_state, client, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
multisensor_6,
multisensor_6_state,
client,
integration,
) -> None:
"""Test when a node is replaced with itself that the device remains."""
dev_reg = dr.async_get(hass)
node_id = multisensor_6.node_id
multisensor_6_state = deepcopy(multisensor_6_state)
@ -1044,9 +1085,9 @@ async def test_replace_same_node(
f"{multisensor_6.product_type}:{multisensor_6.product_id}"
)
device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device
assert device == dev_reg.async_get_device(
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, multisensor_6_device_id)}
)
assert device.manufacturer == "AEON Labs"
@ -1070,7 +1111,7 @@ async def test_replace_same_node(
await hass.async_block_till_done()
# Device should still be there after the node was removed
device = dev_reg.async_get(dev_id)
device = device_registry.async_get(dev_id)
assert device
# When the node is replaced, a non-ready node added event is emitted
@ -1108,7 +1149,7 @@ async def test_replace_same_node(
client.driver.receive_event(event)
await hass.async_block_till_done()
device = dev_reg.async_get(dev_id)
device = device_registry.async_get(dev_id)
assert device
event = Event(
@ -1124,10 +1165,10 @@ async def test_replace_same_node(
await hass.async_block_till_done()
# Device is the same
device = dev_reg.async_get(dev_id)
device = device_registry.async_get(dev_id)
assert device
assert device == dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
assert device == dev_reg.async_get_device(
assert device == device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, multisensor_6_device_id)}
)
assert device.manufacturer == "AEON Labs"
@ -1138,6 +1179,7 @@ async def test_replace_same_node(
async def test_replace_different_node(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
multisensor_6,
multisensor_6_state,
hank_binary_switch_state,
@ -1146,7 +1188,6 @@ async def test_replace_different_node(
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test when a node is replaced with a different node."""
dev_reg = dr.async_get(hass)
node_id = multisensor_6.node_id
state = deepcopy(hank_binary_switch_state)
state["nodeId"] = node_id
@ -1162,9 +1203,9 @@ async def test_replace_different_node(
f"{state['productId']}"
)
device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device
assert device == dev_reg.async_get_device(
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, multisensor_6_device_id_ext)}
)
assert device.manufacturer == "AEON Labs"
@ -1187,7 +1228,7 @@ async def test_replace_different_node(
await hass.async_block_till_done()
# Device should still be there after the node was removed
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
identifiers={(DOMAIN, multisensor_6_device_id_ext)}
)
assert device
@ -1230,7 +1271,7 @@ async def test_replace_different_node(
client.driver.receive_event(event)
await hass.async_block_till_done()
device = dev_reg.async_get(dev_id)
device = device_registry.async_get(dev_id)
assert device
event = Event(
@ -1247,16 +1288,18 @@ async def test_replace_different_node(
# node ID based device identifier should be moved from the old multisensor device
# to the new hank device and both the old and new devices should exist.
new_device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
new_device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert new_device
hank_device = dev_reg.async_get_device(identifiers={(DOMAIN, hank_device_id_ext)})
hank_device = device_registry.async_get_device(
identifiers={(DOMAIN, hank_device_id_ext)}
)
assert hank_device
assert hank_device == new_device
assert hank_device.identifiers == {
(DOMAIN, device_id),
(DOMAIN, hank_device_id_ext),
}
multisensor_6_device = dev_reg.async_get_device(
multisensor_6_device = device_registry.async_get_device(
identifiers={(DOMAIN, multisensor_6_device_id_ext)}
)
assert multisensor_6_device
@ -1287,7 +1330,9 @@ async def test_replace_different_node(
await hass.async_block_till_done()
# Device should still be there after the node was removed
device = dev_reg.async_get_device(identifiers={(DOMAIN, hank_device_id_ext)})
device = device_registry.async_get_device(
identifiers={(DOMAIN, hank_device_id_ext)}
)
assert device
assert len(device.identifiers) == 2
@ -1344,13 +1389,15 @@ async def test_replace_different_node(
# node ID based device identifier should be moved from the new hank device
# to the old multisensor device and both the old and new devices should exist.
old_device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
old_device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert old_device
hank_device = dev_reg.async_get_device(identifiers={(DOMAIN, hank_device_id_ext)})
hank_device = device_registry.async_get_device(
identifiers={(DOMAIN, hank_device_id_ext)}
)
assert hank_device
assert hank_device != old_device
assert hank_device.identifiers == {(DOMAIN, hank_device_id_ext)}
multisensor_6_device = dev_reg.async_get_device(
multisensor_6_device = device_registry.async_get_device(
identifiers={(DOMAIN, multisensor_6_device_id_ext)}
)
assert multisensor_6_device
@ -1383,15 +1430,17 @@ async def test_replace_different_node(
async def test_node_model_change(
hass: HomeAssistant, zp3111, client, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
zp3111,
client,
integration,
) -> None:
"""Test when a node's model is changed due to an updated device config file.
The device and entities should not be removed.
"""
dev_reg = dr.async_get(hass)
er_reg = er.async_get(hass)
device_id = f"{client.driver.controller.home_id}-{zp3111.node_id}"
device_id_ext = (
f"{device_id}-{zp3111.manufacturer_id}:"
@ -1399,9 +1448,11 @@ async def test_node_model_change(
)
# Verify device and entities have default names/ids
device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device
assert device == dev_reg.async_get_device(identifiers={(DOMAIN, device_id_ext)})
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, device_id_ext)}
)
assert device.manufacturer == "Vision Security"
assert device.model == "ZP3111-5"
assert device.name == "4-in-1 Sensor"
@ -1415,18 +1466,20 @@ async def test_node_model_change(
assert state.name == "4-in-1 Sensor Motion detection"
# Customize device and entity names/ids
dev_reg.async_update_device(device.id, name_by_user="Custom Device Name")
device = dev_reg.async_get_device(identifiers={(DOMAIN, device_id)})
device_registry.async_update_device(device.id, name_by_user="Custom Device Name")
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
assert device
assert device.id == dev_id
assert device == dev_reg.async_get_device(identifiers={(DOMAIN, device_id_ext)})
assert device == device_registry.async_get_device(
identifiers={(DOMAIN, device_id_ext)}
)
assert device.manufacturer == "Vision Security"
assert device.model == "ZP3111-5"
assert device.name == "4-in-1 Sensor"
assert device.name_by_user == "Custom Device Name"
custom_entity = "binary_sensor.custom_motion_sensor"
er_reg.async_update_entity(
entity_registry.async_update_entity(
motion_entity, new_entity_id=custom_entity, name="Custom Entity Name"
)
await hass.async_block_till_done()
@ -1452,7 +1505,7 @@ async def test_node_model_change(
await hass.async_block_till_done()
# Device name changes, but the customization is the same
device = dev_reg.async_get(dev_id)
device = device_registry.async_get(dev_id)
assert device
assert device.id == dev_id
assert device.manufacturer == "New Device Manufacturer"
@ -1493,17 +1546,15 @@ async def test_disabled_node_status_entity_on_node_replaced(
async def test_disabled_entity_on_value_removed(
hass: HomeAssistant, zp3111, client, integration
hass: HomeAssistant, entity_registry: er.EntityRegistry, zp3111, client, integration
) -> None:
"""Test that when entity primary values are removed the entity is removed."""
er_reg = er.async_get(hass)
# re-enable this default-disabled entity
sensor_cover_entity = "sensor.4_in_1_sensor_home_security_cover_status"
idle_cover_status_button_entity = (
"button.4_in_1_sensor_idle_home_security_cover_status"
)
er_reg.async_update_entity(entity_id=sensor_cover_entity, disabled_by=None)
entity_registry.async_update_entity(entity_id=sensor_cover_entity, disabled_by=None)
await hass.async_block_till_done()
# must reload the integration when enabling an entity
@ -1778,10 +1829,14 @@ async def test_server_logging(hass: HomeAssistant, client) -> None:
async def test_factory_reset_node(
hass: HomeAssistant, client, multisensor_6, multisensor_6_state, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
client,
multisensor_6,
multisensor_6_state,
integration,
) -> None:
"""Test when a node is removed because it was reset."""
dev_reg = dr.async_get(hass)
# One config entry scenario
remove_event = Event(
type="node removed",
@ -1803,7 +1858,7 @@ async def test_factory_reset_node(
assert "with the home ID" not in notifications[msg_id]["message"]
async_dismiss(hass, msg_id)
await hass.async_block_till_done()
assert not dev_reg.async_get_device(identifiers={dev_id})
assert not device_registry.async_get_device(identifiers={dev_id})
# Add mock config entry to simulate having multiple entries
new_entry = MockConfigEntry(domain=DOMAIN)

View file

@ -865,13 +865,16 @@ async def test_black_is_off_zdb5100(
async def test_basic_cc_light(
hass: HomeAssistant, client, ge_in_wall_dimmer_switch, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client,
ge_in_wall_dimmer_switch,
integration,
) -> None:
"""Test light is created from Basic CC."""
node = ge_in_wall_dimmer_switch
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(BASIC_LIGHT_ENTITY)
entity_entry = entity_registry.async_get(BASIC_LIGHT_ENTITY)
assert entity_entry
assert not entity_entry.disabled

View file

@ -15,11 +15,14 @@ from tests.components.logbook.common import MockRow, mock_humanify
async def test_humanifying_zwave_js_notification_event(
hass: HomeAssistant, client, lock_schlage_be469, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
client,
lock_schlage_be469,
integration,
) -> None:
"""Test humanifying Z-Wave JS notification events."""
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
identifiers={get_device_id(client.driver, lock_schlage_be469)}
)
assert device
@ -99,11 +102,14 @@ async def test_humanifying_zwave_js_notification_event(
async def test_humanifying_zwave_js_value_notification_event(
hass: HomeAssistant, client, lock_schlage_be469, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
client,
lock_schlage_be469,
integration,
) -> None:
"""Test humanifying Z-Wave JS value notification events."""
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
identifiers={get_device_id(client.driver, lock_schlage_be469)}
)
assert device

View file

@ -14,18 +14,20 @@ from .common import AIR_TEMPERATURE_SENSOR, NOTIFICATION_MOTION_BINARY_SENSOR
async def test_unique_id_migration_dupes(
hass: HomeAssistant, multisensor_6_state, client, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
multisensor_6_state,
client,
integration,
) -> None:
"""Test we remove an entity when ."""
ent_reg = er.async_get(hass)
entity_name = AIR_TEMPERATURE_SENSOR.split(".")[1]
# Create entity RegistryEntry using old unique ID format
old_unique_id_1 = (
f"{client.driver.controller.home_id}.52.52-49-00-Air temperature-00"
)
entity_entry = ent_reg.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id_1,
@ -40,7 +42,7 @@ async def test_unique_id_migration_dupes(
old_unique_id_2 = (
f"{client.driver.controller.home_id}.52.52-49-0-Air temperature-00-00"
)
entity_entry = ent_reg.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id_2,
@ -59,11 +61,15 @@ async def test_unique_id_migration_dupes(
await hass.async_block_till_done()
# Check that new RegistryEntry is using new unique ID format
entity_entry = ent_reg.async_get(AIR_TEMPERATURE_SENSOR)
entity_entry = entity_registry.async_get(AIR_TEMPERATURE_SENSOR)
new_unique_id = f"{client.driver.controller.home_id}.52-49-0-Air temperature"
assert entity_entry.unique_id == new_unique_id
assert ent_reg.async_get_entity_id("sensor", DOMAIN, old_unique_id_1) is None
assert ent_reg.async_get_entity_id("sensor", DOMAIN, old_unique_id_2) is None
assert (
entity_registry.async_get_entity_id("sensor", DOMAIN, old_unique_id_1) is None
)
assert (
entity_registry.async_get_entity_id("sensor", DOMAIN, old_unique_id_2) is None
)
@pytest.mark.parametrize(
@ -75,17 +81,20 @@ async def test_unique_id_migration_dupes(
],
)
async def test_unique_id_migration(
hass: HomeAssistant, multisensor_6_state, client, integration, id
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
multisensor_6_state,
client,
integration,
id,
) -> None:
"""Test unique ID is migrated from old format to new."""
ent_reg = er.async_get(hass)
# Migrate version 1
entity_name = AIR_TEMPERATURE_SENSOR.split(".")[1]
# Create entity RegistryEntry using old unique ID format
old_unique_id = f"{client.driver.controller.home_id}.{id}"
entity_entry = ent_reg.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id,
@ -104,10 +113,10 @@ async def test_unique_id_migration(
await hass.async_block_till_done()
# Check that new RegistryEntry is using new unique ID format
entity_entry = ent_reg.async_get(AIR_TEMPERATURE_SENSOR)
entity_entry = entity_registry.async_get(AIR_TEMPERATURE_SENSOR)
new_unique_id = f"{client.driver.controller.home_id}.52-49-0-Air temperature"
assert entity_entry.unique_id == new_unique_id
assert ent_reg.async_get_entity_id("sensor", DOMAIN, old_unique_id) is None
assert entity_registry.async_get_entity_id("sensor", DOMAIN, old_unique_id) is None
@pytest.mark.parametrize(
@ -119,17 +128,20 @@ async def test_unique_id_migration(
],
)
async def test_unique_id_migration_property_key(
hass: HomeAssistant, hank_binary_switch_state, client, integration, id
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hank_binary_switch_state,
client,
integration,
id,
) -> None:
"""Test unique ID with property key is migrated from old format to new."""
ent_reg = er.async_get(hass)
SENSOR_NAME = "sensor.smart_plug_with_two_usb_ports_value_electric_consumed"
entity_name = SENSOR_NAME.split(".")[1]
# Create entity RegistryEntry using old unique ID format
old_unique_id = f"{client.driver.controller.home_id}.{id}"
entity_entry = ent_reg.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id,
@ -148,18 +160,20 @@ async def test_unique_id_migration_property_key(
await hass.async_block_till_done()
# Check that new RegistryEntry is using new unique ID format
entity_entry = ent_reg.async_get(SENSOR_NAME)
entity_entry = entity_registry.async_get(SENSOR_NAME)
new_unique_id = f"{client.driver.controller.home_id}.32-50-0-value-66049"
assert entity_entry.unique_id == new_unique_id
assert ent_reg.async_get_entity_id("sensor", DOMAIN, old_unique_id) is None
assert entity_registry.async_get_entity_id("sensor", DOMAIN, old_unique_id) is None
async def test_unique_id_migration_notification_binary_sensor(
hass: HomeAssistant, multisensor_6_state, client, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
multisensor_6_state,
client,
integration,
) -> None:
"""Test unique ID is migrated from old format to new for a notification binary sensor."""
ent_reg = er.async_get(hass)
entity_name = NOTIFICATION_MOTION_BINARY_SENSOR.split(".")[1]
# Create entity RegistryEntry using old unique ID format
@ -167,7 +181,7 @@ async def test_unique_id_migration_notification_binary_sensor(
f"{client.driver.controller.home_id}.52.52-113-00-Home Security-Motion sensor"
" status.8"
)
entity_entry = ent_reg.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
"binary_sensor",
DOMAIN,
old_unique_id,
@ -186,26 +200,32 @@ async def test_unique_id_migration_notification_binary_sensor(
await hass.async_block_till_done()
# Check that new RegistryEntry is using new unique ID format
entity_entry = ent_reg.async_get(NOTIFICATION_MOTION_BINARY_SENSOR)
entity_entry = entity_registry.async_get(NOTIFICATION_MOTION_BINARY_SENSOR)
new_unique_id = (
f"{client.driver.controller.home_id}.52-113-0-Home Security-Motion sensor"
" status.8"
)
assert entity_entry.unique_id == new_unique_id
assert ent_reg.async_get_entity_id("binary_sensor", DOMAIN, old_unique_id) is None
assert (
entity_registry.async_get_entity_id("binary_sensor", DOMAIN, old_unique_id)
is None
)
async def test_old_entity_migration(
hass: HomeAssistant, hank_binary_switch_state, client, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
hank_binary_switch_state,
client,
integration,
) -> None:
"""Test old entity on a different endpoint is migrated to a new one."""
node = Node(client, copy.deepcopy(hank_binary_switch_state))
driver = client.driver
assert driver
ent_reg = er.async_get(hass)
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=integration.entry_id,
identifiers={get_device_id(driver, node)},
manufacturer=hank_binary_switch_state["deviceConfig"]["manufacturer"],
@ -217,7 +237,7 @@ async def test_old_entity_migration(
# Create entity RegistryEntry using fake endpoint
old_unique_id = f"{driver.controller.home_id}.32-50-1-value-66049"
entity_entry = ent_reg.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id,
@ -237,23 +257,28 @@ async def test_old_entity_migration(
await hass.async_block_till_done()
# Check that new RegistryEntry is using new unique ID format
entity_entry = ent_reg.async_get(SENSOR_NAME)
entity_entry = entity_registry.async_get(SENSOR_NAME)
new_unique_id = f"{client.driver.controller.home_id}.32-50-0-value-66049"
assert entity_entry.unique_id == new_unique_id
assert ent_reg.async_get_entity_id("sensor", DOMAIN, old_unique_id) is None
assert (
entity_registry.async_get_entity_id("sensor", DOMAIN, old_unique_id) is None
)
async def test_different_endpoint_migration_status_sensor(
hass: HomeAssistant, hank_binary_switch_state, client, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
hank_binary_switch_state,
client,
integration,
) -> None:
"""Test that the different endpoint migration logic skips over the status sensor."""
node = Node(client, copy.deepcopy(hank_binary_switch_state))
driver = client.driver
assert driver
ent_reg = er.async_get(hass)
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=integration.entry_id,
identifiers={get_device_id(driver, node)},
manufacturer=hank_binary_switch_state["deviceConfig"]["manufacturer"],
@ -265,7 +290,7 @@ async def test_different_endpoint_migration_status_sensor(
# Create entity RegistryEntry using fake endpoint
old_unique_id = f"{driver.controller.home_id}.32.node_status"
entity_entry = ent_reg.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id,
@ -285,21 +310,24 @@ async def test_different_endpoint_migration_status_sensor(
await hass.async_block_till_done()
# Check that the RegistryEntry is using the same unique ID
entity_entry = ent_reg.async_get(SENSOR_NAME)
entity_entry = entity_registry.async_get(SENSOR_NAME)
assert entity_entry.unique_id == old_unique_id
async def test_skip_old_entity_migration_for_multiple(
hass: HomeAssistant, hank_binary_switch_state, client, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
hank_binary_switch_state,
client,
integration,
) -> None:
"""Test that multiple entities of the same value but on a different endpoint get skipped."""
node = Node(client, copy.deepcopy(hank_binary_switch_state))
driver = client.driver
assert driver
ent_reg = er.async_get(hass)
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=integration.entry_id,
identifiers={get_device_id(driver, node)},
manufacturer=hank_binary_switch_state["deviceConfig"]["manufacturer"],
@ -311,7 +339,7 @@ async def test_skip_old_entity_migration_for_multiple(
# Create two entity entrrys using different endpoints
old_unique_id_1 = f"{driver.controller.home_id}.32-50-1-value-66049"
entity_entry = ent_reg.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id_1,
@ -325,7 +353,7 @@ async def test_skip_old_entity_migration_for_multiple(
# Create two entity entrrys using different endpoints
old_unique_id_2 = f"{driver.controller.home_id}.32-50-2-value-66049"
entity_entry = ent_reg.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id_2,
@ -342,26 +370,29 @@ async def test_skip_old_entity_migration_for_multiple(
await hass.async_block_till_done()
# Check that new RegistryEntry is created using new unique ID format
entity_entry = ent_reg.async_get(SENSOR_NAME)
entity_entry = entity_registry.async_get(SENSOR_NAME)
new_unique_id = f"{driver.controller.home_id}.32-50-0-value-66049"
assert entity_entry.unique_id == new_unique_id
# Check that the old entities stuck around because we skipped the migration step
assert ent_reg.async_get_entity_id("sensor", DOMAIN, old_unique_id_1)
assert ent_reg.async_get_entity_id("sensor", DOMAIN, old_unique_id_2)
assert entity_registry.async_get_entity_id("sensor", DOMAIN, old_unique_id_1)
assert entity_registry.async_get_entity_id("sensor", DOMAIN, old_unique_id_2)
async def test_old_entity_migration_notification_binary_sensor(
hass: HomeAssistant, multisensor_6_state, client, integration
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
multisensor_6_state,
client,
integration,
) -> None:
"""Test old entity on a different endpoint is migrated to a new one for a notification binary sensor."""
node = Node(client, copy.deepcopy(multisensor_6_state))
driver = client.driver
assert driver
ent_reg = er.async_get(hass)
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=integration.entry_id,
identifiers={get_device_id(driver, node)},
manufacturer=multisensor_6_state["deviceConfig"]["manufacturer"],
@ -374,7 +405,7 @@ async def test_old_entity_migration_notification_binary_sensor(
old_unique_id = (
f"{driver.controller.home_id}.52-113-1-Home Security-Motion sensor status.8"
)
entity_entry = ent_reg.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
"binary_sensor",
DOMAIN,
old_unique_id,
@ -394,11 +425,12 @@ async def test_old_entity_migration_notification_binary_sensor(
await hass.async_block_till_done()
# Check that new RegistryEntry is using new unique ID format
entity_entry = ent_reg.async_get(NOTIFICATION_MOTION_BINARY_SENSOR)
entity_entry = entity_registry.async_get(NOTIFICATION_MOTION_BINARY_SENSOR)
new_unique_id = (
f"{driver.controller.home_id}.52-113-0-Home Security-Motion sensor status.8"
)
assert entity_entry.unique_id == new_unique_id
assert (
ent_reg.async_get_entity_id("binary_sensor", DOMAIN, old_unique_id) is None
entity_registry.async_get_entity_id("binary_sensor", DOMAIN, old_unique_id)
is None
)

View file

@ -219,20 +219,22 @@ async def test_volume_number(
async def test_config_parameter_number(
hass: HomeAssistant, climate_adc_t3000, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
climate_adc_t3000,
integration,
) -> None:
"""Test config parameter number is created."""
number_entity_id = "number.adc_t3000_heat_staging_delay"
number_with_states_entity_id = "number.adc_t3000_calibration_temperature"
ent_reg = er.async_get(hass)
for entity_id in (number_entity_id, number_with_states_entity_id):
entity_entry = ent_reg.async_get(entity_id)
entity_entry = entity_registry.async_get(entity_id)
assert entity_entry
assert entity_entry.disabled
assert entity_entry.entity_category == EntityCategory.CONFIG
for entity_id in (number_entity_id, number_with_states_entity_id):
updated_entry = ent_reg.async_update_entity(entity_id, disabled_by=None)
updated_entry = entity_registry.async_update_entity(entity_id, disabled_by=None)
assert updated_entry != entity_entry
assert updated_entry.disabled is False

View file

@ -55,17 +55,19 @@ async def test_device_config_file_changed_confirm_step(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
client,
multisensor_6_state,
integration,
) -> None:
"""Test the device_config_file_changed issue confirm step."""
dev_reg = dr.async_get(hass)
node = await _trigger_repair_issue(hass, client, multisensor_6_state)
client.async_send_command_no_wait.reset_mock()
device = dev_reg.async_get_device(identifiers={get_device_id(client.driver, node)})
device = device_registry.async_get_device(
identifiers={get_device_id(client.driver, node)}
)
assert device
issue_id = f"device_config_file_changed.{device.id}"
@ -128,17 +130,19 @@ async def test_device_config_file_changed_ignore_step(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
client,
multisensor_6_state,
integration,
) -> None:
"""Test the device_config_file_changed issue ignore step."""
dev_reg = dr.async_get(hass)
node = await _trigger_repair_issue(hass, client, multisensor_6_state)
client.async_send_command_no_wait.reset_mock()
device = dev_reg.async_get_device(identifiers={get_device_id(client.driver, node)})
device = device_registry.async_get_device(
identifiers={get_device_id(client.driver, node)}
)
assert device
issue_id = f"device_config_file_changed.{device.id}"
@ -256,15 +260,17 @@ async def test_abort_confirm(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
client,
multisensor_6_state,
integration,
) -> None:
"""Test aborting device_config_file_changed issue in confirm step."""
dev_reg = dr.async_get(hass)
node = await _trigger_repair_issue(hass, client, multisensor_6_state)
device = dev_reg.async_get_device(identifiers={get_device_id(client.driver, node)})
device = device_registry.async_get_device(
identifiers={get_device_id(client.driver, node)}
)
assert device
issue_id = f"device_config_file_changed.{device.id}"

View file

@ -21,6 +21,7 @@ MULTILEVEL_SWITCH_SELECT_ENTITY = "select.front_door_siren"
async def test_default_tone_select(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client: MagicMock,
aeotec_zw164_siren: Node,
integration: ConfigEntry,
@ -64,7 +65,6 @@ async def test_default_tone_select(
"30DOOR~1 (27 sec)",
]
entity_registry = er.async_get(hass)
entity_entry = entity_registry.async_get(DEFAULT_TONE_SELECT_ENTITY)
assert entity_entry
@ -118,6 +118,7 @@ async def test_default_tone_select(
async def test_protection_select(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client: MagicMock,
inovelli_lzw36: Node,
integration: ConfigEntry,
@ -135,7 +136,6 @@ async def test_protection_select(
"NoOperationPossible",
]
entity_registry = er.async_get(hass)
entity_entry = entity_registry.async_get(PROTECTION_SELECT_ENTITY)
assert entity_entry
@ -298,17 +298,21 @@ async def test_multilevel_switch_select_no_value(
async def test_config_parameter_select(
hass: HomeAssistant, climate_adc_t3000, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
climate_adc_t3000,
integration,
) -> None:
"""Test config parameter select is created."""
select_entity_id = "select.adc_t3000_hvac_system_type"
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(select_entity_id)
entity_entry = entity_registry.async_get(select_entity_id)
assert entity_entry
assert entity_entry.disabled
assert entity_entry.entity_category == EntityCategory.CONFIG
updated_entry = ent_reg.async_update_entity(select_entity_id, disabled_by=None)
updated_entry = entity_registry.async_update_entity(
select_entity_id, disabled_by=None
)
assert updated_entry != entity_entry
assert updated_entry.disabled is False

View file

@ -57,7 +57,11 @@ from .common import (
async def test_numeric_sensor(
hass: HomeAssistant, multisensor_6, express_controls_ezmultipli, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
multisensor_6,
express_controls_ezmultipli,
integration,
) -> None:
"""Test the numeric sensor."""
state = hass.states.get(AIR_TEMPERATURE_SENSOR)
@ -76,8 +80,7 @@ async def test_numeric_sensor(
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.BATTERY
assert state.attributes[ATTR_STATE_CLASS] == SensorStateClass.MEASUREMENT
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(BATTERY_SENSOR)
entity_entry = entity_registry.async_get(BATTERY_SENSOR)
assert entity_entry
assert entity_entry.entity_category is EntityCategory.DIAGNOSTIC
@ -210,18 +213,17 @@ async def test_energy_sensors(
async def test_disabled_notification_sensor(
hass: HomeAssistant, multisensor_6, integration
hass: HomeAssistant, entity_registry: er.EntityRegistry, multisensor_6, integration
) -> None:
"""Test sensor is created from Notification CC and is disabled."""
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(NOTIFICATION_MOTION_SENSOR)
entity_entry = entity_registry.async_get(NOTIFICATION_MOTION_SENSOR)
assert entity_entry
assert entity_entry.disabled
assert entity_entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
# Test enabling entity
updated_entry = ent_reg.async_update_entity(
updated_entry = entity_registry.async_update_entity(
entity_entry.entity_id, disabled_by=None
)
assert updated_entry != entity_entry
@ -265,20 +267,23 @@ async def test_disabled_notification_sensor(
async def test_config_parameter_sensor(
hass: HomeAssistant, climate_adc_t3000, lock_id_lock_as_id150, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
climate_adc_t3000,
lock_id_lock_as_id150,
integration,
) -> None:
"""Test config parameter sensor is created."""
sensor_entity_id = "sensor.adc_t3000_system_configuration_cool_stages"
sensor_with_states_entity_id = "sensor.adc_t3000_power_source"
ent_reg = er.async_get(hass)
for entity_id in (sensor_entity_id, sensor_with_states_entity_id):
entity_entry = ent_reg.async_get(entity_id)
entity_entry = entity_registry.async_get(entity_id)
assert entity_entry
assert entity_entry.disabled
assert entity_entry.entity_category == EntityCategory.DIAGNOSTIC
for entity_id in (sensor_entity_id, sensor_with_states_entity_id):
updated_entry = ent_reg.async_update_entity(entity_id, disabled_by=None)
updated_entry = entity_registry.async_update_entity(entity_id, disabled_by=None)
assert updated_entry != entity_entry
assert updated_entry.disabled is False
@ -294,7 +299,7 @@ async def test_config_parameter_sensor(
assert state
assert state.state == "C-Wire"
updated_entry = ent_reg.async_update_entity(
updated_entry = entity_registry.async_update_entity(
entity_entry.entity_id, disabled_by=None
)
assert updated_entry != entity_entry
@ -306,12 +311,11 @@ async def test_config_parameter_sensor(
async def test_controller_status_sensor(
hass: HomeAssistant, client, integration
hass: HomeAssistant, entity_registry: er.EntityRegistry, client, integration
) -> None:
"""Test controller status sensor is created and gets updated on controller state changes."""
entity_id = "sensor.z_stick_gen5_usb_controller_status"
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(entity_id)
entity_entry = entity_registry.async_get(entity_id)
assert not entity_entry.disabled
assert entity_entry.entity_category is EntityCategory.DIAGNOSTIC
@ -344,13 +348,16 @@ async def test_controller_status_sensor(
async def test_node_status_sensor(
hass: HomeAssistant, client, lock_id_lock_as_id150, integration
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client,
lock_id_lock_as_id150,
integration,
) -> None:
"""Test node status sensor is created and gets updated on node state changes."""
node_status_entity_id = "sensor.z_wave_module_for_id_lock_150_and_101_node_status"
node = lock_id_lock_as_id150
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(node_status_entity_id)
entity_entry = entity_registry.async_get(node_status_entity_id)
assert not entity_entry.disabled
assert entity_entry.entity_category is EntityCategory.DIAGNOSTIC
@ -390,7 +397,7 @@ async def test_node_status_sensor(
node = driver.controller.nodes[1]
assert node.is_controller_node
assert (
ent_reg.async_get_entity_id(
entity_registry.async_get_entity_id(
DOMAIN,
"sensor",
f"{get_valueless_base_unique_id(driver, node)}.node_status",
@ -400,7 +407,7 @@ async def test_node_status_sensor(
# Assert a controller status sensor entity is not created for a node
assert (
ent_reg.async_get_entity_id(
entity_registry.async_get_entity_id(
DOMAIN,
"sensor",
f"{get_valueless_base_unique_id(driver, node)}.controller_status",
@ -411,6 +418,7 @@ async def test_node_status_sensor(
async def test_node_status_sensor_not_ready(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client,
lock_id_lock_as_id150_not_ready,
lock_id_lock_as_id150_state,
@ -421,8 +429,7 @@ async def test_node_status_sensor_not_ready(
node_status_entity_id = "sensor.z_wave_module_for_id_lock_150_and_101_node_status"
node = lock_id_lock_as_id150_not_ready
assert not node.ready
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(node_status_entity_id)
entity_entry = entity_registry.async_get(node_status_entity_id)
assert not entity_entry.disabled
assert hass.states.get(node_status_entity_id)
@ -736,10 +743,14 @@ NODE_STATISTICS_SUFFIXES_UNKNOWN = {
async def test_statistics_sensors_no_last_seen(
hass: HomeAssistant, zp3111, client, integration, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
zp3111,
client,
integration,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test all statistics sensors but last seen which is enabled by default."""
ent_reg = er.async_get(hass)
for prefix, suffixes in (
(CONTROLLER_STATISTICS_ENTITY_PREFIX, CONTROLLER_STATISTICS_SUFFIXES),
@ -748,12 +759,12 @@ async def test_statistics_sensors_no_last_seen(
(NODE_STATISTICS_ENTITY_PREFIX, NODE_STATISTICS_SUFFIXES_UNKNOWN),
):
for suffix_key in suffixes:
entry = ent_reg.async_get(f"{prefix}{suffix_key}")
entry = entity_registry.async_get(f"{prefix}{suffix_key}")
assert entry
assert entry.disabled
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
ent_reg.async_update_entity(entry.entity_id, disabled_by=None)
entity_registry.async_update_entity(entry.entity_id, disabled_by=None)
# reload integration and check if entity is correctly there
await hass.config_entries.async_reload(integration.entry_id)
@ -774,7 +785,7 @@ async def test_statistics_sensors_no_last_seen(
),
):
for suffix_key in suffixes:
entry = ent_reg.async_get(f"{prefix}{suffix_key}")
entry = entity_registry.async_get(f"{prefix}{suffix_key}")
assert entry
assert not entry.disabled
assert entry.disabled_by is None
@ -881,13 +892,11 @@ async def test_statistics_sensors_no_last_seen(
async def test_last_seen_statistics_sensors(
hass: HomeAssistant, zp3111, client, integration
hass: HomeAssistant, entity_registry: er.EntityRegistry, zp3111, client, integration
) -> None:
"""Test last_seen statistics sensors."""
ent_reg = er.async_get(hass)
entity_id = f"{NODE_STATISTICS_ENTITY_PREFIX}last_seen"
entry = ent_reg.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
assert not entry.disabled

View file

@ -219,16 +219,21 @@ async def test_switch_no_value(
async def test_config_parameter_switch(
hass: HomeAssistant, hank_binary_switch, integration, client
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hank_binary_switch,
integration,
client,
) -> None:
"""Test config parameter switch is created."""
switch_entity_id = "switch.smart_plug_with_two_usb_ports_overload_protection"
ent_reg = er.async_get(hass)
entity_entry = ent_reg.async_get(switch_entity_id)
entity_entry = entity_registry.async_get(switch_entity_id)
assert entity_entry
assert entity_entry.disabled
updated_entry = ent_reg.async_update_entity(switch_entity_id, disabled_by=None)
updated_entry = entity_registry.async_update_entity(
switch_entity_id, disabled_by=None
)
assert updated_entry != entity_entry
assert updated_entry.disabled is False
assert entity_entry.entity_category == EntityCategory.CONFIG