Fix namespace-import pylint warning in components (#119299)

This commit is contained in:
epenet 2024-06-10 15:55:08 +02:00 committed by GitHub
parent fbaba3753b
commit c896458fcf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 273 additions and 263 deletions

View file

@ -20,15 +20,16 @@ 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 issue_registry as ir from homeassistant.helpers import entity_registry as er, issue_registry as ir
from homeassistant.helpers.entity_registry import async_get as async_get_entities
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
from tests.common import get_fixture_path from tests.common import get_fixture_path
async def test_load_values_when_added_to_hass(hass: HomeAssistant) -> None: async def test_load_values_when_added_to_hass(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that sensor initializes with observations of relevant entities.""" """Test that sensor initializes with observations of relevant entities."""
config = { config = {
@ -57,7 +58,6 @@ async def test_load_values_when_added_to_hass(hass: HomeAssistant) -> None:
assert await async_setup_component(hass, "binary_sensor", config) assert await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done() await hass.async_block_till_done()
entity_registry = async_get_entities(hass)
assert ( assert (
entity_registry.entities["binary_sensor.test_binary"].unique_id entity_registry.entities["binary_sensor.test_binary"].unique_id
== "bayesian-3b4c9563-5e84-4167-8fe7-8f507e796d72" == "bayesian-3b4c9563-5e84-4167-8fe7-8f507e796d72"

View file

@ -6,10 +6,7 @@ from homeassistant.components.bthome.const import CONF_SUBTYPE, DOMAIN
from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM, CONF_TYPE from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM, CONF_TYPE
from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers.device_registry import ( from homeassistant.helpers import device_registry as dr
CONNECTION_NETWORK_MAC,
async_get as async_get_dev_reg,
)
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from . import make_bthome_v2_adv from . import make_bthome_v2_adv
@ -87,7 +84,9 @@ async def test_event_rotate_dimmer(hass: HomeAssistant) -> None:
await hass.async_block_till_done() await hass.async_block_till_done()
async def test_get_triggers_button(hass: HomeAssistant) -> None: async def test_get_triggers_button(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test that we get the expected triggers from a BTHome BLE sensor.""" """Test that we get the expected triggers from a BTHome BLE sensor."""
mac = "A4:C1:38:8D:18:B2" mac = "A4:C1:38:8D:18:B2"
entry = await _async_setup_bthome_device(hass, mac) entry = await _async_setup_bthome_device(hass, mac)
@ -103,8 +102,7 @@ async def test_get_triggers_button(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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
assert device assert device
expected_trigger = { expected_trigger = {
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
@ -123,7 +121,9 @@ async def test_get_triggers_button(hass: HomeAssistant) -> None:
await hass.async_block_till_done() await hass.async_block_till_done()
async def test_get_triggers_dimmer(hass: HomeAssistant) -> None: async def test_get_triggers_dimmer(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test that we get the expected triggers from a BTHome BLE sensor.""" """Test that we get the expected triggers from a BTHome BLE sensor."""
mac = "A4:C1:38:8D:18:B2" mac = "A4:C1:38:8D:18:B2"
entry = await _async_setup_bthome_device(hass, mac) entry = await _async_setup_bthome_device(hass, mac)
@ -139,8 +139,7 @@ async def test_get_triggers_dimmer(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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
assert device assert device
expected_trigger = { expected_trigger = {
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
@ -159,7 +158,9 @@ async def test_get_triggers_dimmer(hass: HomeAssistant) -> None:
await hass.async_block_till_done() await hass.async_block_till_done()
async def test_get_triggers_for_invalid_bthome_ble_device(hass: HomeAssistant) -> None: async def test_get_triggers_for_invalid_bthome_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 = "A4:C1:38:8D:18:B2" mac = "A4:C1:38:8D:18:B2"
entry = await _async_setup_bthome_device(hass, mac) entry = await _async_setup_bthome_device(hass, mac)
@ -175,8 +176,7 @@ async def test_get_triggers_for_invalid_bthome_ble_device(hass: HomeAssistant) -
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(events) == 0 assert len(events) == 0
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")},
) )
@ -190,7 +190,9 @@ async def test_get_triggers_for_invalid_bthome_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_bthome_device(hass, mac) entry = await _async_setup_bthome_device(hass, mac)
@ -204,11 +206,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={(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(
@ -221,7 +221,9 @@ async def test_get_triggers_for_invalid_device_id(hass: HomeAssistant) -> None:
async def test_if_fires_on_motion_detected( async def test_if_fires_on_motion_detected(
hass: HomeAssistant, service_calls: list[ServiceCall] hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
service_calls: list[ServiceCall],
) -> None: ) -> 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"
@ -236,8 +238,7 @@ async def test_if_fires_on_motion_detected(
# # 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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
device_id = device.id device_id = device.id
assert await async_setup_component( assert await async_setup_component(

View file

@ -10,7 +10,7 @@ from aiohttp.client_exceptions import ClientResponseError
from homeassistant.components.buienradar.const import CONF_DELTA, DOMAIN from homeassistant.components.buienradar.const import CONF_DELTA, DOMAIN
from homeassistant.const import CONF_COUNTRY_CODE, CONF_LATITUDE, CONF_LONGITUDE from homeassistant.const import CONF_COUNTRY_CODE, CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import async_get 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 tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -32,7 +32,7 @@ def radar_map_url(country_code: str = "NL") -> str:
async def _setup_config_entry(hass, entry): async def _setup_config_entry(hass, entry):
entity_registry = async_get(hass) entity_registry = er.async_get(hass)
entity_registry.async_get_or_create( entity_registry.async_get_or_create(
domain="camera", domain="camera",
platform="buienradar", platform="buienradar",

View file

@ -5,7 +5,7 @@ from http import HTTPStatus
from homeassistant.components.buienradar.const import DOMAIN from homeassistant.components.buienradar.const import DOMAIN
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import async_get from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMocker from tests.test_util.aiohttp import AiohttpClientMocker
@ -18,7 +18,9 @@ TEST_CFG_DATA = {CONF_LATITUDE: TEST_LATITUDE, CONF_LONGITUDE: TEST_LONGITUDE}
async def test_smoke_test_setup_component( async def test_smoke_test_setup_component(
aioclient_mock: AiohttpClientMocker, hass: HomeAssistant aioclient_mock: AiohttpClientMocker,
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Smoke test for successfully set-up with default config.""" """Smoke test for successfully set-up with default config."""
aioclient_mock.get( aioclient_mock.get(
@ -28,7 +30,6 @@ async def test_smoke_test_setup_component(
mock_entry.add_to_hass(hass) mock_entry.add_to_hass(hass)
entity_registry = async_get(hass)
for cond in CONDITIONS: for cond in CONDITIONS:
entity_registry.async_get_or_create( entity_registry.async_get_or_create(
domain="sensor", domain="sensor",

View file

@ -6,13 +6,12 @@ from pytest_unordered import unordered
from homeassistant.components.config import entity_registry from homeassistant.components.config import entity_registry
from homeassistant.const import ATTR_ICON, EntityCategory from homeassistant.const import ATTR_ICON, EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntryDisabler from homeassistant.helpers.device_registry import DeviceEntryDisabler
from homeassistant.helpers.entity_registry import ( from homeassistant.helpers.entity_registry import (
RegistryEntry, RegistryEntry,
RegistryEntryDisabler, RegistryEntryDisabler,
RegistryEntryHider, RegistryEntryHider,
async_get as async_get_entity_registry,
) )
from tests.common import ( from tests.common import (
@ -863,6 +862,7 @@ async def test_enable_entity_disabled_device(
hass: HomeAssistant, hass: HomeAssistant,
client: MockHAClientWebSocket, client: MockHAClientWebSocket,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test enabling entity of disabled device.""" """Test enabling entity of disabled device."""
entity_id = "test_domain.test_platform_1234" entity_id = "test_domain.test_platform_1234"
@ -889,8 +889,7 @@ async def test_enable_entity_disabled_device(
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is None assert state is None
entity_reg = async_get_entity_registry(hass) entity_entry = entity_registry.async_get(entity_id)
entity_entry = entity_reg.async_get(entity_id)
assert entity_entry.config_entry_id == config_entry.entry_id assert entity_entry.config_entry_id == config_entry.entry_id
assert entity_entry.device_id == device.id assert entity_entry.device_id == device.id
assert entity_entry.disabled_by == RegistryEntryDisabler.DEVICE assert entity_entry.disabled_by == RegistryEntryDisabler.DEVICE

View file

@ -21,7 +21,6 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_registry import async_entries_for_config_entry
from .test_gateway import ( from .test_gateway import (
DECONZ_WEB_REQUEST, DECONZ_WEB_REQUEST,
@ -687,7 +686,8 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_service_call(
assert not hass.states.get("binary_sensor.presence_sensor") assert not hass.states.get("binary_sensor.presence_sensor")
assert ( assert (
len(async_entries_for_config_entry(entity_registry, config_entry.entry_id)) == 0 len(er.async_entries_for_config_entry(entity_registry, config_entry.entry_id))
== 0
) )
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
@ -738,7 +738,8 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_options_change(
assert not hass.states.get("binary_sensor.presence_sensor") assert not hass.states.get("binary_sensor.presence_sensor")
assert ( assert (
len(async_entries_for_config_entry(entity_registry, config_entry.entry_id)) == 0 len(er.async_entries_for_config_entry(entity_registry, config_entry.entry_id))
== 0
) )
aioclient_mock.clear_requests() aioclient_mock.clear_requests()

View file

@ -22,7 +22,6 @@ from homeassistant.components.deconz.services import (
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_registry import async_entries_for_config_entry
from .test_gateway import ( from .test_gateway import (
BRIDGEID, BRIDGEID,
@ -368,7 +367,7 @@ async def test_remove_orphaned_entries_service(
) )
assert ( assert (
len(async_entries_for_config_entry(entity_registry, config_entry.entry_id)) len(er.async_entries_for_config_entry(entity_registry, config_entry.entry_id))
== 3 # Light, switch battery and orphan == 3 # Light, switch battery and orphan
) )
@ -391,6 +390,6 @@ async def test_remove_orphaned_entries_service(
) )
assert ( assert (
len(async_entries_for_config_entry(entity_registry, config_entry.entry_id)) len(er.async_entries_for_config_entry(entity_registry, config_entry.entry_id))
== 2 # Light and switch battery == 2 # Light and switch battery
) )

View file

@ -7,7 +7,7 @@ import pytest
from homeassistant.components.websocket_api import TYPE_RESULT from homeassistant.components.websocket_api import TYPE_RESULT
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import async_get from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.system_info import async_get_system_info from homeassistant.helpers.system_info import async_get_system_info
from homeassistant.loader import async_get_integration from homeassistant.loader import async_get_integration
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -82,7 +82,9 @@ async def test_websocket(
@pytest.mark.usefixtures("enable_custom_integrations") @pytest.mark.usefixtures("enable_custom_integrations")
async def test_download_diagnostics( async def test_download_diagnostics(
hass: HomeAssistant, hass_client: ClientSessionGenerator hass: HomeAssistant,
hass_client: ClientSessionGenerator,
device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test download diagnostics.""" """Test download diagnostics."""
config_entry = MockConfigEntry(domain="fake_integration") config_entry = MockConfigEntry(domain="fake_integration")
@ -178,8 +180,7 @@ async def test_download_diagnostics(
"data": {"config_entry": "info"}, "data": {"config_entry": "info"},
} }
dev_reg = async_get(hass) device = device_registry.async_get_or_create(
device = dev_reg.async_get_or_create(
config_entry_id=config_entry.entry_id, identifiers={("test", "test")} config_entry_id=config_entry.entry_id, identifiers={("test", "test")}
) )

View file

@ -45,16 +45,8 @@ from homeassistant.const import (
CONF_URL, CONF_URL,
) )
from homeassistant.core import CoreState, HomeAssistant from homeassistant.core import CoreState, HomeAssistant
from homeassistant.helpers.device_registry import ( from homeassistant.helpers import device_registry as dr, entity_registry as er
CONNECTION_NETWORK_MAC,
CONNECTION_UPNP,
async_get as async_get_dr,
)
from homeassistant.helpers.entity_component import async_update_entity from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.helpers.entity_registry import (
async_entries_for_config_entry,
async_get as async_get_er,
)
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from .conftest import ( from .conftest import (
@ -80,7 +72,8 @@ async def setup_mock_component(hass: HomeAssistant, mock_entry: MockConfigEntry)
assert await hass.config_entries.async_setup(mock_entry.entry_id) is True assert await hass.config_entries.async_setup(mock_entry.entry_id) is True
await hass.async_block_till_done() await hass.async_block_till_done()
entries = async_entries_for_config_entry(async_get_er(hass), mock_entry.entry_id) entity_registry = er.async_get(hass)
entries = er.async_entries_for_config_entry(entity_registry, mock_entry.entry_id)
assert len(entries) == 1 assert len(entries) == 1
return entries[0].entity_id return entries[0].entity_id
@ -345,6 +338,7 @@ async def test_setup_entry_with_options(
async def test_setup_entry_mac_address( async def test_setup_entry_mac_address(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
domain_data_mock: Mock, domain_data_mock: Mock,
config_entry_mock: MockConfigEntry, config_entry_mock: MockConfigEntry,
ssdp_scanner_mock: Mock, ssdp_scanner_mock: Mock,
@ -356,17 +350,17 @@ async def test_setup_entry_mac_address(
await async_update_entity(hass, mock_entity_id) await async_update_entity(hass, mock_entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
# Check the device registry connections for MAC address # Check the device registry connections for MAC address
dev_reg = async_get_dr(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device( connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
assert (CONNECTION_NETWORK_MAC, MOCK_MAC_ADDRESS) in device.connections assert (dr.CONNECTION_NETWORK_MAC, MOCK_MAC_ADDRESS) in device.connections
async def test_setup_entry_no_mac_address( async def test_setup_entry_no_mac_address(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
domain_data_mock: Mock, domain_data_mock: Mock,
config_entry_mock_no_mac: MockConfigEntry, config_entry_mock_no_mac: MockConfigEntry,
ssdp_scanner_mock: Mock, ssdp_scanner_mock: Mock,
@ -378,13 +372,12 @@ async def test_setup_entry_no_mac_address(
await async_update_entity(hass, mock_entity_id) await async_update_entity(hass, mock_entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
# Check the device registry connections does not include the MAC address # Check the device registry connections does not include the MAC address
dev_reg = async_get_dr(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device( connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
assert (CONNECTION_NETWORK_MAC, MOCK_MAC_ADDRESS) not in device.connections assert (dr.CONNECTION_NETWORK_MAC, MOCK_MAC_ADDRESS) not in device.connections
async def test_event_subscribe_failure( async def test_event_subscribe_failure(
@ -445,15 +438,17 @@ async def test_event_subscribe_rejected(
async def test_available_device( async def test_available_device(
hass: HomeAssistant, dmr_device_mock: Mock, mock_entity_id: str hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
dmr_device_mock: Mock,
mock_entity_id: str,
) -> None: ) -> None:
"""Test a DlnaDmrEntity with a connected DmrDevice.""" """Test a DlnaDmrEntity with a connected DmrDevice."""
# Check hass device information is filled in # Check hass device information is filled in
await async_update_entity(hass, mock_entity_id) await async_update_entity(hass, mock_entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = async_get_dr(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device( connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
@ -1260,6 +1255,7 @@ async def test_playback_update_state(
) )
async def test_unavailable_device( async def test_unavailable_device(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
domain_data_mock: Mock, domain_data_mock: Mock,
ssdp_scanner_mock: Mock, ssdp_scanner_mock: Mock,
config_entry_mock: MockConfigEntry, config_entry_mock: MockConfigEntry,
@ -1357,9 +1353,8 @@ async def test_unavailable_device(
) )
# Check hass device information has not been filled in yet # Check hass device information has not been filled in yet
dev_reg = async_get_dr(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device( connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
@ -1387,6 +1382,7 @@ async def test_unavailable_device(
) )
async def test_become_available( async def test_become_available(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
domain_data_mock: Mock, domain_data_mock: Mock,
ssdp_scanner_mock: Mock, ssdp_scanner_mock: Mock,
config_entry_mock: MockConfigEntry, config_entry_mock: MockConfigEntry,
@ -1404,9 +1400,8 @@ async def test_become_available(
assert mock_state.state == ha_const.STATE_UNAVAILABLE assert mock_state.state == ha_const.STATE_UNAVAILABLE
# Check hass device information has not been filled in yet # Check hass device information has not been filled in yet
dev_reg = async_get_dr(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device( connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
@ -1446,9 +1441,8 @@ async def test_become_available(
assert mock_state is not None assert mock_state is not None
assert mock_state.state == MediaPlayerState.IDLE assert mock_state.state == MediaPlayerState.IDLE
# Check hass device information is now filled in # Check hass device information is now filled in
dev_reg = async_get_dr(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device( connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
@ -2281,6 +2275,7 @@ async def test_config_update_poll_availability(
async def test_config_update_mac_address( async def test_config_update_mac_address(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
domain_data_mock: Mock, domain_data_mock: Mock,
config_entry_mock_no_mac: MockConfigEntry, config_entry_mock_no_mac: MockConfigEntry,
ssdp_scanner_mock: Mock, ssdp_scanner_mock: Mock,
@ -2293,13 +2288,12 @@ async def test_config_update_mac_address(
domain_data_mock.upnp_factory.async_create_device.reset_mock() domain_data_mock.upnp_factory.async_create_device.reset_mock()
# Check the device registry connections does not include the MAC address # Check the device registry connections does not include the MAC address
dev_reg = async_get_dr(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device( connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
assert (CONNECTION_NETWORK_MAC, MOCK_MAC_ADDRESS) not in device.connections assert (dr.CONNECTION_NETWORK_MAC, MOCK_MAC_ADDRESS) not in device.connections
# MAC address discovered and set by config flow # MAC address discovered and set by config flow
hass.config_entries.async_update_entry( hass.config_entries.async_update_entry(
@ -2314,12 +2308,12 @@ async def test_config_update_mac_address(
await hass.async_block_till_done() await hass.async_block_till_done()
# Device registry connections should now include the MAC address # Device registry connections should now include the MAC address
device = dev_reg.async_get_device( device = device_registry.async_get_device(
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)}, connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
assert (CONNECTION_NETWORK_MAC, MOCK_MAC_ADDRESS) in device.connections assert (dr.CONNECTION_NETWORK_MAC, MOCK_MAC_ADDRESS) in device.connections
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -2328,6 +2322,8 @@ async def test_config_update_mac_address(
) )
async def test_connections_restored( async def test_connections_restored(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
domain_data_mock: Mock, domain_data_mock: Mock,
ssdp_scanner_mock: Mock, ssdp_scanner_mock: Mock,
config_entry_mock: MockConfigEntry, config_entry_mock: MockConfigEntry,
@ -2345,9 +2341,8 @@ async def test_connections_restored(
assert mock_state.state == ha_const.STATE_UNAVAILABLE assert mock_state.state == ha_const.STATE_UNAVAILABLE
# Check hass device information has not been filled in yet # Check hass device information has not been filled in yet
dev_reg = async_get_dr(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device( connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
@ -2387,9 +2382,8 @@ async def test_connections_restored(
assert mock_state is not None assert mock_state is not None
assert mock_state.state == MediaPlayerState.IDLE assert mock_state.state == MediaPlayerState.IDLE
# Check hass device information is now filled in # Check hass device information is now filled in
dev_reg = async_get_dr(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device( connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
@ -2410,17 +2404,15 @@ async def test_connections_restored(
dmr_device_mock.async_unsubscribe_services.assert_awaited_once() dmr_device_mock.async_unsubscribe_services.assert_awaited_once()
# Check hass device information has not been filled in yet # Check hass device information has not been filled in yet
dev_reg = async_get_dr(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device( connections={(dr.CONNECTION_UPNP, MOCK_DEVICE_UDN)},
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
identifiers=set(), identifiers=set(),
) )
assert device is not None assert device is not None
assert device.connections == previous_connections assert device.connections == previous_connections
# Verify the entity remains linked to the device # Verify the entity remains linked to the device
ent_reg = async_get_er(hass) entry = entity_registry.async_get(mock_entity_id)
entry = ent_reg.async_get(mock_entity_id)
assert entry is not None assert entry is not None
assert entry.device_id == device.id assert entry.device_id == device.id
@ -2435,6 +2427,8 @@ async def test_connections_restored(
async def test_udn_upnp_connection_added_if_missing( async def test_udn_upnp_connection_added_if_missing(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
domain_data_mock: Mock, domain_data_mock: Mock,
ssdp_scanner_mock: Mock, ssdp_scanner_mock: Mock,
config_entry_mock: MockConfigEntry, config_entry_mock: MockConfigEntry,
@ -2449,8 +2443,7 @@ async def test_udn_upnp_connection_added_if_missing(
config_entry_mock.add_to_hass(hass) config_entry_mock.add_to_hass(hass)
# Cause connection attempts to fail before adding entity # Cause connection attempts to fail before adding entity
ent_reg = async_get_er(hass) entry = entity_registry.async_get_or_create(
entry = ent_reg.async_get_or_create(
mp.DOMAIN, mp.DOMAIN,
DOMAIN, DOMAIN,
MOCK_DEVICE_UDN, MOCK_DEVICE_UDN,
@ -2458,14 +2451,13 @@ async def test_udn_upnp_connection_added_if_missing(
) )
mock_entity_id = entry.entity_id mock_entity_id = entry.entity_id
dev_reg = async_get_dr(hass) device = device_registry.async_get_or_create(
device = dev_reg.async_get_or_create(
config_entry_id=config_entry_mock.entry_id, config_entry_id=config_entry_mock.entry_id,
connections={(CONNECTION_NETWORK_MAC, MOCK_MAC_ADDRESS)}, connections={(dr.CONNECTION_NETWORK_MAC, MOCK_MAC_ADDRESS)},
identifiers=set(), identifiers=set(),
) )
ent_reg.async_update_entity(mock_entity_id, device_id=device.id) entity_registry.async_update_entity(mock_entity_id, device_id=device.id)
domain_data_mock.upnp_factory.async_create_device.side_effect = UpnpConnectionError domain_data_mock.upnp_factory.async_create_device.side_effect = UpnpConnectionError
assert await hass.config_entries.async_setup(config_entry_mock.entry_id) is True assert await hass.config_entries.async_setup(config_entry_mock.entry_id) is True
@ -2476,7 +2468,6 @@ async def test_udn_upnp_connection_added_if_missing(
assert mock_state.state == ha_const.STATE_UNAVAILABLE assert mock_state.state == ha_const.STATE_UNAVAILABLE
# Check hass device information has not been filled in yet # Check hass device information has not been filled in yet
dev_reg = async_get_dr(hass) device = device_registry.async_get(device.id)
device = dev_reg.async_get(device.id)
assert device is not None assert device is not None
assert (CONNECTION_UPNP, MOCK_DEVICE_UDN) in device.connections assert (dr.CONNECTION_UPNP, MOCK_DEVICE_UDN) in device.connections

View file

@ -13,7 +13,7 @@ from homeassistant.components.image import DOMAIN as IMAGE_DOMAIN
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_UNKNOWN, Platform from homeassistant.const import STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from .const import MOCK_FB_SERVICES, MOCK_USER_DATA from .const import MOCK_FB_SERVICES, MOCK_USER_DATA
@ -89,6 +89,7 @@ GUEST_WIFI_DISABLED: dict[str, dict] = {
async def test_image_entity( async def test_image_entity(
hass: HomeAssistant, hass: HomeAssistant,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
fc_class_mock, fc_class_mock,
fh_class_mock, fh_class_mock,
@ -122,7 +123,6 @@ async def test_image_entity(
"friendly_name": "Mock Title GuestWifi", "friendly_name": "Mock Title GuestWifi",
} }
entity_registry = async_get_entity_registry(hass)
entity_entry = entity_registry.async_get("image.mock_title_guestwifi") entity_entry = entity_registry.async_get("image.mock_title_guestwifi")
assert entity_entry.unique_id == "1c_ed_6f_12_34_11_guestwifi_qr_code" assert entity_entry.unique_id == "1c_ed_6f_12_34_11_guestwifi_qr_code"

View file

@ -10,10 +10,7 @@ from homeassistant.components.greeneye_monitor import DOMAIN
from homeassistant.components.sensor import SensorDeviceClass from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import UnitOfElectricPotential, UnitOfPower from homeassistant.const import UnitOfElectricPotential, UnitOfPower
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import ( from homeassistant.helpers import entity_registry as er
RegistryEntry,
async_get as get_entity_registry,
)
from .common import add_listeners from .common import add_listeners
@ -82,15 +79,15 @@ def assert_sensor_registered(
sensor_type: str, sensor_type: str,
number: int, number: int,
name: str, name: str,
) -> RegistryEntry: ) -> er.RegistryEntry:
"""Assert that a sensor entity of a given type was registered properly.""" """Assert that a sensor entity of a given type was registered properly."""
registry = get_entity_registry(hass) entity_registry = er.async_get(hass)
unique_id = f"{serial_number}-{sensor_type}-{number}" unique_id = f"{serial_number}-{sensor_type}-{number}"
entity_id = registry.async_get_entity_id("sensor", DOMAIN, unique_id) entity_id = entity_registry.async_get_entity_id("sensor", DOMAIN, unique_id)
assert entity_id is not None assert entity_id is not None
sensor = registry.async_get(entity_id) sensor = entity_registry.async_get(entity_id)
assert sensor assert sensor
assert sensor.unique_id == unique_id assert sensor.unique_id == unique_id
assert sensor.original_name == name assert sensor.original_name == name

View file

@ -8,10 +8,7 @@ from homeassistant.components.greeneye_monitor.sensor import (
) )
from homeassistant.const import STATE_UNKNOWN from homeassistant.const import STATE_UNKNOWN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import ( from homeassistant.helpers import entity_registry as er
RegistryEntryDisabler,
async_get as get_entity_registry,
)
from .common import ( from .common import (
MULTI_MONITOR_CONFIG, MULTI_MONITOR_CONFIG,
@ -27,7 +24,7 @@ from .conftest import assert_sensor_state
async def test_sensor_does_not_exist_before_monitor_connected( async def test_sensor_does_not_exist_before_monitor_connected(
hass: HomeAssistant, monitors: AsyncMock hass: HomeAssistant, entity_registry: er.EntityRegistry, monitors: AsyncMock
) -> None: ) -> None:
"""Test that a sensor does not exist before its monitor is connected.""" """Test that a sensor does not exist before its monitor is connected."""
# The sensor base class handles connecting the monitor, so we test this with a single voltage sensor for ease # The sensor base class handles connecting the monitor, so we test this with a single voltage sensor for ease
@ -35,7 +32,6 @@ async def test_sensor_does_not_exist_before_monitor_connected(
hass, SINGLE_MONITOR_CONFIG_VOLTAGE_SENSORS hass, SINGLE_MONITOR_CONFIG_VOLTAGE_SENSORS
) )
entity_registry = get_entity_registry(hass)
assert entity_registry.async_get("sensor.voltage_1") is None assert entity_registry.async_get("sensor.voltage_1") is None
@ -204,8 +200,8 @@ async def test_multi_monitor_sensors(hass: HomeAssistant, monitors: AsyncMock) -
async def disable_entity(hass: HomeAssistant, entity_id: str) -> None: async def disable_entity(hass: HomeAssistant, entity_id: str) -> None:
"""Disable the given entity.""" """Disable the given entity."""
entity_registry = get_entity_registry(hass) entity_registry = er.async_get(hass)
entity_registry.async_update_entity( entity_registry.async_update_entity(
entity_id, disabled_by=RegistryEntryDisabler.USER entity_id, disabled_by=er.RegistryEntryDisabler.USER
) )
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -24,7 +24,7 @@ from homeassistant.components.hassio.const import REQUEST_REFRESH_DELAY
from homeassistant.components.hassio.handler import HassioAPIError from homeassistant.components.hassio.handler import HassioAPIError
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import async_get 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 as dt_util from homeassistant.util import dt as dt_util
@ -773,9 +773,10 @@ async def test_migration_off_hassio(hass: HomeAssistant) -> None:
assert hass.config_entries.async_entries(DOMAIN) == [] assert hass.config_entries.async_entries(DOMAIN) == []
async def test_device_registry_calls(hass: HomeAssistant) -> None: async def test_device_registry_calls(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test device registry entries for hassio.""" """Test device registry entries for hassio."""
dev_reg = async_get(hass)
supervisor_mock_data = { supervisor_mock_data = {
"version": "1.0.0", "version": "1.0.0",
"version_latest": "1.0.0", "version_latest": "1.0.0",
@ -829,7 +830,7 @@ async def test_device_registry_calls(hass: HomeAssistant) -> None:
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done(wait_background_tasks=True) await hass.async_block_till_done(wait_background_tasks=True)
assert len(dev_reg.devices) == 6 assert len(device_registry.devices) == 6
supervisor_mock_data = { supervisor_mock_data = {
"version": "1.0.0", "version": "1.0.0",
@ -863,11 +864,11 @@ async def test_device_registry_calls(hass: HomeAssistant) -> None:
): ):
async_fire_time_changed(hass, dt_util.now() + timedelta(hours=1)) async_fire_time_changed(hass, dt_util.now() + timedelta(hours=1))
await hass.async_block_till_done(wait_background_tasks=True) await hass.async_block_till_done(wait_background_tasks=True)
assert len(dev_reg.devices) == 5 assert len(device_registry.devices) == 5
async_fire_time_changed(hass, dt_util.now() + timedelta(hours=2)) async_fire_time_changed(hass, dt_util.now() + timedelta(hours=2))
await hass.async_block_till_done(wait_background_tasks=True) await hass.async_block_till_done(wait_background_tasks=True)
assert len(dev_reg.devices) == 5 assert len(device_registry.devices) == 5
supervisor_mock_data = { supervisor_mock_data = {
"version": "1.0.0", "version": "1.0.0",
@ -921,7 +922,7 @@ async def test_device_registry_calls(hass: HomeAssistant) -> None:
): ):
async_fire_time_changed(hass, dt_util.now() + timedelta(hours=3)) async_fire_time_changed(hass, dt_util.now() + timedelta(hours=3))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(dev_reg.devices) == 5 assert len(device_registry.devices) == 5
async def test_coordinator_updates( async def test_coordinator_updates(

View file

@ -10,7 +10,7 @@ from homeassistant.components.hue.const import ATTR_HUE_EVENT
from homeassistant.components.hue.v1 import sensor_base from homeassistant.components.hue.v1 import sensor_base
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import async_get from homeassistant.helpers import entity_registry as er
from .conftest import create_mock_bridge, setup_platform from .conftest import create_mock_bridge, setup_platform
@ -314,7 +314,9 @@ async def test_sensors_with_multiple_bridges(
assert len(hass.states.async_all()) == 10 assert len(hass.states.async_all()) == 10
async def test_sensors(hass: HomeAssistant, mock_bridge_v1) -> None: async def test_sensors(
hass: HomeAssistant, entity_registry: er.EntityRegistry, mock_bridge_v1
) -> None:
"""Test the update_items function with some sensors.""" """Test the update_items function with some sensors."""
mock_bridge_v1.mock_sensor_responses.append(SENSOR_RESPONSE) mock_bridge_v1.mock_sensor_responses.append(SENSOR_RESPONSE)
await setup_platform(hass, mock_bridge_v1, ["binary_sensor", "sensor"]) await setup_platform(hass, mock_bridge_v1, ["binary_sensor", "sensor"])
@ -351,9 +353,10 @@ async def test_sensors(hass: HomeAssistant, mock_bridge_v1) -> None:
assert battery_remote_1.state == "100" assert battery_remote_1.state == "100"
assert battery_remote_1.name == "Hue dimmer switch 1 battery level" assert battery_remote_1.name == "Hue dimmer switch 1 battery level"
ent_reg = async_get(hass)
assert ( assert (
ent_reg.async_get("sensor.hue_dimmer_switch_1_battery_level").entity_category entity_registry.async_get(
"sensor.hue_dimmer_switch_1_battery_level"
).entity_category
== EntityCategory.DIAGNOSTIC == EntityCategory.DIAGNOSTIC
) )

View file

@ -16,7 +16,7 @@ from homeassistant.components.number import (
) )
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import async_get 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 tests.common import MockConfigEntry, async_fire_time_changed from tests.common import MockConfigEntry, async_fire_time_changed
@ -95,6 +95,7 @@ def mock_get_setting_values(mock_plenticore_client: ApiClient) -> list:
@pytest.mark.usefixtures("entity_registry_enabled_by_default") @pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_setup_all_entries( async def test_setup_all_entries(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_plenticore_client: ApiClient, mock_plenticore_client: ApiClient,
mock_get_setting_values: list, mock_get_setting_values: list,
@ -106,14 +107,16 @@ async def test_setup_all_entries(
assert await hass.config_entries.async_setup(mock_config_entry.entry_id) assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
ent_reg = async_get(hass) assert entity_registry.async_get("number.scb_battery_min_soc") is not None
assert ent_reg.async_get("number.scb_battery_min_soc") is not None assert (
assert ent_reg.async_get("number.scb_battery_min_home_consumption") is not None entity_registry.async_get("number.scb_battery_min_home_consumption") is not None
)
@pytest.mark.usefixtures("entity_registry_enabled_by_default") @pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_setup_no_entries( async def test_setup_no_entries(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_plenticore_client: ApiClient, mock_plenticore_client: ApiClient,
mock_get_setting_values: list, mock_get_setting_values: list,
@ -140,9 +143,8 @@ async def test_setup_no_entries(
assert await hass.config_entries.async_setup(mock_config_entry.entry_id) assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
ent_reg = async_get(hass) assert entity_registry.async_get("number.scb_battery_min_soc") is None
assert ent_reg.async_get("number.scb_battery_min_soc") is None assert entity_registry.async_get("number.scb_battery_min_home_consumption") is None
assert ent_reg.async_get("number.scb_battery_min_home_consumption") is None
@pytest.mark.usefixtures("entity_registry_enabled_by_default") @pytest.mark.usefixtures("entity_registry_enabled_by_default")

View file

@ -8,7 +8,7 @@ import requests_mock
from homeassistant.components.metoffice.const import ATTRIBUTION, DOMAIN from homeassistant.components.metoffice.const import ATTRIBUTION, DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import async_get as get_dev_reg from homeassistant.helpers import device_registry as dr
from .const import ( from .const import (
DEVICE_KEY_KINGSLYNN, DEVICE_KEY_KINGSLYNN,
@ -27,7 +27,9 @@ from tests.common import MockConfigEntry, load_fixture
@pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC)) @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
async def test_one_sensor_site_running( async def test_one_sensor_site_running(
hass: HomeAssistant, requests_mock: requests_mock.Mocker hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
requests_mock: requests_mock.Mocker,
) -> None: ) -> None:
"""Test the Met Office sensor platform.""" """Test the Met Office sensor platform."""
# all metoffice test data encapsulated in here # all metoffice test data encapsulated in here
@ -54,9 +56,10 @@ async def test_one_sensor_site_running(
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = get_dev_reg(hass) assert len(device_registry.devices) == 1
assert len(dev_reg.devices) == 1 device_wavertree = device_registry.async_get_device(
device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE) identifiers=DEVICE_KEY_WAVERTREE
)
assert device_wavertree.name == "Met Office Wavertree" assert device_wavertree.name == "Met Office Wavertree"
running_sensor_ids = hass.states.async_entity_ids("sensor") running_sensor_ids = hass.states.async_entity_ids("sensor")
@ -75,7 +78,9 @@ async def test_one_sensor_site_running(
@pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC)) @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
async def test_two_sensor_sites_running( async def test_two_sensor_sites_running(
hass: HomeAssistant, requests_mock: requests_mock.Mocker hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
requests_mock: requests_mock.Mocker,
) -> None: ) -> None:
"""Test we handle two sets of sensors running for two different sites.""" """Test we handle two sets of sensors running for two different sites."""
@ -115,11 +120,14 @@ async def test_two_sensor_sites_running(
await hass.config_entries.async_setup(entry2.entry_id) await hass.config_entries.async_setup(entry2.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = get_dev_reg(hass) assert len(device_registry.devices) == 2
assert len(dev_reg.devices) == 2 device_kingslynn = device_registry.async_get_device(
device_kingslynn = dev_reg.async_get_device(identifiers=DEVICE_KEY_KINGSLYNN) identifiers=DEVICE_KEY_KINGSLYNN
)
assert device_kingslynn.name == "Met Office King's Lynn" assert device_kingslynn.name == "Met Office King's Lynn"
device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE) device_wavertree = device_registry.async_get_device(
identifiers=DEVICE_KEY_WAVERTREE
)
assert device_wavertree.name == "Met Office Wavertree" assert device_wavertree.name == "Met Office Wavertree"
running_sensor_ids = hass.states.async_entity_ids("sensor") running_sensor_ids = hass.states.async_entity_ids("sensor")

View file

@ -19,8 +19,7 @@ from homeassistant.components.weather import (
) )
from homeassistant.const import STATE_UNAVAILABLE from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import async_get as get_dev_reg
from homeassistant.util import utcnow from homeassistant.util import utcnow
from .const import ( from .const import (
@ -73,7 +72,9 @@ async def wavertree_data(requests_mock: requests_mock.Mocker) -> dict[str, _Matc
@pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC)) @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
async def test_site_cannot_connect( async def test_site_cannot_connect(
hass: HomeAssistant, requests_mock: requests_mock.Mocker hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
requests_mock: requests_mock.Mocker,
) -> None: ) -> None:
"""Test we handle cannot connect error.""" """Test we handle cannot connect error."""
@ -89,8 +90,7 @@ async def test_site_cannot_connect(
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = get_dev_reg(hass) assert len(device_registry.devices) == 0
assert len(dev_reg.devices) == 0
assert hass.states.get("weather.met_office_wavertree_3hourly") is None assert hass.states.get("weather.met_office_wavertree_3hourly") is None
assert hass.states.get("weather.met_office_wavertree_daily") is None assert hass.states.get("weather.met_office_wavertree_daily") is None
@ -103,7 +103,6 @@ async def test_site_cannot_connect(
@pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC)) @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
async def test_site_cannot_update( async def test_site_cannot_update(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry,
requests_mock: requests_mock.Mocker, requests_mock: requests_mock.Mocker,
wavertree_data, wavertree_data,
) -> None: ) -> None:
@ -134,7 +133,7 @@ async def test_site_cannot_update(
@pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC)) @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
async def test_one_weather_site_running( async def test_one_weather_site_running(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, device_registry: dr.DeviceRegistry,
requests_mock: requests_mock.Mocker, requests_mock: requests_mock.Mocker,
wavertree_data, wavertree_data,
) -> None: ) -> None:
@ -148,9 +147,10 @@ async def test_one_weather_site_running(
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = get_dev_reg(hass) assert len(device_registry.devices) == 1
assert len(dev_reg.devices) == 1 device_wavertree = device_registry.async_get_device(
device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE) identifiers=DEVICE_KEY_WAVERTREE
)
assert device_wavertree.name == "Met Office Wavertree" assert device_wavertree.name == "Met Office Wavertree"
# Wavertree daily weather platform expected results # Wavertree daily weather platform expected results
@ -167,7 +167,7 @@ async def test_one_weather_site_running(
@pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC)) @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
async def test_two_weather_sites_running( async def test_two_weather_sites_running(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, device_registry: dr.DeviceRegistry,
requests_mock: requests_mock.Mocker, requests_mock: requests_mock.Mocker,
wavertree_data, wavertree_data,
) -> None: ) -> None:
@ -199,11 +199,14 @@ async def test_two_weather_sites_running(
await hass.config_entries.async_setup(entry2.entry_id) await hass.config_entries.async_setup(entry2.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
dev_reg = get_dev_reg(hass) assert len(device_registry.devices) == 2
assert len(dev_reg.devices) == 2 device_kingslynn = device_registry.async_get_device(
device_kingslynn = dev_reg.async_get_device(identifiers=DEVICE_KEY_KINGSLYNN) identifiers=DEVICE_KEY_KINGSLYNN
)
assert device_kingslynn.name == "Met Office King's Lynn" assert device_kingslynn.name == "Met Office King's Lynn"
device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE) device_wavertree = device_registry.async_get_device(
identifiers=DEVICE_KEY_WAVERTREE
)
assert device_wavertree.name == "Met Office Wavertree" assert device_wavertree.name == "Met Office Wavertree"
# Wavertree daily weather platform expected results # Wavertree daily weather platform expected results
@ -371,7 +374,6 @@ async def test_legacy_config_entry_is_removed(
async def test_forecast_subscription( async def test_forecast_subscription(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
no_sensor, no_sensor,

View file

@ -8,20 +8,19 @@ from homeassistant.components.octoprint.const import (
SERVICE_CONNECT, SERVICE_CONNECT,
) )
from homeassistant.const import ATTR_DEVICE_ID, CONF_PORT, CONF_PROFILE_NAME from homeassistant.const import ATTR_DEVICE_ID, CONF_PORT, CONF_PROFILE_NAME
from homeassistant.helpers.device_registry import ( from homeassistant.core import HomeAssistant
async_entries_for_config_entry, from homeassistant.helpers import device_registry as dr
async_get as async_get_dev_reg,
)
from . import init_integration from . import init_integration
async def test_connect_default(hass) -> None: async def test_connect_default(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test the connect to printer service.""" """Test the connect to printer service."""
await init_integration(hass, "sensor") await init_integration(hass, "sensor")
dev_reg = async_get_dev_reg(hass) device = dr.async_entries_for_config_entry(device_registry, "uuid")[0]
device = async_entries_for_config_entry(dev_reg, "uuid")[0]
# Test pausing the printer when it is printing # Test pausing the printer when it is printing
with patch("pyoctoprintapi.OctoprintClient.connect") as connect_command: with patch("pyoctoprintapi.OctoprintClient.connect") as connect_command:
@ -40,12 +39,13 @@ async def test_connect_default(hass) -> None:
) )
async def test_connect_all_arguments(hass) -> None: async def test_connect_all_arguments(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test the connect to printer service.""" """Test the connect to printer service."""
await init_integration(hass, "sensor") await init_integration(hass, "sensor")
dev_reg = async_get_dev_reg(hass) device = dr.async_entries_for_config_entry(device_registry, "uuid")[0]
device = async_entries_for_config_entry(dev_reg, "uuid")[0]
# Test pausing the printer when it is printing # Test pausing the printer when it is printing
with patch("pyoctoprintapi.OctoprintClient.connect") as connect_command: with patch("pyoctoprintapi.OctoprintClient.connect") as connect_command:

View file

@ -5,8 +5,8 @@ from unittest.mock import MagicMock
from homeassistant.components.plugwise.const import DOMAIN from homeassistant.components.plugwise.const import DOMAIN
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
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.entity_registry import async_get
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -49,13 +49,13 @@ async def test_adam_climate_sensor_entity_2(
async def test_unique_id_migration_humidity( async def test_unique_id_migration_humidity(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_smile_adam_4: MagicMock, mock_smile_adam_4: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test unique ID migration of -relative_humidity to -humidity.""" """Test unique ID migration of -relative_humidity to -humidity."""
mock_config_entry.add_to_hass(hass) mock_config_entry.add_to_hass(hass)
entity_registry = async_get(hass)
# Entry to migrate # Entry to migrate
entity_registry.async_get_or_create( entity_registry.async_get_or_create(
Platform.SENSOR, Platform.SENSOR,
@ -136,7 +136,10 @@ async def test_p1_dsmr_sensor_entities(
async def test_p1_3ph_dsmr_sensor_entities( async def test_p1_3ph_dsmr_sensor_entities(
hass: HomeAssistant, mock_smile_p1_2: MagicMock, init_integration: MockConfigEntry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_smile_p1_2: MagicMock,
init_integration: MockConfigEntry,
) -> None: ) -> None:
"""Test creation of power related sensor entities.""" """Test creation of power related sensor entities."""
state = hass.states.get("sensor.p1_electricity_phase_one_consumed") state = hass.states.get("sensor.p1_electricity_phase_one_consumed")
@ -155,7 +158,6 @@ async def test_p1_3ph_dsmr_sensor_entities(
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert not state assert not state
entity_registry = async_get(hass)
entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None) entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -11,7 +11,7 @@ from homeassistant.components.samsungtv import DOMAIN, device_trigger
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.device_registry import async_get as get_dev_reg from homeassistant.helpers import device_registry as dr
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from . import setup_samsungtv_entry from . import setup_samsungtv_entry
@ -21,12 +21,13 @@ from tests.common import MockConfigEntry, async_get_device_automations
@pytest.mark.usefixtures("remoteencws", "rest_api") @pytest.mark.usefixtures("remoteencws", "rest_api")
async def test_get_triggers(hass: HomeAssistant) -> None: async def test_get_triggers(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test we get the expected triggers.""" """Test we get the expected triggers."""
await setup_samsungtv_entry(hass, MOCK_ENTRYDATA_ENCRYPTED_WS) await setup_samsungtv_entry(hass, MOCK_ENTRYDATA_ENCRYPTED_WS)
device_reg = get_dev_reg(hass) device = device_registry.async_get_device(identifiers={(DOMAIN, "any")})
device = device_reg.async_get_device(identifiers={(DOMAIN, "any")})
turn_on_trigger = { turn_on_trigger = {
"platform": "device", "platform": "device",
@ -44,14 +45,13 @@ async def test_get_triggers(hass: HomeAssistant) -> None:
@pytest.mark.usefixtures("remoteencws", "rest_api") @pytest.mark.usefixtures("remoteencws", "rest_api")
async def test_if_fires_on_turn_on_request( async def test_if_fires_on_turn_on_request(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, device_registry: dr.DeviceRegistry, calls: list[ServiceCall]
) -> None: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
await setup_samsungtv_entry(hass, MOCK_ENTRYDATA_ENCRYPTED_WS) await setup_samsungtv_entry(hass, MOCK_ENTRYDATA_ENCRYPTED_WS)
entity_id = "media_player.fake" entity_id = "media_player.fake"
device_reg = get_dev_reg(hass) device = device_registry.async_get_device(identifiers={(DOMAIN, "any")})
device = device_reg.async_get_device(identifiers={(DOMAIN, "any")})
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -103,7 +103,9 @@ async def test_if_fires_on_turn_on_request(
@pytest.mark.usefixtures("remoteencws", "rest_api") @pytest.mark.usefixtures("remoteencws", "rest_api")
async def test_failure_scenarios(hass: HomeAssistant) -> None: async def test_failure_scenarios(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test failure scenarios.""" """Test failure scenarios."""
await setup_samsungtv_entry(hass, MOCK_ENTRYDATA_ENCRYPTED_WS) await setup_samsungtv_entry(hass, MOCK_ENTRYDATA_ENCRYPTED_WS)
@ -127,9 +129,8 @@ async def test_failure_scenarios(hass: HomeAssistant) -> None:
entry = MockConfigEntry(domain="fake", state=ConfigEntryState.LOADED, data={}) entry = MockConfigEntry(domain="fake", state=ConfigEntryState.LOADED, data={})
entry.add_to_hass(hass) entry.add_to_hass(hass)
device_reg = get_dev_reg(hass)
device = device_reg.async_get_or_create( device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id, identifiers={("fake", "fake")} config_entry_id=entry.entry_id, identifiers={("fake", "fake")}
) )

View file

@ -6,7 +6,7 @@ from smhi.smhi_lib import APIURL_TEMPLATE
from homeassistant.components.smhi.const import DOMAIN from homeassistant.components.smhi.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import async_get from homeassistant.helpers import entity_registry as er
from . import ENTITY_ID, TEST_CONFIG, TEST_CONFIG_MIGRATE from . import ENTITY_ID, TEST_CONFIG, TEST_CONFIG_MIGRATE
@ -57,7 +57,10 @@ async def test_remove_entry(
async def test_migrate_entry( async def test_migrate_entry(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str hass: HomeAssistant,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
api_response: str,
) -> None: ) -> None:
"""Test migrate entry data.""" """Test migrate entry data."""
uri = APIURL_TEMPLATE.format( uri = APIURL_TEMPLATE.format(
@ -68,8 +71,7 @@ async def test_migrate_entry(
entry.add_to_hass(hass) entry.add_to_hass(hass)
assert entry.version == 1 assert entry.version == 1
entity_reg = async_get(hass) entity = entity_registry.async_get_or_create(
entity = entity_reg.async_get_or_create(
domain="weather", domain="weather",
config_entry=entry, config_entry=entry,
original_name="Weather", original_name="Weather",
@ -87,7 +89,7 @@ async def test_migrate_entry(
assert entry.version == 2 assert entry.version == 2
assert entry.unique_id == "17.84197-17.84197" assert entry.unique_id == "17.84197-17.84197"
entity_get = entity_reg.async_get(entity.entity_id) entity_get = entity_registry.async_get(entity.entity_id)
assert entity_get.unique_id == "17.84197, 17.84197" assert entity_get.unique_id == "17.84197, 17.84197"

View file

@ -15,7 +15,7 @@ from homeassistant.const import (
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers.entity_registry import async_get from homeassistant.helpers import entity_registry as er
from tests.common import assert_setup_component from tests.common import assert_setup_component
@ -62,7 +62,10 @@ async def test_missing_required_keys(hass: HomeAssistant) -> None:
async def test_all_optional_config( async def test_all_optional_config(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls: list[ServiceCall] hass: HomeAssistant,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
calls: list[ServiceCall],
) -> None: ) -> None:
"""Test: including all optional templates is ok.""" """Test: including all optional templates is ok."""
with assert_setup_component(1, "template"): with assert_setup_component(1, "template"):
@ -124,8 +127,7 @@ async def test_all_optional_config(
_TEST_OPTIONS_BUTTON, _TEST_OPTIONS_BUTTON,
) )
er = async_get(hass) assert entity_registry.async_get_entity_id("button", "template", "test-test")
assert er.async_get_entity_id("button", "template", "test-test")
async def test_name_template(hass: HomeAssistant) -> None: async def test_name_template(hass: HomeAssistant) -> None:

View file

@ -17,7 +17,7 @@ from homeassistant.components.input_text import (
) )
from homeassistant.const import ATTR_ENTITY_PICTURE, CONF_ENTITY_ID, STATE_UNKNOWN from homeassistant.const import ATTR_ENTITY_PICTURE, CONF_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import async_get 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 tests.common import assert_setup_component from tests.common import assert_setup_component
@ -211,7 +211,9 @@ async def test_missing_required_keys(hass: HomeAssistant) -> None:
assert hass.states.async_all("image") == [] assert hass.states.async_all("image") == []
async def test_unique_id(hass: HomeAssistant) -> None: async def test_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test unique_id configuration.""" """Test unique_id configuration."""
with assert_setup_component(1, "template"): with assert_setup_component(1, "template"):
assert await setup.async_setup_component( assert await setup.async_setup_component(
@ -232,8 +234,7 @@ async def test_unique_id(hass: HomeAssistant) -> None:
await hass.async_start() await hass.async_start()
await hass.async_block_till_done() await hass.async_block_till_done()
ent_reg = async_get(hass) entry = entity_registry.async_get(_TEST_IMAGE)
entry = ent_reg.async_get(_TEST_IMAGE)
assert entry assert entry
assert entry.unique_id == "b-a" assert entry.unique_id == "b-a"

View file

@ -16,7 +16,7 @@ from homeassistant.components.number import (
) )
from homeassistant.const import ATTR_ICON, CONF_ENTITY_ID, STATE_UNKNOWN from homeassistant.const import ATTR_ICON, CONF_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import Context, HomeAssistant, ServiceCall from homeassistant.core import Context, HomeAssistant, ServiceCall
from homeassistant.helpers.entity_registry import async_get from homeassistant.helpers import entity_registry as er
from tests.common import assert_setup_component, async_capture_events from tests.common import assert_setup_component, async_capture_events
@ -128,7 +128,7 @@ async def test_all_optional_config(hass: HomeAssistant) -> None:
async def test_templates_with_entities( async def test_templates_with_entities(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, entity_registry: er.EntityRegistry, calls: list[ServiceCall]
) -> None: ) -> None:
"""Test templates with values from other entities.""" """Test templates with values from other entities."""
with assert_setup_component(4, "input_number"): with assert_setup_component(4, "input_number"):
@ -208,8 +208,7 @@ async def test_templates_with_entities(
await hass.async_start() await hass.async_start()
await hass.async_block_till_done() await hass.async_block_till_done()
ent_reg = async_get(hass) entry = entity_registry.async_get(_TEST_NUMBER)
entry = ent_reg.async_get(_TEST_NUMBER)
assert entry assert entry
assert entry.unique_id == "b-a" assert entry.unique_id == "b-a"

View file

@ -16,7 +16,7 @@ from homeassistant.components.select import (
) )
from homeassistant.const import ATTR_ICON, CONF_ENTITY_ID, STATE_UNKNOWN from homeassistant.const import ATTR_ICON, CONF_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import Context, HomeAssistant, ServiceCall from homeassistant.core import Context, HomeAssistant, ServiceCall
from homeassistant.helpers.entity_registry import async_get from homeassistant.helpers import entity_registry as er
from tests.common import assert_setup_component, async_capture_events from tests.common import assert_setup_component, async_capture_events
@ -133,7 +133,7 @@ async def test_missing_required_keys(hass: HomeAssistant) -> None:
async def test_templates_with_entities( async def test_templates_with_entities(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, entity_registry: er.EntityRegistry, calls: list[ServiceCall]
) -> None: ) -> None:
"""Test templates with values from other entities.""" """Test templates with values from other entities."""
with assert_setup_component(1, "input_select"): with assert_setup_component(1, "input_select"):
@ -189,8 +189,7 @@ async def test_templates_with_entities(
await hass.async_start() await hass.async_start()
await hass.async_block_till_done() await hass.async_block_till_done()
ent_reg = async_get(hass) entry = entity_registry.async_get(_TEST_SELECT)
entry = ent_reg.async_get(_TEST_SELECT)
assert entry assert entry
assert entry.unique_id == "b-a" assert entry.unique_id == "b-a"

View file

@ -24,7 +24,7 @@ from homeassistant.components.tomorrowio.sensor import TomorrowioSensorEntityDes
from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY, SOURCE_USER from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY, SOURCE_USER
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME
from homeassistant.core import HomeAssistant, State, callback from homeassistant.core import HomeAssistant, State, callback
from homeassistant.helpers.entity_registry import async_get 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 homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
@ -103,7 +103,7 @@ V4_FIELDS = [
@callback @callback
def _enable_entity(hass: HomeAssistant, entity_name: str) -> None: def _enable_entity(hass: HomeAssistant, entity_name: str) -> None:
"""Enable disabled entity.""" """Enable disabled entity."""
ent_reg = async_get(hass) ent_reg = er.async_get(hass)
entry = ent_reg.async_get(entity_name) entry = ent_reg.async_get(entity_name)
updated_entry = ent_reg.async_update_entity(entry.entity_id, disabled_by=None) updated_entry = ent_reg.async_update_entity(entry.entity_id, disabled_by=None)
assert updated_entry != entry assert updated_entry != entry

View file

@ -43,7 +43,6 @@ from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY, SOURCE_USER
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_FRIENDLY_NAME, CONF_NAME from homeassistant.const import ATTR_ATTRIBUTION, ATTR_FRIENDLY_NAME, CONF_NAME
from homeassistant.core import HomeAssistant, State, callback from homeassistant.core import HomeAssistant, State, callback
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_registry import async_get
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from .const import API_V4_ENTRY_DATA from .const import API_V4_ENTRY_DATA
@ -55,7 +54,7 @@ from tests.typing import WebSocketGenerator
@callback @callback
def _enable_entity(hass: HomeAssistant, entity_name: str) -> None: def _enable_entity(hass: HomeAssistant, entity_name: str) -> None:
"""Enable disabled entity.""" """Enable disabled entity."""
ent_reg = async_get(hass) ent_reg = er.async_get(hass)
entry = ent_reg.async_get(entity_name) entry = ent_reg.async_get(entity_name)
updated_entry = ent_reg.async_update_entity(entry.entity_id, disabled_by=None) updated_entry = ent_reg.async_update_entity(entry.entity_id, disabled_by=None)
assert updated_entry != entry assert updated_entry != entry

View file

@ -18,7 +18,7 @@ from homeassistant.components.camera import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry from homeassistant.helpers import entity_registry as er
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
@ -111,7 +111,9 @@ def camera_v313_fixture():
yield camera yield camera
async def test_setup_full_config(hass: HomeAssistant, mock_remote, camera_info) -> None: async def test_setup_full_config(
hass: HomeAssistant, entity_registry: er.EntityRegistry, mock_remote, camera_info
) -> None:
"""Test the setup with full configuration.""" """Test the setup with full configuration."""
config = { config = {
"platform": "uvc", "platform": "uvc",
@ -153,7 +155,6 @@ async def test_setup_full_config(hass: HomeAssistant, mock_remote, camera_info)
assert state assert state
assert state.name == "Back" assert state.name == "Back"
entity_registry = async_get_entity_registry(hass)
entity_entry = entity_registry.async_get("camera.front") entity_entry = entity_registry.async_get("camera.front")
assert entity_entry.unique_id == "id1" assert entity_entry.unique_id == "id1"
@ -163,7 +164,9 @@ async def test_setup_full_config(hass: HomeAssistant, mock_remote, camera_info)
assert entity_entry.unique_id == "id2" assert entity_entry.unique_id == "id2"
async def test_setup_partial_config(hass: HomeAssistant, mock_remote) -> None: async def test_setup_partial_config(
hass: HomeAssistant, entity_registry: er.EntityRegistry, mock_remote
) -> None:
"""Test the setup with partial configuration.""" """Test the setup with partial configuration."""
config = {"platform": "uvc", "nvr": "foo", "key": "secret"} config = {"platform": "uvc", "nvr": "foo", "key": "secret"}
@ -187,7 +190,6 @@ async def test_setup_partial_config(hass: HomeAssistant, mock_remote) -> None:
assert state assert state
assert state.name == "Back" assert state.name == "Back"
entity_registry = async_get_entity_registry(hass)
entity_entry = entity_registry.async_get("camera.front") entity_entry = entity_registry.async_get("camera.front")
assert entity_entry.unique_id == "id1" assert entity_entry.unique_id == "id1"
@ -197,7 +199,9 @@ async def test_setup_partial_config(hass: HomeAssistant, mock_remote) -> None:
assert entity_entry.unique_id == "id2" assert entity_entry.unique_id == "id2"
async def test_setup_partial_config_v31x(hass: HomeAssistant, mock_remote) -> None: async def test_setup_partial_config_v31x(
hass: HomeAssistant, entity_registry: er.EntityRegistry, mock_remote
) -> None:
"""Test the setup with a v3.1.x server.""" """Test the setup with a v3.1.x server."""
config = {"platform": "uvc", "nvr": "foo", "key": "secret"} config = {"platform": "uvc", "nvr": "foo", "key": "secret"}
mock_remote.return_value.server_version = (3, 1, 3) mock_remote.return_value.server_version = (3, 1, 3)
@ -222,7 +226,6 @@ async def test_setup_partial_config_v31x(hass: HomeAssistant, mock_remote) -> No
assert state assert state
assert state.name == "Back" assert state.name == "Back"
entity_registry = async_get_entity_registry(hass)
entity_entry = entity_registry.async_get("camera.front") entity_entry = entity_registry.async_get("camera.front")
assert entity_entry.unique_id == "one" assert entity_entry.unique_id == "one"

View file

@ -8,10 +8,7 @@ from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.components.xiaomi_ble.const import CONF_SUBTYPE, DOMAIN from homeassistant.components.xiaomi_ble.const import CONF_SUBTYPE, DOMAIN
from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM, CONF_TYPE from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM, CONF_TYPE
from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers.device_registry import ( from homeassistant.helpers import device_registry as dr
CONNECTION_NETWORK_MAC,
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
@ -176,7 +173,9 @@ async def test_event_dimmer_rotate(hass: HomeAssistant) -> None:
await hass.async_block_till_done() await hass.async_block_till_done()
async def test_get_triggers_button(hass: HomeAssistant) -> None: async def test_get_triggers_button(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test that we get the expected triggers from a Xiaomi BLE button sensor.""" """Test that we get the expected triggers from a Xiaomi BLE button sensor."""
mac = "54:EF:44:E3:9C:BC" mac = "54:EF:44:E3:9C:BC"
data = {"bindkey": "5b51a7c91cde6707c9ef18dfda143a58"} data = {"bindkey": "5b51a7c91cde6707c9ef18dfda143a58"}
@ -196,8 +195,7 @@ async def test_get_triggers_button(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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
assert device assert device
expected_trigger = { expected_trigger = {
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
@ -216,7 +214,9 @@ async def test_get_triggers_button(hass: HomeAssistant) -> None:
await hass.async_block_till_done() await hass.async_block_till_done()
async def test_get_triggers_double_button(hass: HomeAssistant) -> None: async def test_get_triggers_double_button(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test that we get the expected triggers from a Xiaomi BLE switch with 2 buttons.""" """Test that we get the expected triggers from a Xiaomi BLE switch with 2 buttons."""
mac = "DC:ED:83:87:12:73" mac = "DC:ED:83:87:12:73"
data = {"bindkey": "b93eb3787eabda352edd94b667f5d5a9"} data = {"bindkey": "b93eb3787eabda352edd94b667f5d5a9"}
@ -236,8 +236,7 @@ async def test_get_triggers_double_button(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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
assert device assert device
expected_trigger = { expected_trigger = {
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
@ -256,7 +255,9 @@ async def test_get_triggers_double_button(hass: HomeAssistant) -> None:
await hass.async_block_till_done() await hass.async_block_till_done()
async def test_get_triggers_lock(hass: HomeAssistant) -> None: async def test_get_triggers_lock(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test that we get the expected triggers from a Xiaomi BLE lock with fingerprint scanner.""" """Test that we get the expected triggers from a Xiaomi BLE lock with fingerprint scanner."""
mac = "98:0C:33:A3:04:3D" mac = "98:0C:33:A3:04:3D"
data = {"bindkey": "54d84797cb77f9538b224b305c877d1e"} data = {"bindkey": "54d84797cb77f9538b224b305c877d1e"}
@ -277,8 +278,7 @@ async def test_get_triggers_lock(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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
assert device assert device
expected_trigger = { expected_trigger = {
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
@ -297,7 +297,9 @@ async def test_get_triggers_lock(hass: HomeAssistant) -> None:
await hass.async_block_till_done() await hass.async_block_till_done()
async def test_get_triggers_motion(hass: HomeAssistant) -> None: async def test_get_triggers_motion(
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)
@ -313,8 +315,7 @@ async def test_get_triggers_motion(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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
assert device assert device
expected_trigger = { expected_trigger = {
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
@ -333,7 +334,9 @@ async def test_get_triggers_motion(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 device that does not emit events.""" """Test that we don't get triggers for an device that does not emit events."""
mac = "C4:7C:8D:6A:3E:7A" mac = "C4:7C:8D:6A:3E:7A"
entry = await _async_setup_xiaomi_device(hass, mac) entry = await _async_setup_xiaomi_device(hass, mac)
@ -349,8 +352,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) == 0 assert len(events) == 0
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")},
) )
@ -364,7 +366,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)
@ -378,11 +382,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={(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(
@ -395,7 +397,7 @@ async def test_get_triggers_for_invalid_device_id(hass: HomeAssistant) -> None:
async def test_if_fires_on_button_press( async def test_if_fires_on_button_press(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, device_registry: dr.DeviceRegistry, calls: list[ServiceCall]
) -> None: ) -> None:
"""Test for button press event trigger firing.""" """Test for button press event trigger firing."""
mac = "54:EF:44:E3:9C:BC" mac = "54:EF:44:E3:9C:BC"
@ -414,8 +416,7 @@ async def test_if_fires_on_button_press(
# wait for the device being created # wait for the device being created
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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
device_id = device.id device_id = device.id
assert await async_setup_component( assert await async_setup_component(
@ -457,7 +458,7 @@ async def test_if_fires_on_button_press(
async def test_if_fires_on_double_button_long_press( async def test_if_fires_on_double_button_long_press(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, device_registry: dr.DeviceRegistry, calls: list[ServiceCall]
) -> None: ) -> None:
"""Test for button press event trigger firing.""" """Test for button press event trigger firing."""
mac = "DC:ED:83:87:12:73" mac = "DC:ED:83:87:12:73"
@ -476,8 +477,7 @@ async def test_if_fires_on_double_button_long_press(
# wait for the device being created # wait for the device being created
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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
device_id = device.id device_id = device.id
assert await async_setup_component( assert await async_setup_component(
@ -519,7 +519,7 @@ async def test_if_fires_on_double_button_long_press(
async def test_if_fires_on_motion_detected( async def test_if_fires_on_motion_detected(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, device_registry: dr.DeviceRegistry, calls: list[ServiceCall]
) -> None: ) -> 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"
@ -534,8 +534,7 @@ async def test_if_fires_on_motion_detected(
# wait for the device being created # wait for the device being created
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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
device_id = device.id device_id = device.id
assert await async_setup_component( assert await async_setup_component(
@ -575,6 +574,7 @@ async def test_if_fires_on_motion_detected(
async def test_automation_with_invalid_trigger_type( async def test_automation_with_invalid_trigger_type(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test for automation with invalid trigger type.""" """Test for automation with invalid trigger type."""
@ -590,8 +590,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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
device_id = device.id device_id = device.id
assert await async_setup_component( assert await async_setup_component(
@ -624,6 +623,7 @@ 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, hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test for automation with invalid trigger event property.""" """Test for automation with invalid trigger event property."""
@ -639,8 +639,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(identifiers={get_device_id(mac)})
device = dev_reg.async_get_device(identifiers={get_device_id(mac)})
device_id = device.id device_id = device.id
assert await async_setup_component( assert await async_setup_component(
@ -675,7 +674,7 @@ async def test_automation_with_invalid_trigger_event_property(
async def test_triggers_for_invalid__model( async def test_triggers_for_invalid__model(
hass: HomeAssistant, calls: list[ServiceCall] hass: HomeAssistant, device_registry: dr.DeviceRegistry, calls: list[ServiceCall]
) -> None: ) -> 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"
@ -691,8 +690,7 @@ async def test_triggers_for_invalid__model(
await hass.async_block_till_done() await hass.async_block_till_done()
# modify model to invalid model # modify model to invalid model
dev_reg = async_get_dev_reg(hass) invalid_model = device_registry.async_get_or_create(
invalid_model = dev_reg.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

@ -12,7 +12,7 @@ from homeassistant.components.zha.core.helpers import get_zha_gateway
from homeassistant.components.zha.diagnostics import KEYS_TO_REDACT from homeassistant.components.zha.diagnostics import KEYS_TO_REDACT
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import async_get from homeassistant.helpers import device_registry as dr
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,6 +104,7 @@ async def test_diagnostics_for_config_entry(
async def test_diagnostics_for_device( async def test_diagnostics_for_device(
hass: HomeAssistant, hass: HomeAssistant,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
device_registry: dr.DeviceRegistry,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
zha_device_joined, zha_device_joined,
zigpy_device, zigpy_device,
@ -126,8 +127,9 @@ async def test_diagnostics_for_device(
} }
) )
dev_reg = async_get(hass) device = device_registry.async_get_device(
device = dev_reg.async_get_device(identifiers={("zha", str(zha_device.ieee))}) identifiers={("zha", str(zha_device.ieee))}
)
assert device assert device
diagnostics_data = await get_diagnostics_for_device( diagnostics_data = await get_diagnostics_for_device(
hass, hass_client, config_entry, device hass, hass_client, config_entry, device