Adjust entity registry access in tests (3) (#88964)

This commit is contained in:
epenet 2023-03-01 16:04:40 +01:00 committed by GitHub
parent 23cdafd12f
commit 9762b684c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 252 additions and 210 deletions

View file

@ -4,15 +4,13 @@ from unittest.mock import patch
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN, SERVICE_SET_VALUE
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as ent_reg
from homeassistant.helpers import entity_registry as er
async def test_number_entities(
hass: HomeAssistant, async_autosetup_sonos, soco
hass: HomeAssistant, async_autosetup_sonos, soco, entity_registry: er.EntityRegistry
) -> None:
"""Test number entities."""
entity_registry = ent_reg.async_get(hass)
bass_number = entity_registry.entities["number.zone_a_bass"]
bass_state = hass.states.get(bass_number.entity_id)
assert bass_state.state == "1"

View file

@ -10,7 +10,7 @@ from homeassistant.components.sonos.binary_sensor import ATTR_BATTERY_POWER_SOUR
from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as ent_reg
from homeassistant.helpers import entity_registry as er
from homeassistant.util import dt as dt_util
from .conftest import SonosMockEvent
@ -19,37 +19,31 @@ from tests.common import async_fire_time_changed
async def test_entity_registry_unsupported(
hass: HomeAssistant, async_setup_sonos, soco
hass: HomeAssistant, async_setup_sonos, soco, entity_registry: er.EntityRegistry
) -> None:
"""Test sonos device without battery registered in the device registry."""
soco.get_battery_info.side_effect = NotSupportedException
await async_setup_sonos()
entity_registry = ent_reg.async_get(hass)
assert "media_player.zone_a" in entity_registry.entities
assert "sensor.zone_a_battery" not in entity_registry.entities
assert "binary_sensor.zone_a_power" not in entity_registry.entities
async def test_entity_registry_supported(
hass: HomeAssistant, async_autosetup_sonos, soco
hass: HomeAssistant, async_autosetup_sonos, soco, entity_registry: er.EntityRegistry
) -> None:
"""Test sonos device with battery registered in the device registry."""
entity_registry = ent_reg.async_get(hass)
assert "media_player.zone_a" in entity_registry.entities
assert "sensor.zone_a_battery" in entity_registry.entities
assert "binary_sensor.zone_a_power" in entity_registry.entities
async def test_battery_attributes(
hass: HomeAssistant, async_autosetup_sonos, soco
hass: HomeAssistant, async_autosetup_sonos, soco, entity_registry: er.EntityRegistry
) -> None:
"""Test sonos device with battery state."""
entity_registry = ent_reg.async_get(hass)
battery = entity_registry.entities["sensor.zone_a_battery"]
battery_state = hass.states.get(battery.entity_id)
assert battery_state.state == "100"
@ -64,7 +58,11 @@ async def test_battery_attributes(
async def test_battery_on_s1(
hass: HomeAssistant, async_setup_sonos, soco, device_properties_event
hass: HomeAssistant,
async_setup_sonos,
soco,
device_properties_event,
entity_registry: er.EntityRegistry,
) -> None:
"""Test battery state updates on a Sonos S1 device."""
soco.get_battery_info.return_value = {}
@ -74,8 +72,6 @@ async def test_battery_on_s1(
subscription = soco.deviceProperties.subscribe.return_value
sub_callback = subscription.callback
entity_registry = ent_reg.async_get(hass)
assert "sensor.zone_a_battery" not in entity_registry.entities
assert "binary_sensor.zone_a_power" not in entity_registry.entities
@ -142,11 +138,14 @@ async def test_device_payload_without_battery_and_ignored_keys(
async def test_audio_input_sensor(
hass: HomeAssistant, async_autosetup_sonos, soco, tv_event, no_media_event
hass: HomeAssistant,
async_autosetup_sonos,
soco,
tv_event,
no_media_event,
entity_registry: er.EntityRegistry,
) -> None:
"""Test audio input sensor."""
entity_registry = ent_reg.async_get(hass)
subscription = soco.avTransport.subscribe.return_value
sub_callback = subscription.callback
sub_callback(tv_event)
@ -183,10 +182,13 @@ async def test_audio_input_sensor(
async def test_microphone_binary_sensor(
hass: HomeAssistant, async_autosetup_sonos, soco, device_properties_event
hass: HomeAssistant,
async_autosetup_sonos,
soco,
device_properties_event,
entity_registry: er.EntityRegistry,
) -> None:
"""Test microphone binary sensor."""
entity_registry = ent_reg.async_get(hass)
assert "binary_sensor.zone_a_microphone" in entity_registry.entities
mic_binary_sensor = entity_registry.entities["binary_sensor.zone_a_microphone"]
@ -203,10 +205,13 @@ async def test_microphone_binary_sensor(
async def test_favorites_sensor(
hass: HomeAssistant, async_autosetup_sonos, soco, fire_zgs_event
hass: HomeAssistant,
async_autosetup_sonos,
soco,
fire_zgs_event,
entity_registry: er.EntityRegistry,
) -> None:
"""Test Sonos favorites sensor."""
entity_registry = ent_reg.async_get(hass)
favorites = entity_registry.entities["sensor.sonos_favorites"]
assert hass.states.get(favorites.entity_id) is None

View file

@ -15,7 +15,7 @@ from homeassistant.components.sonos.switch import (
from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY
from homeassistant.const import ATTR_TIME, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as ent_reg
from homeassistant.helpers import entity_registry as er
from homeassistant.util import dt
from .conftest import SonosMockEvent
@ -23,10 +23,10 @@ from .conftest import SonosMockEvent
from tests.common import async_fire_time_changed
async def test_entity_registry(hass: HomeAssistant, async_autosetup_sonos) -> None:
async def test_entity_registry(
hass: HomeAssistant, async_autosetup_sonos, entity_registry: er.EntityRegistry
) -> None:
"""Test sonos device with alarm registered in the device registry."""
entity_registry = ent_reg.async_get(hass)
assert "media_player.zone_a" in entity_registry.entities
assert "switch.sonos_alarm_14" in entity_registry.entities
assert "switch.zone_a_status_light" in entity_registry.entities
@ -39,11 +39,13 @@ async def test_entity_registry(hass: HomeAssistant, async_autosetup_sonos) -> No
async def test_switch_attributes(
hass: HomeAssistant, async_autosetup_sonos, soco, fire_zgs_event
hass: HomeAssistant,
async_autosetup_sonos,
soco,
fire_zgs_event,
entity_registry: er.EntityRegistry,
) -> None:
"""Test for correct Sonos switch states."""
entity_registry = ent_reg.async_get(hass)
alarm = entity_registry.entities["switch.sonos_alarm_14"]
alarm_state = hass.states.get(alarm.entity_id)
assert alarm_state.state == STATE_ON
@ -135,10 +137,9 @@ async def test_alarm_create_delete(
alarm_clock,
alarm_clock_extended,
alarm_event,
entity_registry: er.EntityRegistry,
) -> None:
"""Test for correct creation and deletion of alarms during runtime."""
entity_registry = ent_reg.async_get(hass)
one_alarm = copy(alarm_clock.ListAlarms.return_value)
two_alarms = copy(alarm_clock_extended.ListAlarms.return_value)

View file

@ -16,7 +16,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
)
from homeassistant.core import Context, CoreState, HomeAssistant, State
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
@ -804,22 +804,18 @@ async def test_no_update_template_match_all(
},
],
)
async def test_unique_id(hass: HomeAssistant, start_ha) -> None:
async def test_unique_id(
hass: HomeAssistant, start_ha, entity_registry: er.EntityRegistry
) -> None:
"""Test unique_id option only creates one binary sensor per id."""
assert len(hass.states.async_all()) == 2
ent_reg = entity_registry.async_get(hass)
assert len(ent_reg.entities) == 2
assert (
ent_reg.async_get_entity_id("binary_sensor", "template", "group-id-sensor-id")
is not None
assert len(entity_registry.entities) == 2
assert entity_registry.async_get_entity_id(
"binary_sensor", "template", "group-id-sensor-id"
)
assert (
ent_reg.async_get_entity_id(
"binary_sensor", "template", "not-so-unique-anymore"
)
is not None
assert entity_registry.async_get_entity_id(
"binary_sensor", "template", "not-so-unique-anymore"
)
@ -1052,7 +1048,9 @@ async def test_restore_state(
},
],
)
async def test_trigger_entity(hass: HomeAssistant, start_ha) -> None:
async def test_trigger_entity(
hass: HomeAssistant, start_ha, entity_registry: er.EntityRegistry
) -> None:
"""Test trigger entity works."""
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.hello_name")
@ -1075,14 +1073,13 @@ async def test_trigger_entity(hass: HomeAssistant, start_ha) -> None:
assert state.attributes.get("plus_one") == 3
assert state.context is context
ent_reg = entity_registry.async_get(hass)
assert len(ent_reg.entities) == 2
assert len(entity_registry.entities) == 2
assert (
ent_reg.entities["binary_sensor.hello_name"].unique_id
entity_registry.entities["binary_sensor.hello_name"].unique_id
== "listening-test-event-hello_name-id"
)
assert (
ent_reg.entities["binary_sensor.via_list"].unique_id
entity_registry.entities["binary_sensor.via_list"].unique_id
== "listening-test-event-via_list-id"
)

View file

@ -18,7 +18,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
)
from homeassistant.core import Context, CoreState, HomeAssistant, State, callback
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.helpers.template import Template
from homeassistant.setup import ATTR_COMPONENT, async_setup_component
@ -558,19 +558,18 @@ async def test_no_template_match_all(
},
],
)
async def test_unique_id(hass: HomeAssistant, start_ha) -> None:
async def test_unique_id(
hass: HomeAssistant, start_ha, entity_registry: er.EntityRegistry
) -> None:
"""Test unique_id option only creates one sensor per id."""
assert len(hass.states.async_all()) == 2
ent_reg = entity_registry.async_get(hass)
assert len(ent_reg.entities) == 2
assert (
ent_reg.async_get_entity_id("sensor", "template", "group-id-sensor-id")
is not None
assert len(entity_registry.entities) == 2
assert entity_registry.async_get_entity_id(
"sensor", "template", "group-id-sensor-id"
)
assert (
ent_reg.async_get_entity_id("sensor", "template", "not-so-unique-anymore")
is not None
assert entity_registry.async_get_entity_id(
"sensor", "template", "not-so-unique-anymore"
)
@ -1094,7 +1093,9 @@ async def test_duplicate_templates(hass: HomeAssistant, start_ha) -> None:
},
],
)
async def test_trigger_entity(hass: HomeAssistant, start_ha) -> None:
async def test_trigger_entity(
hass: HomeAssistant, start_ha, entity_registry: er.EntityRegistry
) -> None:
"""Test trigger entity works."""
state = hass.states.get("sensor.hello_name")
assert state is not None
@ -1117,14 +1118,13 @@ async def test_trigger_entity(hass: HomeAssistant, start_ha) -> None:
assert state.attributes.get("unit_of_measurement") == "%"
assert state.context is context
ent_reg = entity_registry.async_get(hass)
assert len(ent_reg.entities) == 2
assert len(entity_registry.entities) == 2
assert (
ent_reg.entities["sensor.hello_name"].unique_id
entity_registry.entities["sensor.hello_name"].unique_id
== "listening-test-event-hello_name-id"
)
assert (
ent_reg.entities["sensor.via_list"].unique_id
entity_registry.entities["sensor.via_list"].unique_id
== "listening-test-event-via_list-id"
)
@ -1157,7 +1157,9 @@ async def test_trigger_entity(hass: HomeAssistant, start_ha) -> None:
},
],
)
async def test_trigger_entity_render_error(hass: HomeAssistant, start_ha) -> None:
async def test_trigger_entity_render_error(
hass: HomeAssistant, start_ha, entity_registry: er.EntityRegistry
) -> None:
"""Test trigger entity handles render error."""
state = hass.states.get("sensor.hello")
assert state is not None
@ -1170,9 +1172,8 @@ async def test_trigger_entity_render_error(hass: HomeAssistant, start_ha) -> Non
state = hass.states.get("sensor.hello")
assert state.state == STATE_UNAVAILABLE
ent_reg = entity_registry.async_get(hass)
assert len(ent_reg.entities) == 1
assert ent_reg.entities["sensor.hello"].unique_id == "no-base-id"
assert len(entity_registry.entities) == 1
assert entity_registry.entities["sensor.hello"].unique_id == "no-base-id"
@pytest.mark.parametrize(("count", "domain"), [(0, sensor.DOMAIN)])

