Update h* tests to use entity & device registry fixtures (#103866)

* Update h* tests to use entity & device registry fixtures

* Add missed lines
This commit is contained in:
Jan-Philipp Benecke 2023-11-12 19:52:32 +01:00 committed by GitHub
parent 3a531f5698
commit eda475fe25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 330 additions and 240 deletions

View file

@ -142,7 +142,9 @@ async def test_ecobee3_setup(hass: HomeAssistant) -> None:
async def test_ecobee3_setup_from_cache(
hass: HomeAssistant, hass_storage: dict[str, Any]
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_storage: dict[str, Any],
) -> None:
"""Test that Ecbobee can be correctly setup from its cached entity map."""
accessories = await setup_accessories_from_file(hass, "ecobee3.json")
@ -163,8 +165,6 @@ async def test_ecobee3_setup_from_cache(
await setup_test_accessories(hass, accessories)
entity_registry = er.async_get(hass)
climate = entity_registry.async_get("climate.homew")
assert climate.unique_id == "00:00:00:00:00:00_1_16"
@ -178,12 +178,12 @@ async def test_ecobee3_setup_from_cache(
assert occ3.unique_id == "00:00:00:00:00:00_4_56"
async def test_ecobee3_setup_connection_failure(hass: HomeAssistant) -> None:
async def test_ecobee3_setup_connection_failure(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that Ecbobee can be correctly setup from its cached entity map."""
accessories = await setup_accessories_from_file(hass, "ecobee3.json")
entity_registry = er.async_get(hass)
# Test that the connection fails during initial setup.
# No entities should be created.
with mock.patch.object(FakePairing, "async_populate_accessories_state") as laac:
@ -218,9 +218,10 @@ async def test_ecobee3_setup_connection_failure(hass: HomeAssistant) -> None:
assert occ3.unique_id == "00:00:00:00:00:00_4_56"
async def test_ecobee3_add_sensors_at_runtime(hass: HomeAssistant) -> None:
async def test_ecobee3_add_sensors_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that new sensors are automatically added."""
entity_registry = er.async_get(hass)
# Set up a base Ecobee 3 with no additional sensors.
# There shouldn't be any entities but climate visible.
@ -254,9 +255,10 @@ async def test_ecobee3_add_sensors_at_runtime(hass: HomeAssistant) -> None:
assert occ3.unique_id == "00:00:00:00:00:00_4_56"
async def test_ecobee3_remove_sensors_at_runtime(hass: HomeAssistant) -> None:
async def test_ecobee3_remove_sensors_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that sensors are automatically removed."""
entity_registry = er.async_get(hass)
# Set up a base Ecobee 3 with additional sensors.
accessories = await setup_accessories_from_file(hass, "ecobee3.json")
@ -307,10 +309,9 @@ async def test_ecobee3_remove_sensors_at_runtime(hass: HomeAssistant) -> None:
async def test_ecobee3_services_and_chars_removed(
hass: HomeAssistant,
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test handling removal of some services and chars."""
entity_registry = er.async_get(hass)
# Set up a base Ecobee 3 with additional sensors.
accessories = await setup_accessories_from_file(hass, "ecobee3.json")

View file

@ -13,9 +13,10 @@ from ..common import (
)
async def test_fan_add_feature_at_runtime(hass: HomeAssistant) -> None:
async def test_fan_add_feature_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that new features can be added at runtime."""
entity_registry = er.async_get(hass)
# Set up a basic fan that does not support oscillation
accessories = await setup_accessories_from_file(
@ -55,9 +56,10 @@ async def test_fan_add_feature_at_runtime(hass: HomeAssistant) -> None:
assert fan_state.attributes[ATTR_SUPPORTED_FEATURES] is FanEntityFeature.SET_SPEED
async def test_fan_remove_feature_at_runtime(hass: HomeAssistant) -> None:
async def test_fan_remove_feature_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that features can be removed at runtime."""
entity_registry = er.async_get(hass)
# Set up a basic fan that does not support oscillation
accessories = await setup_accessories_from_file(
@ -97,9 +99,11 @@ async def test_fan_remove_feature_at_runtime(hass: HomeAssistant) -> None:
assert fan_state.attributes[ATTR_SUPPORTED_FEATURES] is FanEntityFeature.SET_SPEED
async def test_bridge_with_two_fans_one_removed(hass: HomeAssistant) -> None:
async def test_bridge_with_two_fans_one_removed(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None:
"""Test a bridge with two fans and one gets removed."""
entity_registry = er.async_get(hass)
# Set up a basic fan that does not support oscillation
accessories = await setup_accessories_from_file(

View file

@ -14,10 +14,12 @@ from ..common import (
)
async def test_vocolinc_vp3_setup(hass: HomeAssistant) -> None:
async def test_vocolinc_vp3_setup(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None:
"""Test that a VOCOlinc VP3 can be correctly setup in HA."""
entity_registry = er.async_get(hass)
outlet = entity_registry.async_get_or_create(
"switch",
"homekit_controller",

View file

@ -124,9 +124,10 @@ async def test_switch_read_alarm_state(hass: HomeAssistant, utcnow) -> None:
assert state.state == "triggered"
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a alarm_control_panel unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
alarm_control_panel_entry = entity_registry.async_get_or_create(
"alarm_control_panel",

View file

@ -173,9 +173,10 @@ async def test_leak_sensor_read_state(hass: HomeAssistant, utcnow) -> None:
assert state.attributes["device_class"] == BinarySensorDeviceClass.MOISTURE
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a binary_sensor unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
binary_sensor_entry = entity_registry.async_get_or_create(
"binary_sensor",

View file

@ -94,9 +94,10 @@ async def test_ecobee_clear_hold_press_button(hass: HomeAssistant) -> None:
)
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a button unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
button_entry = entity_registry.async_get_or_create(
"button",

View file

@ -16,9 +16,10 @@ def create_camera(accessory):
accessory.add_service(ServicesTypes.CAMERA_RTP_STREAM_MANAGEMENT)
async def test_migrate_unique_ids(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_ids(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test migrating entity unique ids."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
camera = entity_registry.async_get_or_create(
"camera",

View file

@ -1112,9 +1112,10 @@ async def test_heater_cooler_turn_off(hass: HomeAssistant, utcnow) -> None:
assert state.attributes["hvac_action"] == "off"
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a switch unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
climate_entry = entity_registry.async_get_or_create(
"climate",

View file

@ -90,7 +90,9 @@ DEVICE_MIGRATION_TESTS = [
@pytest.mark.parametrize("variant", DEVICE_MIGRATION_TESTS)
async def test_migrate_device_id_no_serial_skip_if_other_owner(
hass: HomeAssistant, variant: DeviceMigrationTest
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
variant: DeviceMigrationTest,
) -> None:
"""Don't migrate unrelated devices.
@ -99,7 +101,6 @@ async def test_migrate_device_id_no_serial_skip_if_other_owner(
"""
entry = MockConfigEntry()
entry.add_to_hass(hass)
device_registry = dr.async_get(hass)
bridge = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
@ -122,11 +123,11 @@ async def test_migrate_device_id_no_serial_skip_if_other_owner(
@pytest.mark.parametrize("variant", DEVICE_MIGRATION_TESTS)
async def test_migrate_device_id_no_serial(
hass: HomeAssistant, variant: DeviceMigrationTest
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
variant: DeviceMigrationTest,
) -> None:
"""Test that a Ryse smart bridge with four shades can be migrated correctly in HA."""
device_registry = dr.async_get(hass)
accessories = await setup_accessories_from_file(hass, variant.fixture)
fake_controller = await setup_platform(hass)

View file

@ -398,9 +398,10 @@ async def test_read_door_state(hass: HomeAssistant, utcnow) -> None:
assert state.attributes["obstruction-detected"] is True
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a cover unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
cover_entry = entity_registry.async_get_or_create(
"cover",

View file

@ -83,15 +83,18 @@ def create_doorbell(accessory):
battery.add_char(CharacteristicsTypes.BATTERY_LEVEL)
async def test_enumerate_remote(hass: HomeAssistant, utcnow) -> None:
async def test_enumerate_remote(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
utcnow,
) -> None:
"""Test that remote is correctly enumerated."""
await setup_test_component(hass, create_remote)
entity_registry = er.async_get(hass)
bat_sensor = entity_registry.async_get("sensor.testdevice_battery")
identify_button = entity_registry.async_get("button.testdevice_identify")
device_registry = dr.async_get(hass)
device = device_registry.async_get(bat_sensor.device_id)
expected = [
@ -132,15 +135,18 @@ async def test_enumerate_remote(hass: HomeAssistant, utcnow) -> None:
assert triggers == unordered(expected)
async def test_enumerate_button(hass: HomeAssistant, utcnow) -> None:
async def test_enumerate_button(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
utcnow,
) -> None:
"""Test that a button is correctly enumerated."""
await setup_test_component(hass, create_button)
entity_registry = er.async_get(hass)
bat_sensor = entity_registry.async_get("sensor.testdevice_battery")
identify_button = entity_registry.async_get("button.testdevice_identify")
device_registry = dr.async_get(hass)
device = device_registry.async_get(bat_sensor.device_id)
expected = [
@ -180,15 +186,18 @@ async def test_enumerate_button(hass: HomeAssistant, utcnow) -> None:
assert triggers == unordered(expected)
async def test_enumerate_doorbell(hass: HomeAssistant, utcnow) -> None:
async def test_enumerate_doorbell(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
utcnow,
) -> None:
"""Test that a button is correctly enumerated."""
await setup_test_component(hass, create_doorbell)
entity_registry = er.async_get(hass)
bat_sensor = entity_registry.async_get("sensor.testdevice_battery")
identify_button = entity_registry.async_get("button.testdevice_identify")
device_registry = dr.async_get(hass)
device = device_registry.async_get(bat_sensor.device_id)
expected = [
@ -228,14 +237,18 @@ async def test_enumerate_doorbell(hass: HomeAssistant, utcnow) -> None:
assert triggers == unordered(expected)
async def test_handle_events(hass: HomeAssistant, utcnow, calls) -> None:
async def test_handle_events(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
utcnow,
calls,
) -> None:
"""Test that events are handled."""
helper = await setup_test_component(hass, create_remote)
entity_registry = er.async_get(hass)
entry = entity_registry.async_get("sensor.testdevice_battery")
device_registry = dr.async_get(hass)
device = device_registry.async_get(entry.device_id)
assert await async_setup_component(
@ -345,14 +358,18 @@ async def test_handle_events(hass: HomeAssistant, utcnow, calls) -> None:
assert len(calls) == 2
async def test_handle_events_late_setup(hass: HomeAssistant, utcnow, calls) -> None:
async def test_handle_events_late_setup(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
utcnow,
calls,
) -> None:
"""Test that events are handled when setup happens after startup."""
helper = await setup_test_component(hass, create_remote)
entity_registry = er.async_get(hass)
entry = entity_registry.async_get("sensor.testdevice_battery")
device_registry = dr.async_get(hass)
device = device_registry.async_get(entry.device_id)
await hass.config_entries.async_unload(helper.config_entry.entry_id)

View file

@ -290,14 +290,16 @@ async def test_config_entry(
async def test_device(
hass: HomeAssistant, hass_client: ClientSessionGenerator, utcnow
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
device_registry: dr.DeviceRegistry,
utcnow,
) -> None:
"""Test generating diagnostics for a device entry."""
accessories = await setup_accessories_from_file(hass, "koogeek_ls1.json")
config_entry, _ = await setup_test_accessories(hass, accessories)
connection = hass.data[KNOWN_DEVICES]["00:00:00:00:00:00"]
device_registry = dr.async_get(hass)
device = device_registry.async_get(connection.devices[1])
diag = await get_diagnostics_for_device(hass, hass_client, config_entry, device)

View file

@ -64,7 +64,9 @@ def create_doorbell(accessory):
battery.add_char(CharacteristicsTypes.BATTERY_LEVEL)
async def test_remote(hass: HomeAssistant, utcnow) -> None:
async def test_remote(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test that remote is supported."""
helper = await setup_test_component(hass, create_remote)
@ -75,8 +77,6 @@ async def test_remote(hass: HomeAssistant, utcnow) -> None:
("event.testdevice_button_4", "Button 4"),
]
entity_registry = er.async_get(hass)
for entity_id, service in entities:
button = entity_registry.async_get(entity_id)
@ -109,12 +109,13 @@ async def test_remote(hass: HomeAssistant, utcnow) -> None:
assert state.attributes["event_type"] == "long_press"
async def test_button(hass: HomeAssistant, utcnow) -> None:
async def test_button(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test that a button is correctly enumerated."""
helper = await setup_test_component(hass, create_button)
entity_id = "event.testdevice_button_1"
entity_registry = er.async_get(hass)
button = entity_registry.async_get(entity_id)
assert button.original_device_class == EventDeviceClass.BUTTON
@ -146,12 +147,13 @@ async def test_button(hass: HomeAssistant, utcnow) -> None:
assert state.attributes["event_type"] == "long_press"
async def test_doorbell(hass: HomeAssistant, utcnow) -> None:
async def test_doorbell(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test that doorbell service is handled."""
helper = await setup_test_component(hass, create_doorbell)
entity_id = "event.testdevice_doorbell"
entity_registry = er.async_get(hass)
doorbell = entity_registry.async_get(entity_id)
assert doorbell.original_device_class == EventDeviceClass.DOORBELL

View file

@ -811,9 +811,10 @@ async def test_v2_set_percentage_non_standard_rotation_range(
)
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a fan unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
fan_entry = entity_registry.async_get_or_create(
"fan",

View file

@ -455,11 +455,12 @@ async def test_dehumidifier_target_humidity_modes(hass: HomeAssistant, utcnow) -
assert state.attributes["current_humidity"] == 51
async def test_migrate_entity_ids(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_entity_ids(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test that we can migrate humidifier entity ids."""
aid = get_next_aid()
entity_registry = er.async_get(hass)
humidifier_entry = entity_registry.async_get_or_create(
"humidifier",
"homekit_controller",

View file

@ -17,7 +17,6 @@ from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, STATE_OFF, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_registry import EntityRegistry
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
@ -85,7 +84,10 @@ def create_alive_service(accessory):
async def test_device_remove_devices(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test we can only remove a device that no longer exists."""
assert await async_setup_component(hass, "config", {})
@ -93,9 +95,7 @@ async def test_device_remove_devices(
config_entry = helper.config_entry
entry_id = config_entry.entry_id
registry: EntityRegistry = er.async_get(hass)
entity = registry.entities[ALIVE_DEVICE_ENTITY_ID]
device_registry = dr.async_get(hass)
entity = entity_registry.entities[ALIVE_DEVICE_ENTITY_ID]
live_device_entry = device_registry.async_get(entity.device_id)
assert (
@ -231,15 +231,16 @@ async def test_ble_device_only_checks_is_available(
@pytest.mark.parametrize("example", FIXTURES, ids=lambda val: str(val.stem))
async def test_snapshots(
hass: HomeAssistant, snapshot: SnapshotAssertion, example: str
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
snapshot: SnapshotAssertion,
example: str,
) -> None:
"""Detect regressions in enumerating a homekit accessory database and building entities."""
accessories = await setup_accessories_from_file(hass, example)
config_entry, _ = await setup_test_accessories(hass, accessories)
device_registry = dr.async_get(hass)
entity_registry = er.async_get(hass)
registry_devices = dr.async_entries_for_config_entry(
device_registry, config_entry.entry_id
)

View file

@ -343,9 +343,10 @@ async def test_light_unloaded_removed(hass: HomeAssistant, utcnow) -> None:
assert hass.states.get(helper.entity_id).state == STATE_UNAVAILABLE
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a light unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
light_entry = entity_registry.async_get_or_create(
"light",
@ -360,9 +361,10 @@ async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
)
async def test_only_migrate_once(hass: HomeAssistant, utcnow) -> None:
async def test_only_migrate_once(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we handle migration happening after an upgrade and than a downgrade and then an upgrade."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
old_light_entry = entity_registry.async_get_or_create(
"light",

View file

@ -117,9 +117,10 @@ async def test_switch_read_lock_state(hass: HomeAssistant, utcnow) -> None:
assert state.state == "unlocking"
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a lock unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
lock_entry = entity_registry.async_get_or_create(
"lock",

View file

@ -368,9 +368,10 @@ async def test_tv_set_source_fail(hass: HomeAssistant, utcnow) -> None:
assert state.attributes["source"] == "HDMI 1"
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a media_player unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
media_player_entry = entity_registry.async_get_or_create(
"media_player",

View file

@ -29,9 +29,10 @@ def create_switch_with_spray_level(accessory):
return service
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a number unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
number = entity_registry.async_get_or_create(
"number",

View file

@ -33,9 +33,10 @@ def create_service_with_temperature_units(accessory: Accessory):
return service
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test we can migrate a select unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
select = entity_registry.async_get_or_create(
"select",

View file

@ -409,12 +409,12 @@ async def test_rssi_sensor(
async def test_migrate_rssi_sensor_unique_id(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
utcnow,
entity_registry_enabled_by_default: None,
enable_bluetooth: None,
) -> None:
"""Test an rssi sensor unique id migration."""
entity_registry = er.async_get(hass)
rssi_sensor = entity_registry.async_get_or_create(
"sensor",
"homekit_controller",

View file

@ -219,9 +219,10 @@ async def test_char_switch_read_state(hass: HomeAssistant, utcnow) -> None:
assert switch_1.state == "off"
async def test_migrate_unique_id(hass: HomeAssistant, utcnow) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, utcnow
) -> None:
"""Test a we can migrate a switch unique id."""
entity_registry = er.async_get(hass)
aid = get_next_aid()
switch_entry = entity_registry.async_get_or_create(
"switch",