Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update k-l* tests to use entity & device registry fixtures #103929

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/components/kaleidescape/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ async def test_config_entry_not_ready(

async def test_device(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
mock_device: AsyncMock,
mock_integration: MockConfigEntry,
) -> None:
"""Test device."""
device_registry = dr.async_get(hass)
device = device_registry.async_get_device(
identifiers={("kaleidescape", MOCK_SERIAL)}
)
Expand Down
2 changes: 1 addition & 1 deletion tests/components/kaleidescape/test_media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ async def test_services(

async def test_device(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
mock_device: MagicMock,
mock_integration: MockConfigEntry,
) -> None:
"""Test device attributes."""
device_registry = dr.async_get(hass)
device = device_registry.async_get_device(
identifiers={("kaleidescape", MOCK_SERIAL)}
)
Expand Down
5 changes: 3 additions & 2 deletions tests/components/kaleidescape/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

async def test_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_device: MagicMock,
mock_integration: MockConfigEntry,
) -> None:
"""Test sensors."""
entity = hass.states.get(f"{ENTITY_ID}_media_location")
entry = er.async_get(hass).async_get(f"{ENTITY_ID}_media_location")
entry = entity_registry.async_get(f"{ENTITY_ID}_media_location")
assert entity
assert entity.state == "none"
assert (
Expand All @@ -33,7 +34,7 @@ async def test_sensors(
assert entry.unique_id == f"{MOCK_SERIAL}-media_location"

entity = hass.states.get(f"{ENTITY_ID}_play_status")
entry = er.async_get(hass).async_get(f"{ENTITY_ID}_play_status")
entry = entity_registry.async_get(f"{ENTITY_ID}_play_status")
assert entity
assert entity.state == "none"
assert entity.attributes.get(ATTR_FRIENDLY_NAME) == f"{FRIENDLY_NAME} Play status"
Expand Down
5 changes: 2 additions & 3 deletions tests/components/knx/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


async def test_binary_sensor_entity_category(
hass: HomeAssistant, knx: KNXTestKit
hass: HomeAssistant, entity_registry: er.EntityRegistry, knx: KNXTestKit
) -> None:
"""Test KNX binary sensor entity category."""
await knx.setup_integration(
Expand All @@ -42,8 +42,7 @@ async def test_binary_sensor_entity_category(
await knx.assert_read("1/1/1")
await knx.receive_response("1/1/1", True)

registry = er.async_get(hass)
entity = registry.async_get("binary_sensor.test_normal")
entity = entity_registry.async_get("binary_sensor.test_normal")
assert entity.entity_category is EntityCategory.DIAGNOSTIC


Expand Down
5 changes: 3 additions & 2 deletions tests/components/kraken/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ async def test_sensor(


async def test_sensors_available_after_restart(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test that all sensors are added again after a restart."""
with patch(
Expand All @@ -153,7 +155,6 @@ async def test_sensors_available_after_restart(
)
entry.add_to_hass(hass)

device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, "XBT_USD")},
Expand Down
3 changes: 1 addition & 2 deletions tests/components/lametric/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@

async def test_get_coordinator_by_device_id(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
init_integration: MockConfigEntry,
mock_lametric: MagicMock,
) -> None:
"""Test get LaMetric coordinator by device ID ."""
entity_registry = er.async_get(hass)

with pytest.raises(ValueError, match="Unknown LaMetric device ID: bla"):
async_get_coordinator_by_device_id(hass, "bla")

Expand Down
4 changes: 2 additions & 2 deletions tests/components/lametric/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@

async def test_service_chart(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_lametric: MagicMock,
) -> None:
"""Test the LaMetric chart service."""
entity_registry = er.async_get(hass)

entry = entity_registry.async_get("button.frenck_s_lametric_next_app")
assert entry
Expand Down Expand Up @@ -121,10 +121,10 @@ async def test_service_chart(

async def test_service_message(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_lametric: MagicMock,
) -> None:
"""Test the LaMetric message service."""
entity_registry = er.async_get(hass)

entry = entity_registry.async_get("button.frenck_s_lametric_next_app")
assert entry
Expand Down
9 changes: 5 additions & 4 deletions tests/components/landisgyr_heat_meter/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ async def test_unload_entry(_, hass: HomeAssistant) -> None:


@patch(API_HEAT_METER_SERVICE)
async def test_migrate_entry(_, hass: HomeAssistant) -> None:
async def test_migrate_entry(
_, hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test successful migration of entry data from version 1 to 2."""

mock_entry_data = {
Expand All @@ -59,8 +61,7 @@ async def test_migrate_entry(_, hass: HomeAssistant) -> None:
mock_entry.add_to_hass(hass)

# Create entity entry to migrate to new unique ID
registry = er.async_get(hass)
registry.async_get_or_create(
entity_registry.async_get_or_create(
SENSOR_DOMAIN,
LANDISGYR_HEAT_METER_DOMAIN,
"landisgyr_heat_meter_987654321_measuring_range_m3ph",
Expand All @@ -74,5 +75,5 @@ async def test_migrate_entry(_, hass: HomeAssistant) -> None:

# Check if entity unique id is migrated successfully
assert mock_entry.version == 2
entity = registry.async_get("sensor.heat_meter_measuring_range")
entity = entity_registry.async_get("sensor.heat_meter_measuring_range")
assert entity.unique_id == "12345_measuring_range_m3ph"
5 changes: 3 additions & 2 deletions tests/components/lcn/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ async def test_entity_state(hass: HomeAssistant, lcn_connection) -> None:
assert state


async def test_entity_attributes(hass: HomeAssistant, entry, lcn_connection) -> None:
async def test_entity_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, lcn_connection
) -> None:
"""Test the attributes of an entity."""
entity_registry = er.async_get(hass)

entity_setpoint1 = entity_registry.async_get(BINARY_SENSOR_LOCKREGULATOR1)
assert entity_setpoint1
Expand Down
5 changes: 3 additions & 2 deletions tests/components/lcn/test_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ async def test_setup_lcn_cover(hass: HomeAssistant, entry, lcn_connection) -> No
assert state.state == STATE_OPEN


async def test_entity_attributes(hass: HomeAssistant, entry, lcn_connection) -> None:
async def test_entity_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, lcn_connection
) -> None:
"""Test the attributes of an entity."""
entity_registry = er.async_get(hass)

entity_outputs = entity_registry.async_get(COVER_OUTPUTS)

Expand Down
3 changes: 1 addition & 2 deletions tests/components/lcn/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ async def test_get_triggers_module_device(


async def test_get_triggers_non_module_device(
hass: HomeAssistant, entry, lcn_connection
hass: HomeAssistant, device_registry: dr.DeviceRegistry, entry, lcn_connection
) -> None:
"""Test we get the expected triggers from a LCN non-module device."""
not_included_types = ("transmitter", "transponder", "fingerprint", "send_keys")

device_registry = dr.async_get(hass)
host_device = device_registry.async_get_device(
identifiers={(DOMAIN, entry.entry_id)}
)
Expand Down
9 changes: 6 additions & 3 deletions tests/components/lcn/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,23 @@ async def test_async_setup_multiple_entries(hass: HomeAssistant, entry, entry2)
assert not hass.data.get(DOMAIN)


async def test_async_setup_entry_update(hass: HomeAssistant, entry) -> None:
async def test_async_setup_entry_update(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
entry,
) -> None:
"""Test a successful setup entry if entry with same id already exists."""
# setup first entry
entry.source = config_entries.SOURCE_IMPORT
entry.add_to_hass(hass)

# create dummy entity for LCN platform as an orphan
entity_registry = er.async_get(hass)
dummy_entity = entity_registry.async_get_or_create(
"switch", DOMAIN, "dummy", config_entry=entry
)

# create dummy device for LCN platform as an orphan
device_registry = dr.async_get(hass)
dummy_device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, entry.entry_id, 0, 7, False)},
Expand Down
6 changes: 3 additions & 3 deletions tests/components/lcn/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ async def test_entity_state(hass: HomeAssistant, lcn_connection) -> None:
assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.ONOFF]


async def test_entity_attributes(hass: HomeAssistant, entry, lcn_connection) -> None:
async def test_entity_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, lcn_connection
) -> None:
"""Test the attributes of an entity."""
entity_registry = er.async_get(hass)

entity_output = entity_registry.async_get(LIGHT_OUTPUT1)

assert entity_output
Expand Down
5 changes: 3 additions & 2 deletions tests/components/lcn/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ async def test_entity_state(hass: HomeAssistant, lcn_connection) -> None:
assert state


async def test_entity_attributes(hass: HomeAssistant, entry, lcn_connection) -> None:
async def test_entity_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, lcn_connection
) -> None:
"""Test the attributes of an entity."""
entity_registry = er.async_get(hass)

entity_var1 = entity_registry.async_get(SENSOR_VAR1)
assert entity_var1
Expand Down
5 changes: 3 additions & 2 deletions tests/components/lcn/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ async def test_setup_lcn_switch(hass: HomeAssistant, lcn_connection) -> None:
assert state.state == STATE_OFF


async def test_entity_attributes(hass: HomeAssistant, entry, lcn_connection) -> None:
async def test_entity_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, lcn_connection
) -> None:
"""Test the attributes of an entity."""
entity_registry = er.async_get(hass)

entity_output = entity_registry.async_get(SWITCH_OUTPUT1)

Expand Down
6 changes: 4 additions & 2 deletions tests/components/lidarr/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ async def test_async_setup_entry_auth_failed(


async def test_device_info(
hass: HomeAssistant, setup_integration: ComponentSetup, connection
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
setup_integration: ComponentSetup,
connection,
) -> None:
"""Test device info."""
await setup_integration()
entry = hass.config_entries.async_entries(DOMAIN)[0]
device_registry = dr.async_get(hass)
await hass.async_block_till_done()
device = device_registry.async_get_device(identifiers={(DOMAIN, entry.entry_id)})

Expand Down
5 changes: 3 additions & 2 deletions tests/components/lifx/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
from tests.common import MockConfigEntry, async_fire_time_changed


async def test_hev_cycle_state(hass: HomeAssistant) -> None:
async def test_hev_cycle_state(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test HEV cycle state binary sensor."""
config_entry = MockConfigEntry(
domain=lifx.DOMAIN,
Expand All @@ -48,7 +50,6 @@ async def test_hev_cycle_state(hass: HomeAssistant) -> None:
await hass.async_block_till_done()

entity_id = "binary_sensor.my_bulb_clean_cycle"
entity_registry = er.async_get(hass)

state = hass.states.get(entity_id)
assert state
Expand Down
10 changes: 6 additions & 4 deletions tests/components/lifx/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def mock_lifx_coordinator_sleep():
yield


async def test_button_restart(hass: HomeAssistant) -> None:
async def test_button_restart(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that a bulb can be restarted."""
config_entry = MockConfigEntry(
domain=DOMAIN,
Expand All @@ -50,7 +52,6 @@ async def test_button_restart(hass: HomeAssistant) -> None:
unique_id = f"{SERIAL}_restart"
entity_id = "button.my_bulb_restart"

entity_registry = er.async_get(hass)
entity = entity_registry.async_get(entity_id)
assert entity
assert not entity.disabled
Expand All @@ -63,7 +64,9 @@ async def test_button_restart(hass: HomeAssistant) -> None:
bulb.set_reboot.assert_called_once()


async def test_button_identify(hass: HomeAssistant) -> None:
async def test_button_identify(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that a bulb can be identified."""
config_entry = MockConfigEntry(
domain=DOMAIN,
Expand All @@ -82,7 +85,6 @@ async def test_button_identify(hass: HomeAssistant) -> None:
unique_id = f"{SERIAL}_identify"
entity_id = "button.my_bulb_identify"

entity_registry = er.async_get(hass)
entity = entity_registry.async_get(entity_id)
assert entity
assert not entity.disabled
Expand Down
8 changes: 5 additions & 3 deletions tests/components/lifx/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,11 @@ async def test_refuse_relays(hass: HomeAssistant) -> None:
assert result2["errors"] == {"base": "cannot_connect"}


async def test_suggested_area(hass: HomeAssistant) -> None:
async def test_suggested_area(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test suggested area is populated from lifx group label."""

class MockLifxCommandGetGroup:
Expand Down Expand Up @@ -567,10 +571,8 @@ def __call__(self, callb=None, *args, **kwargs):
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()

entity_registry = er.async_get(hass)
entity_id = "light.my_bulb"
entity = entity_registry.async_get(entity_id)

device_registry = dr.async_get(hass)
device = device_registry.async_get(entity.device_id)
assert device.suggested_area == "My LIFX Group"