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.components.number import DOMAIN as NUMBER_DOMAIN, SERVICE_SET_VALUE
from homeassistant.const import ATTR_ENTITY_ID from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant 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( async def test_number_entities(
hass: HomeAssistant, async_autosetup_sonos, soco hass: HomeAssistant, async_autosetup_sonos, soco, entity_registry: er.EntityRegistry
) -> None: ) -> None:
"""Test number entities.""" """Test number entities."""
entity_registry = ent_reg.async_get(hass)
bass_number = entity_registry.entities["number.zone_a_bass"] bass_number = entity_registry.entities["number.zone_a_bass"]
bass_state = hass.states.get(bass_number.entity_id) bass_state = hass.states.get(bass_number.entity_id)
assert bass_state.state == "1" 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.config_entries import RELOAD_AFTER_UPDATE_DELAY
from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant 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 homeassistant.util import dt as dt_util
from .conftest import SonosMockEvent from .conftest import SonosMockEvent
@ -19,37 +19,31 @@ from tests.common import async_fire_time_changed
async def test_entity_registry_unsupported( async def test_entity_registry_unsupported(
hass: HomeAssistant, async_setup_sonos, soco hass: HomeAssistant, async_setup_sonos, soco, entity_registry: er.EntityRegistry
) -> None: ) -> None:
"""Test sonos device without battery registered in the device registry.""" """Test sonos device without battery registered in the device registry."""
soco.get_battery_info.side_effect = NotSupportedException soco.get_battery_info.side_effect = NotSupportedException
await async_setup_sonos() await async_setup_sonos()
entity_registry = ent_reg.async_get(hass)
assert "media_player.zone_a" in entity_registry.entities assert "media_player.zone_a" in entity_registry.entities
assert "sensor.zone_a_battery" not 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 assert "binary_sensor.zone_a_power" not in entity_registry.entities
async def test_entity_registry_supported( async def test_entity_registry_supported(
hass: HomeAssistant, async_autosetup_sonos, soco hass: HomeAssistant, async_autosetup_sonos, soco, entity_registry: er.EntityRegistry
) -> None: ) -> None:
"""Test sonos device with battery registered in the device registry.""" """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 "media_player.zone_a" in entity_registry.entities
assert "sensor.zone_a_battery" in entity_registry.entities assert "sensor.zone_a_battery" in entity_registry.entities
assert "binary_sensor.zone_a_power" in entity_registry.entities assert "binary_sensor.zone_a_power" in entity_registry.entities
async def test_battery_attributes( async def test_battery_attributes(
hass: HomeAssistant, async_autosetup_sonos, soco hass: HomeAssistant, async_autosetup_sonos, soco, entity_registry: er.EntityRegistry
) -> None: ) -> None:
"""Test sonos device with battery state.""" """Test sonos device with battery state."""
entity_registry = ent_reg.async_get(hass)
battery = entity_registry.entities["sensor.zone_a_battery"] battery = entity_registry.entities["sensor.zone_a_battery"]
battery_state = hass.states.get(battery.entity_id) battery_state = hass.states.get(battery.entity_id)
assert battery_state.state == "100" assert battery_state.state == "100"
@ -64,7 +58,11 @@ async def test_battery_attributes(
async def test_battery_on_s1( 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: ) -> None:
"""Test battery state updates on a Sonos S1 device.""" """Test battery state updates on a Sonos S1 device."""
soco.get_battery_info.return_value = {} soco.get_battery_info.return_value = {}
@ -74,8 +72,6 @@ async def test_battery_on_s1(
subscription = soco.deviceProperties.subscribe.return_value subscription = soco.deviceProperties.subscribe.return_value
sub_callback = subscription.callback sub_callback = subscription.callback
entity_registry = ent_reg.async_get(hass)
assert "sensor.zone_a_battery" not 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 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( 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: ) -> None:
"""Test audio input sensor.""" """Test audio input sensor."""
entity_registry = ent_reg.async_get(hass)
subscription = soco.avTransport.subscribe.return_value subscription = soco.avTransport.subscribe.return_value
sub_callback = subscription.callback sub_callback = subscription.callback
sub_callback(tv_event) sub_callback(tv_event)
@ -183,10 +182,13 @@ async def test_audio_input_sensor(
async def test_microphone_binary_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: ) -> None:
"""Test microphone binary sensor.""" """Test microphone binary sensor."""
entity_registry = ent_reg.async_get(hass)
assert "binary_sensor.zone_a_microphone" in entity_registry.entities assert "binary_sensor.zone_a_microphone" in entity_registry.entities
mic_binary_sensor = entity_registry.entities["binary_sensor.zone_a_microphone"] 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( 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: ) -> None:
"""Test Sonos favorites sensor.""" """Test Sonos favorites sensor."""
entity_registry = ent_reg.async_get(hass)
favorites = entity_registry.entities["sensor.sonos_favorites"] favorites = entity_registry.entities["sensor.sonos_favorites"]
assert hass.states.get(favorites.entity_id) is None 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.config_entries import RELOAD_AFTER_UPDATE_DELAY
from homeassistant.const import ATTR_TIME, STATE_OFF, STATE_ON from homeassistant.const import ATTR_TIME, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant 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 homeassistant.util import dt
from .conftest import SonosMockEvent from .conftest import SonosMockEvent
@ -23,10 +23,10 @@ from .conftest import SonosMockEvent
from tests.common import async_fire_time_changed 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.""" """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 "media_player.zone_a" in entity_registry.entities
assert "switch.sonos_alarm_14" in entity_registry.entities assert "switch.sonos_alarm_14" in entity_registry.entities
assert "switch.zone_a_status_light" 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( 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: ) -> None:
"""Test for correct Sonos switch states.""" """Test for correct Sonos switch states."""
entity_registry = ent_reg.async_get(hass)
alarm = entity_registry.entities["switch.sonos_alarm_14"] alarm = entity_registry.entities["switch.sonos_alarm_14"]
alarm_state = hass.states.get(alarm.entity_id) alarm_state = hass.states.get(alarm.entity_id)
assert alarm_state.state == STATE_ON assert alarm_state.state == STATE_ON
@ -135,10 +137,9 @@ async def test_alarm_create_delete(
alarm_clock, alarm_clock,
alarm_clock_extended, alarm_clock_extended,
alarm_event, alarm_event,
entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test for correct creation and deletion of alarms during runtime.""" """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) one_alarm = copy(alarm_clock.ListAlarms.return_value)
two_alarms = copy(alarm_clock_extended.ListAlarms.return_value) two_alarms = copy(alarm_clock_extended.ListAlarms.return_value)

View file

@ -16,7 +16,7 @@ from homeassistant.const import (
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.core import Context, CoreState, HomeAssistant, State 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.helpers.entity_component import async_update_entity
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util 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.""" """Test unique_id option only creates one binary sensor per id."""
assert len(hass.states.async_all()) == 2 assert len(hass.states.async_all()) == 2
ent_reg = entity_registry.async_get(hass) assert len(entity_registry.entities) == 2
assert entity_registry.async_get_entity_id(
assert len(ent_reg.entities) == 2 "binary_sensor", "template", "group-id-sensor-id"
assert (
ent_reg.async_get_entity_id("binary_sensor", "template", "group-id-sensor-id")
is not None
) )
assert ( assert entity_registry.async_get_entity_id(
ent_reg.async_get_entity_id( "binary_sensor", "template", "not-so-unique-anymore"
"binary_sensor", "template", "not-so-unique-anymore"
)
is not None
) )
@ -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.""" """Test trigger entity works."""
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("binary_sensor.hello_name") 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.attributes.get("plus_one") == 3
assert state.context is context assert state.context is context
ent_reg = entity_registry.async_get(hass) assert len(entity_registry.entities) == 2
assert len(ent_reg.entities) == 2
assert ( 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" == "listening-test-event-hello_name-id"
) )
assert ( 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" == "listening-test-event-via_list-id"
) )

View file

@ -18,7 +18,7 @@ from homeassistant.const import (
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.core import Context, CoreState, HomeAssistant, State, callback 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.entity_component import async_update_entity
from homeassistant.helpers.template import Template from homeassistant.helpers.template import Template
from homeassistant.setup import ATTR_COMPONENT, async_setup_component 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.""" """Test unique_id option only creates one sensor per id."""
assert len(hass.states.async_all()) == 2 assert len(hass.states.async_all()) == 2
ent_reg = entity_registry.async_get(hass) assert len(entity_registry.entities) == 2
assert len(ent_reg.entities) == 2 assert entity_registry.async_get_entity_id(
assert ( "sensor", "template", "group-id-sensor-id"
ent_reg.async_get_entity_id("sensor", "template", "group-id-sensor-id")
is not None
) )
assert ( assert entity_registry.async_get_entity_id(
ent_reg.async_get_entity_id("sensor", "template", "not-so-unique-anymore") "sensor", "template", "not-so-unique-anymore"
is not None
) )
@ -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.""" """Test trigger entity works."""
state = hass.states.get("sensor.hello_name") state = hass.states.get("sensor.hello_name")
assert state is not None 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.attributes.get("unit_of_measurement") == "%"
assert state.context is context assert state.context is context
ent_reg = entity_registry.async_get(hass) assert len(entity_registry.entities) == 2
assert len(ent_reg.entities) == 2
assert ( assert (
ent_reg.entities["sensor.hello_name"].unique_id entity_registry.entities["sensor.hello_name"].unique_id
== "listening-test-event-hello_name-id" == "listening-test-event-hello_name-id"
) )
assert ( assert (
ent_reg.entities["sensor.via_list"].unique_id entity_registry.entities["sensor.via_list"].unique_id
== "listening-test-event-via_list-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.""" """Test trigger entity handles render error."""
state = hass.states.get("sensor.hello") state = hass.states.get("sensor.hello")
assert state is not None 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") state = hass.states.get("sensor.hello")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
ent_reg = entity_registry.async_get(hass) assert len(entity_registry.entities) == 1
assert len(ent_reg.entities) == 1 assert entity_registry.entities["sensor.hello"].unique_id == "no-base-id"
assert ent_reg.entities["sensor.hello"].unique_id == "no-base-id"
@pytest.mark.parametrize(("count", "domain"), [(0, sensor.DOMAIN)]) @pytest.mark.parametrize(("count", "domain"), [(0, sensor.DOMAIN)])