View file

@ -22,7 +22,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
)
from homeassistant.core import Context, HomeAssistant, callback
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.setup import async_setup_component
@ -1183,7 +1183,9 @@ async def test_device_class(hass: HomeAssistant) -> None:
assert hass.states.get("media_player.tv").attributes["device_class"] == "tv"
async def test_unique_id(hass: HomeAssistant) -> None:
async def test_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test unique_id property."""
hass.states.async_set("sensor.test_sensor", "on")
@ -1199,8 +1201,10 @@ async def test_unique_id(hass: HomeAssistant) -> None:
},
)
await hass.async_block_till_done()
er = entity_registry.async_get(hass)
assert er.async_get("media_player.tv").unique_id == "universal_master_bed_tv"
assert (
entity_registry.async_get("media_player.tv").unique_id
== "universal_master_bed_tv"
)
async def test_invalid_state_template(hass: HomeAssistant) -> None:

View file

@ -38,7 +38,7 @@ from homeassistant.const import (
UnitOfEnergy,
)
from homeassistant.core import CoreState, HomeAssistant, State
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
@ -324,7 +324,9 @@ async def test_init(hass: HomeAssistant, yaml_config, config_entry_config) -> No
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR
async def test_unique_id(hass: HomeAssistant) -> None:
async def test_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test unique_id configuration option."""
yaml_config = {
"utility_meter": {
@ -342,10 +344,9 @@ async def test_unique_id(hass: HomeAssistant) -> None:
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
ent_reg = entity_registry.async_get(hass)
assert len(ent_reg.entities) == 4
assert ent_reg.entities["select.energy_bill"].unique_id == "1"
assert ent_reg.entities["sensor.energy_bill_onpeak"].unique_id == "1_onpeak"
assert len(entity_registry.entities) == 4
assert entity_registry.entities["select.energy_bill"].unique_id == "1"
assert entity_registry.entities["sensor.energy_bill_onpeak"].unique_id == "1_onpeak"
@pytest.mark.parametrize(

View file

@ -61,7 +61,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, State
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry
from homeassistant.helpers import device_registry as dr
from homeassistant.setup import async_setup_component
from homeassistant.util import dt
@ -275,7 +275,7 @@ async def test_select_sound_output(hass: HomeAssistant, client) -> None:
async def test_device_info_startup_off(
hass: HomeAssistant, client, monkeypatch
hass: HomeAssistant, client, monkeypatch, device_registry: dr.DeviceRegistry
) -> None:
"""Test device info when device is off at startup."""
monkeypatch.setattr(client, "system_info", None)
@ -285,8 +285,7 @@ async def test_device_info_startup_off(
assert hass.states.get(ENTITY_ID).state == STATE_OFF
device_reg = device_registry.async_get(hass)
device = device_reg.async_get_device({(DOMAIN, entry.unique_id)})
device = device_registry.async_get_device({(DOMAIN, entry.unique_id)})
assert device
assert device.identifiers == {(DOMAIN, entry.unique_id)}
@ -296,7 +295,9 @@ async def test_device_info_startup_off(
assert device.model is None
async def test_entity_attributes(hass: HomeAssistant, client, monkeypatch) -> None:
async def test_entity_attributes(
hass: HomeAssistant, client, monkeypatch, device_registry: dr.DeviceRegistry
) -> None:
"""Test entity attributes."""
entry = await setup_webostv(hass)
await client.mock_state_update()
@ -331,8 +332,7 @@ async def test_entity_attributes(hass: HomeAssistant, client, monkeypatch) -> No
assert attrs[ATTR_MEDIA_TITLE] == "Channel Name 2"
# Device Info
device_reg = device_registry.async_get(hass)
device = device_reg.async_get_device({(DOMAIN, entry.unique_id)})
device = device_registry.async_get_device({(DOMAIN, entry.unique_id)})
assert device
assert device.identifiers == {(DOMAIN, entry.unique_id)}

View file

@ -12,7 +12,7 @@ from homeassistant import runner
from homeassistant.components.wemo import CONF_DISCOVERY, CONF_STATIC, wemo_device
from homeassistant.components.wemo.const import DOMAIN, WEMO_SUBSCRIPTION_EVENT
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.update_coordinator import UpdateFailed
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
@ -31,7 +31,7 @@ def pywemo_model():
async def test_async_register_device_longpress_fails(
hass: HomeAssistant, pywemo_device
hass: HomeAssistant, pywemo_device, device_registry: dr.DeviceRegistry
) -> None:
"""Device is still registered if ensure_long_press_virtual_device fails."""
with patch.object(pywemo_device, "ensure_long_press_virtual_device") as elp:
@ -47,8 +47,7 @@ async def test_async_register_device_longpress_fails(
},
)
await hass.async_block_till_done()
dr = device_registry.async_get(hass)
device_entries = list(dr.devices.values())
device_entries = list(device_registry.devices.values())
assert len(device_entries) == 1
device = wemo_device.async_get_coordinator(hass, device_entries[0].id)
assert device.supports_long_press is False
@ -164,10 +163,11 @@ async def test_async_update_data_subscribed(
pywemo_device.get_state.assert_not_called()
async def test_device_info(hass: HomeAssistant, wemo_entity) -> None:
async def test_device_info(
hass: HomeAssistant, wemo_entity, device_registry: dr.DeviceRegistry
) -> None:
"""Verify the DeviceInfo data is set properly."""
dr = device_registry.async_get(hass)
device_entries = list(dr.devices.values())
device_entries = list(device_registry.devices.values())
assert len(device_entries) == 1
assert device_entries[0].connections == {
@ -178,10 +178,11 @@ async def test_device_info(hass: HomeAssistant, wemo_entity) -> None:
assert device_entries[0].sw_version == MOCK_FIRMWARE_VERSION
async def test_dli_device_info(hass: HomeAssistant, wemo_dli_entity) -> None:
async def test_dli_device_info(
hass: HomeAssistant, wemo_dli_entity, device_registry: dr.DeviceRegistry
) -> None:
"""Verify the DeviceInfo data for Digital Loggers emulated wemo device."""
dr = device_registry.async_get(hass)
device_entries = list(dr.devices.values())
device_entries = list(device_registry.devices.values())
assert device_entries[0].configuration_url == "http://127.0.0.1"
assert device_entries[0].identifiers == {(DOMAIN, "123456789")}

View file

@ -5,7 +5,7 @@ from unittest.mock import MagicMock
from whirlpool.washerdryer import MachineState
from homeassistant.core import CoreState, HomeAssistant, State
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import as_timestamp, utc_from_timestamp
from . import init_integration
@ -44,6 +44,7 @@ async def test_dryer_sensor_values(
hass: HomeAssistant,
mock_sensor_api_instances: MagicMock,
mock_sensor2_api: MagicMock,
entity_registry: er.EntityRegistry,
) -> None:
"""Test the sensor value callbacks."""
hass.state = CoreState.not_running
@ -69,8 +70,7 @@ async def test_dryer_sensor_values(
entity_id = "sensor.dryer_state"
mock_instance = mock_sensor2_api
registry = entity_registry.async_get(hass)
entry = registry.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
state = hass.states.get(entity_id)
assert state is not None
@ -108,6 +108,7 @@ async def test_washer_sensor_values(
hass: HomeAssistant,
mock_sensor_api_instances: MagicMock,
mock_sensor1_api: MagicMock,
entity_registry: er.EntityRegistry,
) -> None:
"""Test the sensor value callbacks."""
hass.state = CoreState.not_running
@ -133,8 +134,7 @@ async def test_washer_sensor_values(
entity_id = "sensor.washer_state"
mock_instance = mock_sensor1_api
registry = entity_registry.async_get(hass)
entry = registry.async_get(entity_id)
entry = entity_registry.async_get(entity_id)
assert entry
state = hass.states.get(entity_id)
assert state is not None
@ -147,13 +147,14 @@ async def test_washer_sensor_values(
assert state.state == thetimestamp.isoformat()
state_id = f"{entity_id.split('_')[0]}_detergent_level"
registry = entity_registry.async_get(hass)
entry = registry.async_get(state_id)
entry = entity_registry.async_get(state_id)
assert entry
assert entry.disabled
assert entry.disabled_by is entity_registry.RegistryEntryDisabler.INTEGRATION
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
update_entry = registry.async_update_entity(entry.entity_id, disabled_by=None)
update_entry = entity_registry.async_update_entity(
entry.entity_id, disabled_by=None
)
await hass.async_block_till_done()
assert update_entry != entry

View file

@ -19,8 +19,7 @@ from homeassistant.const import (
CONF_TYPE,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry
from homeassistant.helpers.device_registry import async_get as async_get_dev_reg
from homeassistant.helpers import device_registry as dr
from homeassistant.setup import async_setup_component
from . import make_advertisement
@ -82,7 +81,9 @@ async def test_event_motion_detected(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
async def test_get_triggers(hass: HomeAssistant) -> None:
async def test_get_triggers(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test that we get the expected triggers from a Xiaomi BLE motion sensor."""
mac = "DE:70:E8:B2:39:0C"
entry = await _async_setup_xiaomi_device(hass, mac)
@ -98,8 +99,7 @@ async def test_get_triggers(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
assert len(events) == 1
dev_reg = async_get_dev_reg(hass)
device = dev_reg.async_get_device({get_device_id(mac)})
device = device_registry.async_get_device({get_device_id(mac)})
assert device
expected_trigger = {
CONF_PLATFORM: "device",
@ -118,7 +118,9 @@ async def test_get_triggers(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
async def test_get_triggers_for_invalid_xiami_ble_device(hass: HomeAssistant) -> None:
async def test_get_triggers_for_invalid_xiami_ble_device(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test that we don't get triggers for an invalid device."""
mac = "DE:70:E8:B2:39:0C"
entry = await _async_setup_xiaomi_device(hass, mac)
@ -134,8 +136,7 @@ async def test_get_triggers_for_invalid_xiami_ble_device(hass: HomeAssistant) ->
await hass.async_block_till_done()
assert len(events) == 1
dev_reg = async_get_dev_reg(hass)
invalid_device = dev_reg.async_get_or_create(
invalid_device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, "invdevmac")},
)
@ -149,7 +150,9 @@ async def test_get_triggers_for_invalid_xiami_ble_device(hass: HomeAssistant) ->
await hass.async_block_till_done()
async def test_get_triggers_for_invalid_device_id(hass: HomeAssistant) -> None:
async def test_get_triggers_for_invalid_device_id(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test that we don't get triggers when using an invalid device_id."""
mac = "DE:70:E8:B2:39:0C"
entry = await _async_setup_xiaomi_device(hass, mac)
@ -163,11 +166,9 @@ async def test_get_triggers_for_invalid_device_id(hass: HomeAssistant) -> None:
# wait for the event
await hass.async_block_till_done()
dev_reg = async_get_dev_reg(hass)
invalid_device = dev_reg.async_get_or_create(
invalid_device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
assert invalid_device
triggers = await async_get_device_automations(
@ -179,7 +180,9 @@ async def test_get_triggers_for_invalid_device_id(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
async def test_if_fires_on_motion_detected(hass: HomeAssistant, calls) -> None:
async def test_if_fires_on_motion_detected(
hass: HomeAssistant, calls, device_registry: dr.DeviceRegistry
) -> None:
"""Test for motion event trigger firing."""
mac = "DE:70:E8:B2:39:0C"
entry = await _async_setup_xiaomi_device(hass, mac)
@ -193,8 +196,7 @@ async def test_if_fires_on_motion_detected(hass: HomeAssistant, calls) -> None:
# wait for the event
await hass.async_block_till_done()
dev_reg = async_get_dev_reg(hass)
device = dev_reg.async_get_device({get_device_id(mac)})
device = device_registry.async_get_device({get_device_id(mac)})
device_id = device.id
assert await async_setup_component(
@ -237,7 +239,9 @@ async def test_if_fires_on_motion_detected(hass: HomeAssistant, calls) -> None:
async def test_automation_with_invalid_trigger_type(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test for automation with invalid trigger type."""
mac = "DE:70:E8:B2:39:0C"
@ -252,8 +256,7 @@ async def test_automation_with_invalid_trigger_type(
# wait for the event
await hass.async_block_till_done()
dev_reg = async_get_dev_reg(hass)
device = dev_reg.async_get_device({get_device_id(mac)})
device = device_registry.async_get_device({get_device_id(mac)})
device_id = device.id
assert await async_setup_component(
@ -285,7 +288,9 @@ async def test_automation_with_invalid_trigger_type(
async def test_automation_with_invalid_trigger_event_property(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test for automation with invalid trigger event property."""
mac = "DE:70:E8:B2:39:0C"
@ -300,8 +305,7 @@ async def test_automation_with_invalid_trigger_event_property(
# wait for the event
await hass.async_block_till_done()
dev_reg = async_get_dev_reg(hass)
device = dev_reg.async_get_device({get_device_id(mac)})
device = device_registry.async_get_device({get_device_id(mac)})
device_id = device.id
assert await async_setup_component(
@ -332,7 +336,9 @@ async def test_automation_with_invalid_trigger_event_property(
await hass.async_block_till_done()
async def test_triggers_for_invalid__model(hass: HomeAssistant, calls) -> None:
async def test_triggers_for_invalid__model(
hass: HomeAssistant, calls, device_registry: dr.DeviceRegistry
) -> None:
"""Test invalid model doesn't return triggers."""
mac = "DE:70:E8:B2:39:0C"
entry = await _async_setup_xiaomi_device(hass, mac)
@ -346,9 +352,8 @@ async def test_triggers_for_invalid__model(hass: HomeAssistant, calls) -> None:
# wait for the event
await hass.async_block_till_done()
dev_reg = async_get_dev_reg(hass)
# modify model to invalid model
invalid_model = dev_reg.async_get_or_create(
invalid_model = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, mac)},
model="invalid model",

View file

@ -10,7 +10,7 @@ import zigpy.zcl.foundation as zcl_f
import homeassistant.components.zha.core.const as zha_const
from homeassistant.components.zha.core.helpers import async_get_zha_config_value
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed
@ -157,12 +157,10 @@ def find_entity_ids(domain, zha_device, hass):
machine so that we can test state changes.
"""
registry = entity_registry.async_get(hass)
registry = er.async_get(hass)
return [
entity.entity_id
for entity in entity_registry.async_entries_for_device(
registry, zha_device.device_id
)
for entity in er.async_entries_for_device(registry, zha_device.device_id)
if entity.domain == domain
]

View file

@ -28,7 +28,7 @@ import homeassistant.components.zha.sensor
import homeassistant.components.zha.switch
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
import homeassistant.helpers.entity_registry
import homeassistant.helpers.entity_registry as er
from .common import get_zha_gateway
from .conftest import SIG_EP_INPUT, SIG_EP_OUTPUT, SIG_EP_PROFILE, SIG_EP_TYPE
@ -104,9 +104,7 @@ async def test_devices(
zha_device_joined_restored,
) -> None:
"""Test device discovery."""
entity_registry = homeassistant.helpers.entity_registry.async_get(
hass_disable_services
)
entity_registry = er.async_get(hass_disable_services)
zigpy_device = zigpy_device_mock(
device[SIG_ENDPOINTS],

View file

@ -15,7 +15,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, device_registry
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.setup import async_setup_component
from tests.common import async_get_device_automations
@ -26,13 +26,13 @@ async def test_get_actions(
client: Client,
lock_schlage_be469: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we get the expected actions from a zwave_js node."""
node = lock_schlage_be469
dev_reg = device_registry.async_get(hass)
driver = client.driver
assert driver
device = dev_reg.async_get_device({get_device_id(driver, node)})
device = device_registry.async_get_device({get_device_id(driver, node)})
assert device
expected_actions = [
{
@ -92,7 +92,7 @@ async def test_get_actions(
assert action in actions
# Test that we don't return actions for a controller node
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(driver, client.driver.controller.nodes[1])}
)
assert device
@ -107,13 +107,13 @@ async def test_get_actions_meter(
client: Client,
aeon_smart_switch_6: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we get the expected meter actions from a zwave_js node."""
node = aeon_smart_switch_6
dev_reg = device_registry.async_get(hass)
driver = client.driver
assert driver
device = dev_reg.async_get_device({get_device_id(driver, node)})
device = device_registry.async_get_device({get_device_id(driver, node)})
assert device
actions = await async_get_device_automations(
hass, DeviceAutomationType.ACTION, device.id
@ -127,14 +127,14 @@ async def test_actions(
client: Client,
climate_radio_thermostat_ct100_plus: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test actions."""
node = climate_radio_thermostat_ct100_plus
driver = client.driver
assert driver
device_id = get_device_id(driver, node)
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device({device_id})
device = device_registry.async_get_device({device_id})
assert device
assert await async_setup_component(
@ -249,14 +249,14 @@ async def test_actions_multiple_calls(
client: Client,
climate_radio_thermostat_ct100_plus: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test actions can be called multiple times and still work."""
node = climate_radio_thermostat_ct100_plus
driver = client.driver
assert driver
device_id = get_device_id(driver, node)
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device({device_id})
device = device_registry.async_get_device({device_id})
assert device
assert await async_setup_component(
@ -296,14 +296,14 @@ async def test_lock_actions(
client: Client,
lock_schlage_be469: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test actions for locks."""
node = lock_schlage_be469
driver = client.driver
assert driver
device_id = get_device_id(driver, node)
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device({device_id})
device = device_registry.async_get_device({device_id})
assert device
assert await async_setup_component(
@ -367,14 +367,14 @@ async def test_reset_meter_action(
client: Client,
aeon_smart_switch_6: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test reset_meter action."""
node = aeon_smart_switch_6
driver = client.driver
assert driver
device_id = get_device_id(driver, node)
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device({device_id})
device = device_registry.async_get_device({device_id})
assert device
assert await async_setup_component(
@ -415,10 +415,10 @@ async def test_get_action_capabilities(
client: Client,
climate_radio_thermostat_ct100_plus: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we get the expected action capabilities."""
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(client.driver, climate_radio_thermostat_ct100_plus)}
)
assert device
@ -582,12 +582,10 @@ async def test_get_action_capabilities_lock_triggers(
client: Client,
lock_schlage_be469: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we get the expected action capabilities for lock triggers."""
dev_reg = device_registry.async_get(hass)
device = device_registry.async_entries_for_config_entry(
dev_reg, integration.entry_id
)[0]
device = dr.async_entries_for_config_entry(device_registry, integration.entry_id)[0]
# Test clear_lock_usercode
capabilities = await device_action.async_get_action_capabilities(
@ -632,13 +630,13 @@ async def test_get_action_capabilities_meter_triggers(
client: Client,
aeon_smart_switch_6: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we get the expected action capabilities for meter triggers."""
node = aeon_smart_switch_6
dev_reg = device_registry.async_get(hass)
driver = client.driver
assert driver
device = dev_reg.async_get_device({get_device_id(driver, node)})
device = device_registry.async_get_device({get_device_id(driver, node)})
assert device
capabilities = await device_action.async_get_action_capabilities(
hass,
@ -662,12 +660,10 @@ async def test_failure_scenarios(
client: Client,
hank_binary_switch: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test failure scenarios."""
dev_reg = device_registry.async_get(hass)
device = device_registry.async_entries_for_config_entry(
dev_reg, integration.entry_id
)[0]
device = dr.async_entries_for_config_entry(device_registry, integration.entry_id)[0]
with pytest.raises(HomeAssistantError):
await device_action.async_call_action_from_config(
@ -687,16 +683,16 @@ async def test_unavailable_entity_actions(
client: Client,
lock_schlage_be469: Node,
integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test unavailable entities are not included in actions list."""
entity_id_unavailable = "binary_sensor.touchscreen_deadbolt_home_security_intrusion"
hass.states.async_set(entity_id_unavailable, STATE_UNAVAILABLE, force_update=True)
await hass.async_block_till_done()
node = lock_schlage_be469
dev_reg = device_registry.async_get(hass)
driver = client.driver
assert driver
device = dev_reg.async_get_device({get_device_id(driver, node)})
device = device_registry.async_get_device({get_device_id(driver, node)})
assert device
actions = await async_get_device_automations(
hass, DeviceAutomationType.ACTION, device.id

View file

@ -21,7 +21,7 @@ from homeassistant.components.zwave_js.helpers import (
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, device_registry
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.setup import async_setup_component
from tests.common import async_get_device_automations, async_mock_service
@ -34,11 +34,14 @@ def calls(hass):
async def test_get_conditions(
hass: HomeAssistant, client, lock_schlage_be469, integration
hass: HomeAssistant,
client,
lock_schlage_be469,
integration,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we get the expected onditions from a zwave_js."""
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)}
)
assert device
@ -78,7 +81,7 @@ async def test_get_conditions(
assert condition in conditions
# Test that we don't return actions for a controller node
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(client.driver, client.driver.controller.nodes[1])}
)
assert device
@ -91,11 +94,15 @@ async def test_get_conditions(
async def test_node_status_state(
hass: HomeAssistant, client, lock_schlage_be469, integration, calls
hass: HomeAssistant,
client,
lock_schlage_be469,
integration,
calls,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test for node_status conditions."""
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)}
)
assert device
@ -252,11 +259,15 @@ async def test_node_status_state(
async def test_config_parameter_state(
hass: HomeAssistant, client, lock_schlage_be469, integration, calls
hass: HomeAssistant,
client,
lock_schlage_be469,
integration,
calls,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test for config_parameter conditions."""
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)}
)
assert device
@ -368,11 +379,15 @@ async def test_config_parameter_state(
async def test_value_state(
hass: HomeAssistant, client, lock_schlage_be469, integration, calls
hass: HomeAssistant,
client,
lock_schlage_be469,
integration,
calls,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test for value conditions."""
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)}
)
assert device
@ -416,11 +431,14 @@ async def test_value_state(
async def test_get_condition_capabilities_node_status(
hass: HomeAssistant, client, lock_schlage_be469, integration
hass: HomeAssistant,
client,
lock_schlage_be469,
integration,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we don't get capabilities from a node_status condition."""
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)}
)
assert device
@ -453,11 +471,14 @@ async def test_get_condition_capabilities_node_status(
async def test_get_condition_capabilities_value(
hass: HomeAssistant, client, lock_schlage_be469, integration
hass: HomeAssistant,
client,
lock_schlage_be469,
integration,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we get the expected capabilities from a value condition."""
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)}
)
assert device
@ -502,12 +523,15 @@ async def test_get_condition_capabilities_value(
async def test_get_condition_capabilities_config_parameter(
hass: HomeAssistant, client, climate_radio_thermostat_ct100_plus, integration
hass: HomeAssistant,
client,
climate_radio_thermostat_ct100_plus,
integration,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we get the expected capabilities from a config_parameter condition."""
node = climate_radio_thermostat_ct100_plus
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(client.driver, climate_radio_thermostat_ct100_plus)}
)
assert device
@ -585,11 +609,14 @@ async def test_get_condition_capabilities_config_parameter(
async def test_failure_scenarios(
hass: HomeAssistant, client, hank_binary_switch, integration
hass: HomeAssistant,
client,
hank_binary_switch,
integration,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test failure scenarios."""
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_device(
device = device_registry.async_get_device(
{get_device_id(client.driver, hank_binary_switch)}
)
assert device

View file

@ -30,7 +30,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
async def test_generic_fan(
@ -538,23 +538,26 @@ async def test_leviton_zw4sf_fan(
async def test_thermostat_fan(
hass: HomeAssistant, client, climate_adc_t3000, integration
hass: HomeAssistant,
client,
climate_adc_t3000,
integration,
entity_registry: er.EntityRegistry,
) -> None:
"""Test the fan entity for a z-wave fan."""
node = climate_adc_t3000
entity_id = "fan.adc_t3000"
registry = entity_registry.async_get(hass)
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 entity_registry.RegistryEntryDisabler.INTEGRATION
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
# Test enabling entity
updated_entry = registry.async_update_entity(entity_id, disabled_by=None)
updated_entry = entity_registry.async_update_entity(entity_id, disabled_by=None)
assert updated_entry != entry
assert updated_entry.disabled is False
@ -769,22 +772,25 @@ async def test_thermostat_fan(
async def test_thermostat_fan_without_off(
hass: HomeAssistant, client, climate_radio_thermostat_ct100_plus, integration
hass: HomeAssistant,
client,
climate_radio_thermostat_ct100_plus,
integration,
entity_registry: er.EntityRegistry,
) -> None:
"""Test the fan entity for a z-wave fan without "off" property."""
entity_id = "fan.z_wave_thermostat"
registry = entity_registry.async_get(hass)
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 entity_registry.RegistryEntryDisabler.INTEGRATION
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
# Test enabling entity
updated_entry = registry.async_update_entity(entity_id, disabled_by=None)
updated_entry = entity_registry.async_update_entity(entity_id, disabled_by=None)
assert updated_entry != entry
assert updated_entry.disabled is False
@ -827,22 +833,25 @@ async def test_thermostat_fan_without_off(
async def test_thermostat_fan_without_preset_modes(
hass: HomeAssistant, client, climate_adc_t3000_missing_fan_mode_states, integration
hass: HomeAssistant,
client,
climate_adc_t3000_missing_fan_mode_states,
integration,
entity_registry: er.EntityRegistry,
) -> None:
"""Test the fan entity for a z-wave fan without "states" metadata."""
entity_id = "fan.adc_t3000_missing_fan_mode_states"
registry = entity_registry.async_get(hass)
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 entity_registry.RegistryEntryDisabler.INTEGRATION
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
# Test enabling entity
updated_entry = registry.async_update_entity(entity_id, disabled_by=None)
updated_entry = entity_registry.async_update_entity(entity_id, disabled_by=None)
assert updated_entry != entry
assert updated_entry.disabled is False