View file

@ -22,7 +22,7 @@ from homeassistant.const import (
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.core import Context, HomeAssistant, callback 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.helpers.event import async_track_state_change_event
from homeassistant.setup import async_setup_component 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" 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.""" """Test unique_id property."""
hass.states.async_set("sensor.test_sensor", "on") 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() await hass.async_block_till_done()
er = entity_registry.async_get(hass) assert (
assert er.async_get("media_player.tv").unique_id == "universal_master_bed_tv" entity_registry.async_get("media_player.tv").unique_id
== "universal_master_bed_tv"
)
async def test_invalid_state_template(hass: HomeAssistant) -> None: async def test_invalid_state_template(hass: HomeAssistant) -> None:

View file

@ -38,7 +38,7 @@ from homeassistant.const import (
UnitOfEnergy, UnitOfEnergy,
) )
from homeassistant.core import CoreState, HomeAssistant, State 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 from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util 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 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.""" """Test unique_id configuration option."""
yaml_config = { yaml_config = {
"utility_meter": { "utility_meter": {
@ -342,10 +344,9 @@ async def test_unique_id(hass: HomeAssistant) -> None:
hass.bus.async_fire(EVENT_HOMEASSISTANT_START) hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done() await hass.async_block_till_done()
ent_reg = entity_registry.async_get(hass) assert len(entity_registry.entities) == 4
assert len(ent_reg.entities) == 4 assert entity_registry.entities["select.energy_bill"].unique_id == "1"
assert ent_reg.entities["select.energy_bill"].unique_id == "1" assert entity_registry.entities["sensor.energy_bill_onpeak"].unique_id == "1_onpeak"
assert ent_reg.entities["sensor.energy_bill_onpeak"].unique_id == "1_onpeak"
@pytest.mark.parametrize( @pytest.mark.parametrize(

View file

@ -61,7 +61,7 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant, State from homeassistant.core import HomeAssistant, State
from homeassistant.exceptions import HomeAssistantError 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.setup import async_setup_component
from homeassistant.util import dt 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( async def test_device_info_startup_off(
hass: HomeAssistant, client, monkeypatch hass: HomeAssistant, client, monkeypatch, device_registry: dr.DeviceRegistry
) -> None: ) -> None:
"""Test device info when device is off at startup.""" """Test device info when device is off at startup."""
monkeypatch.setattr(client, "system_info", None) 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 assert hass.states.get(ENTITY_ID).state == STATE_OFF
device_reg = device_registry.async_get(hass) device = device_registry.async_get_device({(DOMAIN, entry.unique_id)})
device = device_reg.async_get_device({(DOMAIN, entry.unique_id)})
assert device assert device
assert device.identifiers == {(DOMAIN, entry.unique_id)} assert device.identifiers == {(DOMAIN, entry.unique_id)}
@ -296,7 +295,9 @@ async def test_device_info_startup_off(
assert device.model is None 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.""" """Test entity attributes."""
entry = await setup_webostv(hass) entry = await setup_webostv(hass)
await client.mock_state_update() 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" assert attrs[ATTR_MEDIA_TITLE] == "Channel Name 2"
# Device Info # Device Info
device_reg = device_registry.async_get(hass) device = device_registry.async_get_device({(DOMAIN, entry.unique_id)})
device = device_reg.async_get_device({(DOMAIN, entry.unique_id)})
assert device assert device
assert device.identifiers == {(DOMAIN, entry.unique_id)} 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 import CONF_DISCOVERY, CONF_STATIC, wemo_device
from homeassistant.components.wemo.const import DOMAIN, WEMO_SUBSCRIPTION_EVENT from homeassistant.components.wemo.const import DOMAIN, WEMO_SUBSCRIPTION_EVENT
from homeassistant.core import HomeAssistant, callback 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.helpers.update_coordinator import UpdateFailed
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
@ -31,7 +31,7 @@ def pywemo_model():
async def test_async_register_device_longpress_fails( async def test_async_register_device_longpress_fails(
hass: HomeAssistant, pywemo_device hass: HomeAssistant, pywemo_device, device_registry: dr.DeviceRegistry
) -> None: ) -> None:
"""Device is still registered if ensure_long_press_virtual_device fails.""" """Device is still registered if ensure_long_press_virtual_device fails."""
with patch.object(pywemo_device, "ensure_long_press_virtual_device") as elp: 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() await hass.async_block_till_done()
dr = device_registry.async_get(hass) device_entries = list(device_registry.devices.values())
device_entries = list(dr.devices.values())
assert len(device_entries) == 1 assert len(device_entries) == 1
device = wemo_device.async_get_coordinator(hass, device_entries[0].id) device = wemo_device.async_get_coordinator(hass, device_entries[0].id)
assert device.supports_long_press is False 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() 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.""" """Verify the DeviceInfo data is set properly."""
dr = device_registry.async_get(hass) device_entries = list(device_registry.devices.values())
device_entries = list(dr.devices.values())
assert len(device_entries) == 1 assert len(device_entries) == 1
assert device_entries[0].connections == { 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 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.""" """Verify the DeviceInfo data for Digital Loggers emulated wemo device."""
dr = device_registry.async_get(hass) device_entries = list(device_registry.devices.values())
device_entries = list(dr.devices.values())
assert device_entries[0].configuration_url == "http://127.0.0.1" assert device_entries[0].configuration_url == "http://127.0.0.1"
assert device_entries[0].identifiers == {(DOMAIN, "123456789")} assert device_entries[0].identifiers == {(DOMAIN, "123456789")}

View file

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

View file

@ -19,8 +19,7 @@ from homeassistant.const import (
CONF_TYPE, CONF_TYPE,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import async_get as async_get_dev_reg
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from . import make_advertisement from . import make_advertisement
@ -82,7 +81,9 @@ async def test_event_motion_detected(hass: HomeAssistant) -> None:
await hass.async_block_till_done() 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.""" """Test that we get the expected triggers from a Xiaomi BLE motion sensor."""
mac = "DE:70:E8:B2:39:0C" mac = "DE:70:E8:B2:39:0C"
entry = await _async_setup_xiaomi_device(hass, mac) 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() await hass.async_block_till_done()
assert len(events) == 1 assert len(events) == 1
dev_reg = async_get_dev_reg(hass) device = device_registry.async_get_device({get_device_id(mac)})
device = dev_reg.async_get_device({get_device_id(mac)})
assert device assert device
expected_trigger = { expected_trigger = {
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
@ -118,7 +118,9 @@ async def test_get_triggers(hass: HomeAssistant) -> None:
await hass.async_block_till_done() 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.""" """Test that we don't get triggers for an invalid device."""
mac = "DE:70:E8:B2:39:0C" mac = "DE:70:E8:B2:39:0C"
entry = await _async_setup_xiaomi_device(hass, mac) 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() await hass.async_block_till_done()
assert len(events) == 1 assert len(events) == 1
dev_reg = async_get_dev_reg(hass) invalid_device = device_registry.async_get_or_create(
invalid_device = dev_reg.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, "invdevmac")}, 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() 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.""" """Test that we don't get triggers when using an invalid device_id."""
mac = "DE:70:E8:B2:39:0C" mac = "DE:70:E8:B2:39:0C"
entry = await _async_setup_xiaomi_device(hass, mac) 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 # wait for the event
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = async_get_dev_reg(hass) invalid_device = device_registry.async_get_or_create(
invalid_device = dev_reg.async_get_or_create(
config_entry_id=entry.entry_id, 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 assert invalid_device
triggers = await async_get_device_automations( 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() 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.""" """Test for motion event trigger firing."""
mac = "DE:70:E8:B2:39:0C" mac = "DE:70:E8:B2:39:0C"
entry = await _async_setup_xiaomi_device(hass, mac) 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 # wait for the event
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = async_get_dev_reg(hass) device = device_registry.async_get_device({get_device_id(mac)})
device = dev_reg.async_get_device({get_device_id(mac)})
device_id = device.id device_id = device.id
assert await async_setup_component( 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( async def test_automation_with_invalid_trigger_type(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test for automation with invalid trigger type.""" """Test for automation with invalid trigger type."""
mac = "DE:70:E8:B2:39:0C" mac = "DE:70:E8:B2:39:0C"
@ -252,8 +256,7 @@ async def test_automation_with_invalid_trigger_type(
# wait for the event # wait for the event
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = async_get_dev_reg(hass) device = device_registry.async_get_device({get_device_id(mac)})
device = dev_reg.async_get_device({get_device_id(mac)})
device_id = device.id device_id = device.id
assert await async_setup_component( 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( async def test_automation_with_invalid_trigger_event_property(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test for automation with invalid trigger event property.""" """Test for automation with invalid trigger event property."""
mac = "DE:70:E8:B2:39:0C" mac = "DE:70:E8:B2:39:0C"
@ -300,8 +305,7 @@ async def test_automation_with_invalid_trigger_event_property(
# wait for the event # wait for the event
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = async_get_dev_reg(hass) device = device_registry.async_get_device({get_device_id(mac)})
device = dev_reg.async_get_device({get_device_id(mac)})
device_id = device.id device_id = device.id
assert await async_setup_component( assert await async_setup_component(
@ -332,7 +336,9 @@ async def test_automation_with_invalid_trigger_event_property(
await hass.async_block_till_done() 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.""" """Test invalid model doesn't return triggers."""
mac = "DE:70:E8:B2:39:0C" mac = "DE:70:E8:B2:39:0C"
entry = await _async_setup_xiaomi_device(hass, mac) 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 # wait for the event
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = async_get_dev_reg(hass)
# modify model to invalid model # 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, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, mac)}, identifiers={(DOMAIN, mac)},
model="invalid model", 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 import homeassistant.components.zha.core.const as zha_const
from homeassistant.components.zha.core.helpers import async_get_zha_config_value 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 import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed 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. machine so that we can test state changes.
""" """
registry = entity_registry.async_get(hass) registry = er.async_get(hass)
return [ return [
entity.entity_id entity.entity_id
for entity in entity_registry.async_entries_for_device( for entity in er.async_entries_for_device(registry, zha_device.device_id)
registry, zha_device.device_id
)
if entity.domain == domain if entity.domain == domain
] ]

View file

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

View file

@ -15,7 +15,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_UNAVAILABLE from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError 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 homeassistant.setup import async_setup_component
from tests.common import async_get_device_automations from tests.common import async_get_device_automations
@ -26,13 +26,13 @@ async def test_get_actions(
client: Client, client: Client,
lock_schlage_be469: Node, lock_schlage_be469: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test we get the expected actions from a zwave_js node.""" """Test we get the expected actions from a zwave_js node."""
node = lock_schlage_be469 node = lock_schlage_be469
dev_reg = device_registry.async_get(hass)
driver = client.driver driver = client.driver
assert 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 assert device
expected_actions = [ expected_actions = [
{ {
@ -92,7 +92,7 @@ async def test_get_actions(
assert action in actions assert action in actions
# Test that we don't return actions for a controller node # 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])} {get_device_id(driver, client.driver.controller.nodes[1])}
) )
assert device assert device
@ -107,13 +107,13 @@ async def test_get_actions_meter(
client: Client, client: Client,
aeon_smart_switch_6: Node, aeon_smart_switch_6: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test we get the expected meter actions from a zwave_js node.""" """Test we get the expected meter actions from a zwave_js node."""
node = aeon_smart_switch_6 node = aeon_smart_switch_6
dev_reg = device_registry.async_get(hass)
driver = client.driver driver = client.driver
assert 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 assert device
actions = await async_get_device_automations( actions = await async_get_device_automations(
hass, DeviceAutomationType.ACTION, device.id hass, DeviceAutomationType.ACTION, device.id
@ -127,14 +127,14 @@ async def test_actions(
client: Client, client: Client,
climate_radio_thermostat_ct100_plus: Node, climate_radio_thermostat_ct100_plus: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test actions.""" """Test actions."""
node = climate_radio_thermostat_ct100_plus node = climate_radio_thermostat_ct100_plus
driver = client.driver driver = client.driver
assert driver assert driver
device_id = get_device_id(driver, node) device_id = get_device_id(driver, node)
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device({device_id})
device = dev_reg.async_get_device({device_id})
assert device assert device
assert await async_setup_component( assert await async_setup_component(
@ -249,14 +249,14 @@ async def test_actions_multiple_calls(
client: Client, client: Client,
climate_radio_thermostat_ct100_plus: Node, climate_radio_thermostat_ct100_plus: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test actions can be called multiple times and still work.""" """Test actions can be called multiple times and still work."""
node = climate_radio_thermostat_ct100_plus node = climate_radio_thermostat_ct100_plus
driver = client.driver driver = client.driver
assert driver assert driver
device_id = get_device_id(driver, node) device_id = get_device_id(driver, node)
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device({device_id})
device = dev_reg.async_get_device({device_id})
assert device assert device
assert await async_setup_component( assert await async_setup_component(
@ -296,14 +296,14 @@ async def test_lock_actions(
client: Client, client: Client,
lock_schlage_be469: Node, lock_schlage_be469: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test actions for locks.""" """Test actions for locks."""
node = lock_schlage_be469 node = lock_schlage_be469
driver = client.driver driver = client.driver
assert driver assert driver
device_id = get_device_id(driver, node) device_id = get_device_id(driver, node)
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device({device_id})
device = dev_reg.async_get_device({device_id})
assert device assert device
assert await async_setup_component( assert await async_setup_component(
@ -367,14 +367,14 @@ async def test_reset_meter_action(
client: Client, client: Client,
aeon_smart_switch_6: Node, aeon_smart_switch_6: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test reset_meter action.""" """Test reset_meter action."""
node = aeon_smart_switch_6 node = aeon_smart_switch_6
driver = client.driver driver = client.driver
assert driver assert driver
device_id = get_device_id(driver, node) device_id = get_device_id(driver, node)
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device({device_id})
device = dev_reg.async_get_device({device_id})
assert device assert device
assert await async_setup_component( assert await async_setup_component(
@ -415,10 +415,10 @@ async def test_get_action_capabilities(
client: Client, client: Client,
climate_radio_thermostat_ct100_plus: Node, climate_radio_thermostat_ct100_plus: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test we get the expected action capabilities.""" """Test we get the expected action capabilities."""
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device(
{get_device_id(client.driver, climate_radio_thermostat_ct100_plus)} {get_device_id(client.driver, climate_radio_thermostat_ct100_plus)}
) )
assert device assert device
@ -582,12 +582,10 @@ async def test_get_action_capabilities_lock_triggers(
client: Client, client: Client,
lock_schlage_be469: Node, lock_schlage_be469: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test we get the expected action capabilities for lock triggers.""" """Test we get the expected action capabilities for lock triggers."""
dev_reg = device_registry.async_get(hass) device = dr.async_entries_for_config_entry(device_registry, integration.entry_id)[0]
device = device_registry.async_entries_for_config_entry(
dev_reg, integration.entry_id
)[0]
# Test clear_lock_usercode # Test clear_lock_usercode
capabilities = await device_action.async_get_action_capabilities( capabilities = await device_action.async_get_action_capabilities(
@ -632,13 +630,13 @@ async def test_get_action_capabilities_meter_triggers(
client: Client, client: Client,
aeon_smart_switch_6: Node, aeon_smart_switch_6: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test we get the expected action capabilities for meter triggers.""" """Test we get the expected action capabilities for meter triggers."""
node = aeon_smart_switch_6 node = aeon_smart_switch_6
dev_reg = device_registry.async_get(hass)
driver = client.driver driver = client.driver
assert 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 assert device
capabilities = await device_action.async_get_action_capabilities( capabilities = await device_action.async_get_action_capabilities(
hass, hass,
@ -662,12 +660,10 @@ async def test_failure_scenarios(
client: Client, client: Client,
hank_binary_switch: Node, hank_binary_switch: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test failure scenarios.""" """Test failure scenarios."""
dev_reg = device_registry.async_get(hass) device = dr.async_entries_for_config_entry(device_registry, integration.entry_id)[0]
device = device_registry.async_entries_for_config_entry(
dev_reg, integration.entry_id
)[0]
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
await device_action.async_call_action_from_config( await device_action.async_call_action_from_config(
@ -687,16 +683,16 @@ async def test_unavailable_entity_actions(
client: Client, client: Client,
lock_schlage_be469: Node, lock_schlage_be469: Node,
integration: ConfigEntry, integration: ConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test unavailable entities are not included in actions list.""" """Test unavailable entities are not included in actions list."""
entity_id_unavailable = "binary_sensor.touchscreen_deadbolt_home_security_intrusion" entity_id_unavailable = "binary_sensor.touchscreen_deadbolt_home_security_intrusion"
hass.states.async_set(entity_id_unavailable, STATE_UNAVAILABLE, force_update=True) hass.states.async_set(entity_id_unavailable, STATE_UNAVAILABLE, force_update=True)
await hass.async_block_till_done() await hass.async_block_till_done()
node = lock_schlage_be469 node = lock_schlage_be469
dev_reg = device_registry.async_get(hass)
driver = client.driver driver = client.driver
assert 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 assert device
actions = await async_get_device_automations( actions = await async_get_device_automations(
hass, DeviceAutomationType.ACTION, device.id 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.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError 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 homeassistant.setup import async_setup_component
from tests.common import async_get_device_automations, async_mock_service from tests.common import async_get_device_automations, async_mock_service
@ -34,11 +34,14 @@ def calls(hass):
async def test_get_conditions( async def test_get_conditions(
hass: HomeAssistant, client, lock_schlage_be469, integration hass: HomeAssistant,
client,
lock_schlage_be469,
integration,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test we get the expected onditions from a zwave_js.""" """Test we get the expected onditions from a zwave_js."""
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)} {get_device_id(client.driver, lock_schlage_be469)}
) )
assert device assert device
@ -78,7 +81,7 @@ async def test_get_conditions(
assert condition in conditions assert condition in conditions
# Test that we don't return actions for a controller node # 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])} {get_device_id(client.driver, client.driver.controller.nodes[1])}
) )
assert device assert device
@ -91,11 +94,15 @@ async def test_get_conditions(
async def test_node_status_state( 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: ) -> None:
"""Test for node_status conditions.""" """Test for node_status conditions."""
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)} {get_device_id(client.driver, lock_schlage_be469)}
) )
assert device assert device
@ -252,11 +259,15 @@ async def test_node_status_state(
async def test_config_parameter_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: ) -> None:
"""Test for config_parameter conditions.""" """Test for config_parameter conditions."""
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)} {get_device_id(client.driver, lock_schlage_be469)}
) )
assert device assert device
@ -368,11 +379,15 @@ async def test_config_parameter_state(
async def test_value_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: ) -> None:
"""Test for value conditions.""" """Test for value conditions."""
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)} {get_device_id(client.driver, lock_schlage_be469)}
) )
assert device assert device
@ -416,11 +431,14 @@ async def test_value_state(
async def test_get_condition_capabilities_node_status( 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: ) -> None:
"""Test we don't get capabilities from a node_status condition.""" """Test we don't get capabilities from a node_status condition."""
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)} {get_device_id(client.driver, lock_schlage_be469)}
) )
assert device assert device
@ -453,11 +471,14 @@ async def test_get_condition_capabilities_node_status(
async def test_get_condition_capabilities_value( 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: ) -> None:
"""Test we get the expected capabilities from a value condition.""" """Test we get the expected capabilities from a value condition."""
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device(
{get_device_id(client.driver, lock_schlage_be469)} {get_device_id(client.driver, lock_schlage_be469)}
) )
assert device assert device
@ -502,12 +523,15 @@ async def test_get_condition_capabilities_value(
async def test_get_condition_capabilities_config_parameter( 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: ) -> None:
"""Test we get the expected capabilities from a config_parameter condition.""" """Test we get the expected capabilities from a config_parameter condition."""
node = climate_radio_thermostat_ct100_plus node = climate_radio_thermostat_ct100_plus
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device(
{get_device_id(client.driver, climate_radio_thermostat_ct100_plus)} {get_device_id(client.driver, climate_radio_thermostat_ct100_plus)}
) )
assert device assert device
@ -585,11 +609,14 @@ async def test_get_condition_capabilities_config_parameter(
async def test_failure_scenarios( async def test_failure_scenarios(
hass: HomeAssistant, client, hank_binary_switch, integration hass: HomeAssistant,
client,
hank_binary_switch,
integration,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test failure scenarios.""" """Test failure scenarios."""
dev_reg = device_registry.async_get(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device(
{get_device_id(client.driver, hank_binary_switch)} {get_device_id(client.driver, hank_binary_switch)}
) )
assert device assert device

View file

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