Adjust entity registry access in tests (2) (#88960)

This commit is contained in:
epenet 2023-03-01 16:23:36 +01:00 committed by GitHub
parent 09f1c2318d
commit d65dff3f9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 291 additions and 213 deletions

View File

@ -1,5 +1,6 @@
"""Test Google Smart Home."""
import asyncio
from types import SimpleNamespace
from unittest.mock import ANY, call, patch
import pytest
@ -23,30 +24,32 @@ from homeassistant.components.google_assistant import (
from homeassistant.config import async_process_ha_core_config
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, UnitOfTemperature, __version__
from homeassistant.core import EVENT_CALL_SERVICE, HomeAssistant, State
from homeassistant.helpers import device_registry, entity_platform
from homeassistant.helpers import (
area_registry as ar,
device_registry as dr,
entity_platform,
entity_registry as er,
)
from homeassistant.setup import async_setup_component
from . import BASIC_CONFIG, MockConfig
from tests.common import (
async_capture_events,
mock_area_registry,
mock_device_registry,
mock_registry,
)
from tests.common import async_capture_events
REQ_ID = "ff36a3cc-ec34-11e6-b1a0-64510650abcf"
@pytest.fixture
def registries(hass):
def registries(
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
area_registry: ar.AreaRegistry,
) -> SimpleNamespace:
"""Registry mock setup."""
from types import SimpleNamespace
ret = SimpleNamespace()
ret.entity = mock_registry(hass)
ret.device = mock_device_registry(hass)
ret.area = mock_area_registry(hass)
ret.entity = entity_registry
ret.device = device_registry
ret.area = area_registry
return ret
@ -238,7 +241,7 @@ async def test_sync_in_area(area_on_device, hass: HomeAssistant, registries) ->
manufacturer="Someone",
model="Some model",
sw_version="Some Version",
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
registries.device.async_update_device(
device.id, area_id=area.id if area_on_device else None

View File

@ -10,7 +10,7 @@ from homeassistant.const import (
CONF_UNIQUE_ID,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry
from homeassistant.helpers import device_registry as dr
from homeassistant.setup import async_setup_component
from .conftest import setup_platform
@ -35,7 +35,9 @@ SAMPLE_V2_EVENT = {
}
async def test_humanify_hue_events(hass: HomeAssistant, mock_bridge_v2) -> None:
async def test_humanify_hue_events(
hass: HomeAssistant, mock_bridge_v2, device_registry: dr.DeviceRegistry
) -> None:
"""Test hue events when the devices are present in the registry."""
await setup_platform(hass, mock_bridge_v2, "sensor")
hass.config.components.add("recorder")
@ -43,11 +45,10 @@ async def test_humanify_hue_events(hass: HomeAssistant, mock_bridge_v2) -> None:
await hass.async_block_till_done()
entry: ConfigEntry = hass.config_entries.async_entries(DOMAIN)[0]
dev_reg = device_registry.async_get(hass)
v1_device = dev_reg.async_get_or_create(
v1_device = device_registry.async_get_or_create(
identifiers={(DOMAIN, "v1")}, name="Remote 1", config_entry_id=entry.entry_id
)
v2_device = dev_reg.async_get_or_create(
v2_device = device_registry.async_get_or_create(
identifiers={(DOMAIN, "v2")}, name="Remote 2", config_entry_id=entry.entry_id
)

View File

@ -3,7 +3,7 @@ from homeassistant.components.climate import PRESET_ECO, PRESET_SLEEP, HVACMode
from homeassistant.components.knx.schema import ClimateSchema
from homeassistant.const import CONF_NAME, STATE_IDLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from .conftest import KNXTestKit
@ -99,7 +99,9 @@ async def test_climate_hvac_mode(hass: HomeAssistant, knx: KNXTestKit) -> None:
await knx.assert_write("1/2/6", (0x01,))
async def test_climate_preset_mode(hass: HomeAssistant, knx: KNXTestKit) -> None:
async def test_climate_preset_mode(
hass: HomeAssistant, knx: KNXTestKit, entity_registry: er.EntityRegistry
) -> None:
"""Test KNX climate preset mode."""
events = async_capture_events(hass, "state_changed")
await knx.setup_integration(
@ -155,8 +157,7 @@ async def test_climate_preset_mode(hass: HomeAssistant, knx: KNXTestKit) -> None
assert len(knx.xknx.devices[0].device_updated_cbs) == 2
assert len(knx.xknx.devices[1].device_updated_cbs) == 2
# test removing also removes hooks
er = entity_registry.async_get(hass)
er.async_remove("climate.test")
entity_registry.async_remove("climate.test")
await hass.async_block_till_done()
# If we remove the entity the underlying devices should disappear too

View File

@ -3,13 +3,16 @@ from pykoplenti import SettingsData
from homeassistant.components.kostal_plenticore.helper import Plenticore
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry
async def test_select_battery_charging_usage_available(
hass: HomeAssistant, mock_plenticore: Plenticore, mock_config_entry: MockConfigEntry
hass: HomeAssistant,
mock_plenticore: Plenticore,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test that the battery charging usage select entity is added if the settings are available."""
@ -25,13 +28,14 @@ async def test_select_battery_charging_usage_available(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert entity_registry.async_get(hass).async_is_registered(
"select.battery_charging_usage_mode"
)
assert entity_registry.async_is_registered("select.battery_charging_usage_mode")
async def test_select_battery_charging_usage_not_available(
hass: HomeAssistant, mock_plenticore: Plenticore, mock_config_entry: MockConfigEntry
hass: HomeAssistant,
mock_plenticore: Plenticore,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test that the battery charging usage select entity is not added if the settings are unavailable."""
@ -40,6 +44,4 @@ async def test_select_battery_charging_usage_not_available(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert not entity_registry.async_get(hass).async_is_registered(
"select.battery_charging_usage_mode"
)
assert not entity_registry.async_is_registered("select.battery_charging_usage_mode")

View File

@ -24,7 +24,7 @@ from homeassistant.const import (
UnitOfVolume,
)
from homeassistant.core import CoreState, HomeAssistant, State
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -43,7 +43,9 @@ class MockHeatMeterResponse:
@patch("homeassistant.components.landisgyr_heat_meter.ultraheat_api.HeatMeterService")
async def test_create_sensors(mock_heat_meter, hass: HomeAssistant) -> None:
async def test_create_sensors(
mock_heat_meter, hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test sensor."""
entry_data = {
"device": "/dev/USB0",
@ -77,7 +79,6 @@ async def test_create_sensors(mock_heat_meter, hass: HomeAssistant) -> None:
# check if 26 attributes have been created
assert len(hass.states.async_all()) == 27
entity_reg = entity_registry.async_get(hass)
state = hass.states.get("sensor.heat_meter_heat_usage")
assert state
@ -96,14 +97,16 @@ async def test_create_sensors(mock_heat_meter, hass: HomeAssistant) -> None:
assert state
assert state.state == "devicenr_789"
assert state.attributes.get(ATTR_STATE_CLASS) is None
entity_registry_entry = entity_reg.async_get("sensor.heat_meter_device_number")
entity_registry_entry = entity_registry.async_get("sensor.heat_meter_device_number")
assert entity_registry_entry.entity_category == EntityCategory.DIAGNOSTIC
state = hass.states.get("sensor.heat_meter_meter_date_time")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:clock-outline"
assert state.attributes.get(ATTR_STATE_CLASS) is None
entity_registry_entry = entity_reg.async_get("sensor.heat_meter_meter_date_time")
entity_registry_entry = entity_registry.async_get(
"sensor.heat_meter_meter_date_time"
)
assert entity_registry_entry.entity_category == EntityCategory.DIAGNOSTIC

View File

@ -9,22 +9,23 @@ from homeassistant.components.select import (
)
from homeassistant.const import ATTR_ENTITY_ID, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from .conftest import setup_integration
SELECT_ENTITY_ID = "select.test_clean_cycle_wait_time_minutes"
async def test_wait_time_select(hass: HomeAssistant, mock_account) -> None:
async def test_wait_time_select(
hass: HomeAssistant, mock_account, entity_registry: er.EntityRegistry
) -> None:
"""Tests the wait time select entity."""
await setup_integration(hass, mock_account, PLATFORM_DOMAIN)
select = hass.states.get(SELECT_ENTITY_ID)
assert select
ent_reg = entity_registry.async_get(hass)
entity_entry = ent_reg.async_get(SELECT_ENTITY_ID)
entity_entry = entity_registry.async_get(SELECT_ENTITY_ID)
assert entity_entry
assert entity_entry.entity_category is EntityCategory.CONFIG

View File

@ -11,7 +11,7 @@ from homeassistant.components.switch import (
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from .conftest import setup_integration
@ -19,7 +19,9 @@ NIGHT_LIGHT_MODE_ENTITY_ID = "switch.test_night_light_mode"
PANEL_LOCKOUT_ENTITY_ID = "switch.test_panel_lockout"
async def test_switch(hass: HomeAssistant, mock_account: MagicMock) -> None:
async def test_switch(
hass: HomeAssistant, mock_account: MagicMock, entity_registry: er.EntityRegistry
) -> None:
"""Tests the switch entity was set up."""
await setup_integration(hass, mock_account, PLATFORM_DOMAIN)
@ -27,8 +29,7 @@ async def test_switch(hass: HomeAssistant, mock_account: MagicMock) -> None:
assert state
assert state.state == STATE_ON
ent_reg = entity_registry.async_get(hass)
entity_entry = ent_reg.async_get(NIGHT_LIGHT_MODE_ENTITY_ID)
entity_entry = entity_registry.async_get(NIGHT_LIGHT_MODE_ENTITY_ID)
assert entity_entry
assert entity_entry.entity_category is EntityCategory.CONFIG

View File

@ -41,7 +41,7 @@ from homeassistant.const import (
)
import homeassistant.core as ha
from homeassistant.core import Event, HomeAssistant
from homeassistant.helpers import device_registry, entity_registry as er
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entityfilter import CONF_ENTITY_GLOBS
from homeassistant.helpers.json import JSONEncoder
from homeassistant.setup import async_setup_component
@ -2586,7 +2586,10 @@ async def test_get_events_invalid_filters(
async def test_get_events_with_device_ids(
recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator
recorder_mock: Recorder,
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test logbook get_events for device ids."""
now = dt_util.utcnow()
@ -2599,10 +2602,9 @@ async def test_get_events_with_device_ids(
entry = MockConfigEntry(domain="test", data={"first": True}, options=None)
entry.add_to_hass(hass)
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
identifiers={("bridgeid", "0123")},
sw_version="sw-version",
name="device name",

View File

@ -31,7 +31,7 @@ from homeassistant.const import (
STATE_ON,
)
from homeassistant.core import Event, HomeAssistant, State
from homeassistant.helpers import device_registry, entity_registry
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entityfilter import CONF_ENTITY_GLOBS
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
@ -86,12 +86,13 @@ async def _async_mock_logbook_platform(hass: HomeAssistant) -> None:
await logbook._process_logbook_platform(hass, "test", MockLogbookPlatform)
async def _async_mock_entity_with_logbook_platform(hass):
async def _async_mock_entity_with_logbook_platform(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> er.RegistryEntry:
"""Mock an integration that provides an entity that are described by the logbook."""
entry = MockConfigEntry(domain="test", data={"first": True}, options=None)
entry.add_to_hass(hass)
ent_reg = entity_registry.async_get(hass)
entry = ent_reg.async_get_or_create(
entry = entity_registry.async_get_or_create(
platform="test",
domain="sensor",
config_entry=entry,
@ -102,14 +103,15 @@ async def _async_mock_entity_with_logbook_platform(hass):
return entry
async def _async_mock_devices_with_logbook_platform(hass):
async def _async_mock_devices_with_logbook_platform(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> list[dr.DeviceEntry]:
"""Mock an integration that provides a device that are described by the logbook."""
entry = MockConfigEntry(domain="test", data={"first": True}, options=None)
entry.add_to_hass(hass)
dev_reg = device_registry.async_get(hass)
device = dev_reg.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
identifiers={("bridgeid", "0123")},
sw_version="sw-version",
name="device name",
@ -117,9 +119,9 @@ async def _async_mock_devices_with_logbook_platform(hass):
model="model",
suggested_area="Game Room",
)
device2 = dev_reg.async_get_or_create(
device2 = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:CC")},
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:CC")},
identifiers={("bridgeid", "4567")},
sw_version="sw-version",
name="device name",
@ -413,7 +415,10 @@ async def test_get_events_invalid_filters(
async def test_get_events_with_device_ids(
recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator
recorder_mock: Recorder,
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test logbook get_events for device ids."""
now = dt_util.utcnow()
@ -424,7 +429,7 @@ async def test_get_events_with_device_ids(
]
)
devices = await _async_mock_devices_with_logbook_platform(hass)
devices = await _async_mock_devices_with_logbook_platform(hass, device_registry)
device = devices[0]
device2 = devices[1]
@ -1797,7 +1802,10 @@ async def test_subscribe_unsubscribe_logbook_stream_big_query(
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
async def test_subscribe_unsubscribe_logbook_stream_device(
recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator
recorder_mock: Recorder,
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test subscribe/unsubscribe logbook stream with a device."""
now = dt_util.utcnow()
@ -1807,7 +1815,7 @@ async def test_subscribe_unsubscribe_logbook_stream_device(
for comp in ("homeassistant", "logbook", "automation", "script")
]
)
devices = await _async_mock_devices_with_logbook_platform(hass)
devices = await _async_mock_devices_with_logbook_platform(hass, device_registry)
device = devices[0]
device2 = devices[1]
@ -1923,7 +1931,10 @@ async def test_event_stream_bad_start_time(
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
async def test_logbook_stream_match_multiple_entities(
recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator
recorder_mock: Recorder,
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
entity_registry: er.EntityRegistry,
) -> None:
"""Test logbook stream with a described integration that uses multiple entities."""
now = dt_util.utcnow()
@ -1933,7 +1944,7 @@ async def test_logbook_stream_match_multiple_entities(
for comp in ("homeassistant", "logbook", "automation", "script")
]
)
entry = await _async_mock_entity_with_logbook_platform(hass)
entry = await _async_mock_entity_with_logbook_platform(hass, entity_registry)
entity_id = entry.entity_id
hass.states.async_set(entity_id, STATE_ON)
@ -2066,6 +2077,7 @@ async def test_live_stream_with_one_second_commit_interval(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test the recorder with a 1s commit interval."""
config = {recorder.CONF_COMMIT_INTERVAL: 0.5}
@ -2077,7 +2089,7 @@ async def test_live_stream_with_one_second_commit_interval(
for comp in ("homeassistant", "logbook", "automation", "script")
]
)
devices = await _async_mock_devices_with_logbook_platform(hass)
devices = await _async_mock_devices_with_logbook_platform(hass, device_registry)
device = devices[0]
await hass.async_block_till_done()
@ -2281,6 +2293,7 @@ async def test_recorder_is_far_behind(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
caplog: pytest.LogCaptureFixture,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we still start live streaming if the recorder is far behind."""
now = dt_util.utcnow()
@ -2291,7 +2304,7 @@ async def test_recorder_is_far_behind(
]
)
await async_wait_recording_done(hass)
devices = await _async_mock_devices_with_logbook_platform(hass)
devices = await _async_mock_devices_with_logbook_platform(hass, device_registry)
device = devices[0]
await async_wait_recording_done(hass)
@ -2705,7 +2718,10 @@ async def test_logbook_stream_ignores_forced_updates(
@patch("homeassistant.components.logbook.websocket_api.EVENT_COALESCE_TIME", 0)
async def test_subscribe_all_entities_are_continuous_with_device(
recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator
recorder_mock: Recorder,
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test subscribe/unsubscribe logbook stream with entities that are always filtered and a device."""
now = dt_util.utcnow()
@ -2716,7 +2732,7 @@ async def test_subscribe_all_entities_are_continuous_with_device(
]
)
await async_wait_recording_done(hass)
devices = await _async_mock_devices_with_logbook_platform(hass)
devices = await _async_mock_devices_with_logbook_platform(hass, device_registry)
device = devices[0]
device2 = devices[1]

View File

@ -6,7 +6,7 @@ from freezegun import freeze_time
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import PERCENTAGE, SIGNAL_STRENGTH_DECIBELS_MILLIWATT
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
import homeassistant.util.dt as dt_util
from .conftest import (
@ -72,7 +72,9 @@ async def test_minutes_remaining_sensor(hass: HomeAssistant) -> None:
assert minutes_remaining_sensor.state == end_time.isoformat(timespec="seconds")
async def test_rssi_sensor(hass: HomeAssistant) -> None:
async def test_rssi_sensor(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test the rssi sensor."""
entry = mock_config_entry(hass)
@ -88,15 +90,14 @@ async def test_rssi_sensor(hass: HomeAssistant) -> None:
entity_id = f"sensor.{device.name}_rssi"
# Ensure the entity is disabled by default by checking the registry
ent_registry = entity_registry.async_get(hass)
rssi_registry_entry = ent_registry.async_get(entity_id)
rssi_registry_entry = entity_registry.async_get(entity_id)
assert rssi_registry_entry is not None
assert rssi_registry_entry.disabled_by is not None
# Enable the entity and assert everything else is working as expected
ent_registry.async_update_entity(entity_id, disabled_by=None)
entity_registry.async_update_entity(entity_id, disabled_by=None)
await hass.config_entries.async_reload(entry.entry_id)
await hass.async_block_till_done()

View File

@ -13,7 +13,7 @@ from homeassistant.components.switch import (
from homeassistant.const import ATTR_ENTITY_ID, CONF_IP_ADDRESS, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as ent_reg
from homeassistant.helpers import entity_registry as er
from .mocks import _mock_powerwall_with_fixtures
@ -38,11 +38,12 @@ async def mock_powerwall_fixture(hass):
yield mock_powerwall
async def test_entity_registry(hass: HomeAssistant, mock_powerwall) -> None:
async def test_entity_registry(
hass: HomeAssistant, mock_powerwall, entity_registry: er.EntityRegistry
) -> None:
"""Test powerwall off-grid switch device."""
mock_powerwall.get_grid_status = Mock(return_value=GridStatus.CONNECTED)
entity_registry = ent_reg.async_get(hass)
assert ENTITY_ID in entity_registry.entities

View File

@ -2,6 +2,7 @@
from dataclasses import dataclass
import datetime
from http import HTTPStatus
from typing import Any
from unittest import mock
import prometheus_client
@ -58,7 +59,7 @@ from homeassistant.const import (
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, split_entity_id
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -522,7 +523,11 @@ async def test_counter(client, counter_entities) -> None:
@pytest.mark.parametrize("namespace", [""])
async def test_renaming_entity_name(
hass: HomeAssistant, registry, client, sensor_entities, climate_entities
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client,
sensor_entities,
climate_entities,
) -> None:
"""Test renaming entity name."""
data = {**sensor_entities, **climate_entities}
@ -566,9 +571,9 @@ async def test_renaming_entity_name(
'friendly_name="HeatPump"} 0.0' in body
)
assert "sensor.outside_temperature" in registry.entities
assert "climate.heatpump" in registry.entities
registry.async_update_entity(
assert "sensor.outside_temperature" in entity_registry.entities
assert "climate.heatpump" in entity_registry.entities
entity_registry.async_update_entity(
entity_id=data["sensor_1"].entity_id,
name="Outside Temperature Renamed",
)
@ -578,7 +583,7 @@ async def test_renaming_entity_name(
15.6,
{ATTR_FRIENDLY_NAME: "Outside Temperature Renamed"},
)
registry.async_update_entity(
entity_registry.async_update_entity(
entity_id=data["climate_1"].entity_id,
name="HeatPump Renamed",
)
@ -644,7 +649,11 @@ async def test_renaming_entity_name(
@pytest.mark.parametrize("namespace", [""])
async def test_renaming_entity_id(
hass: HomeAssistant, registry, client, sensor_entities, climate_entities
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client,
sensor_entities,
climate_entities,
) -> None:
"""Test renaming entity id."""
data = {**sensor_entities, **climate_entities}
@ -674,9 +683,9 @@ async def test_renaming_entity_id(
'friendly_name="Outside Humidity"} 1.0' in body
)
assert "sensor.outside_temperature" in registry.entities
assert "climate.heatpump" in registry.entities
registry.async_update_entity(
assert "sensor.outside_temperature" in entity_registry.entities
assert "climate.heatpump" in entity_registry.entities
entity_registry.async_update_entity(
entity_id="sensor.outside_temperature",
new_entity_id="sensor.outside_temperature_renamed",
)
@ -720,7 +729,11 @@ async def test_renaming_entity_id(
@pytest.mark.parametrize("namespace", [""])
async def test_deleting_entity(
hass: HomeAssistant, registry, client, sensor_entities, climate_entities
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client,
sensor_entities,
climate_entities,
) -> None:
"""Test deleting a entity."""
data = {**sensor_entities, **climate_entities}
@ -764,10 +777,10 @@ async def test_deleting_entity(
'friendly_name="HeatPump"} 0.0' in body
)
assert "sensor.outside_temperature" in registry.entities
assert "climate.heatpump" in registry.entities
registry.async_remove(data["sensor_1"].entity_id)
registry.async_remove(data["climate_1"].entity_id)
assert "sensor.outside_temperature" in entity_registry.entities
assert "climate.heatpump" in entity_registry.entities
entity_registry.async_remove(data["sensor_1"].entity_id)
entity_registry.async_remove(data["climate_1"].entity_id)
await hass.async_block_till_done()
body = await generate_latest_metrics(client)
@ -795,7 +808,11 @@ async def test_deleting_entity(
@pytest.mark.parametrize("namespace", [""])
async def test_disabling_entity(
hass: HomeAssistant, registry, client, sensor_entities, climate_entities
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client,
sensor_entities,
climate_entities,
) -> None:
"""Test disabling a entity."""
data = {**sensor_entities, **climate_entities}
@ -848,15 +865,15 @@ async def test_disabling_entity(
'friendly_name="HeatPump"} 0.0' in body
)
assert "sensor.outside_temperature" in registry.entities
assert "climate.heatpump" in registry.entities
registry.async_update_entity(
assert "sensor.outside_temperature" in entity_registry.entities
assert "climate.heatpump" in entity_registry.entities
entity_registry.async_update_entity(
entity_id=data["sensor_1"].entity_id,
disabled_by=entity_registry.RegistryEntryDisabler.USER,
disabled_by=er.RegistryEntryDisabler.USER,
)
registry.async_update_entity(
entity_registry.async_update_entity(
entity_id="climate.heatpump",
disabled_by=entity_registry.RegistryEntryDisabler.USER,
disabled_by=er.RegistryEntryDisabler.USER,
)
await hass.async_block_till_done()
@ -883,17 +900,13 @@ async def test_disabling_entity(
)
@pytest.fixture(name="registry")
def entity_registry_fixture(hass):
"""Provide entity registry."""
return entity_registry.async_get(hass)
@pytest.fixture(name="sensor_entities")
async def sensor_fixture(hass, registry):
async def sensor_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate sensor entities."""
data = {}
sensor_1 = registry.async_get_or_create(
sensor_1 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_1",
@ -907,7 +920,7 @@ async def sensor_fixture(hass, registry):
data["sensor_1"] = sensor_1
data["sensor_1_attributes"] = sensor_1_attributes
sensor_2 = registry.async_get_or_create(
sensor_2 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_2",
@ -919,7 +932,7 @@ async def sensor_fixture(hass, registry):
set_state_with_entry(hass, sensor_2, 54.0)
data["sensor_2"] = sensor_2
sensor_3 = registry.async_get_or_create(
sensor_3 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_3",
@ -935,7 +948,7 @@ async def sensor_fixture(hass, registry):
set_state_with_entry(hass, sensor_3, 14)
data["sensor_3"] = sensor_3
sensor_4 = registry.async_get_or_create(
sensor_4 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_4",
@ -946,7 +959,7 @@ async def sensor_fixture(hass, registry):
set_state_with_entry(hass, sensor_4, 74)
data["sensor_4"] = sensor_4
sensor_5 = registry.async_get_or_create(
sensor_5 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_5",
@ -957,7 +970,7 @@ async def sensor_fixture(hass, registry):
set_state_with_entry(hass, sensor_5, 0.123)
data["sensor_5"] = sensor_5
sensor_6 = registry.async_get_or_create(
sensor_6 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_6",
@ -968,7 +981,7 @@ async def sensor_fixture(hass, registry):
set_state_with_entry(hass, sensor_6, 25)
data["sensor_6"] = sensor_6
sensor_7 = registry.async_get_or_create(
sensor_7 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_7",
@ -979,7 +992,7 @@ async def sensor_fixture(hass, registry):
set_state_with_entry(hass, sensor_7, 3.7069)
data["sensor_7"] = sensor_7
sensor_8 = registry.async_get_or_create(
sensor_8 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_8",
@ -989,7 +1002,7 @@ async def sensor_fixture(hass, registry):
set_state_with_entry(hass, sensor_8, 0.002)
data["sensor_8"] = sensor_8
sensor_9 = registry.async_get_or_create(
sensor_9 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_9",
@ -999,7 +1012,7 @@ async def sensor_fixture(hass, registry):
set_state_with_entry(hass, sensor_9, "should_not_work")
data["sensor_9"] = sensor_9
sensor_10 = registry.async_get_or_create(
sensor_10 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_10",
@ -1010,7 +1023,7 @@ async def sensor_fixture(hass, registry):
set_state_with_entry(hass, sensor_10, "should_not_work")
data["sensor_10"] = sensor_10
sensor_11 = registry.async_get_or_create(
sensor_11 = entity_registry.async_get_or_create(
domain=sensor.DOMAIN,
platform="test",
unique_id="sensor_11",
@ -1027,10 +1040,12 @@ async def sensor_fixture(hass, registry):
@pytest.fixture(name="climate_entities")
async def climate_fixture(hass, registry):
async def climate_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry | dict[str, Any]]:
"""Simulate climate entities."""
data = {}
climate_1 = registry.async_get_or_create(
climate_1 = entity_registry.async_get_or_create(
domain=climate.DOMAIN,
platform="test",
unique_id="climate_1",
@ -1049,7 +1064,7 @@ async def climate_fixture(hass, registry):
data["climate_1"] = climate_1
data["climate_1_attributes"] = climate_1_attributes
climate_2 = registry.async_get_or_create(
climate_2 = entity_registry.async_get_or_create(
domain=climate.DOMAIN,
platform="test",
unique_id="climate_2",
@ -1070,7 +1085,7 @@ async def climate_fixture(hass, registry):
data["climate_2"] = climate_2
data["climate_2_attributes"] = climate_2_attributes
climate_3 = registry.async_get_or_create(
climate_3 = entity_registry.async_get_or_create(
domain=climate.DOMAIN,
platform="test",
unique_id="climate_3",
@ -1092,10 +1107,12 @@ async def climate_fixture(hass, registry):
@pytest.fixture(name="humidifier_entities")
async def humidifier_fixture(hass, registry):
async def humidifier_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry | dict[str, Any]]:
"""Simulate humidifier entities."""
data = {}
humidifier_1 = registry.async_get_or_create(
humidifier_1 = entity_registry.async_get_or_create(
domain=humidifier.DOMAIN,
platform="test",
unique_id="humidifier_1",
@ -1110,7 +1127,7 @@ async def humidifier_fixture(hass, registry):
data["humidifier_1"] = humidifier_1
data["humidifier_1_attributes"] = humidifier_1_attributes
humidifier_2 = registry.async_get_or_create(
humidifier_2 = entity_registry.async_get_or_create(
domain=humidifier.DOMAIN,
platform="test",
unique_id="humidifier_2",
@ -1125,7 +1142,7 @@ async def humidifier_fixture(hass, registry):
data["humidifier_2"] = humidifier_2
data["humidifier_2_attributes"] = humidifier_2_attributes
humidifier_3 = registry.async_get_or_create(
humidifier_3 = entity_registry.async_get_or_create(
domain=humidifier.DOMAIN,
platform="test",
unique_id="humidifier_3",
@ -1146,10 +1163,12 @@ async def humidifier_fixture(hass, registry):
@pytest.fixture(name="lock_entities")
async def lock_fixture(hass, registry):
async def lock_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate lock entities."""
data = {}
lock_1 = registry.async_get_or_create(
lock_1 = entity_registry.async_get_or_create(
domain=lock.DOMAIN,
platform="test",
unique_id="lock_1",
@ -1159,7 +1178,7 @@ async def lock_fixture(hass, registry):
set_state_with_entry(hass, lock_1, STATE_LOCKED)
data["lock_1"] = lock_1
lock_2 = registry.async_get_or_create(
lock_2 = entity_registry.async_get_or_create(
domain=lock.DOMAIN,
platform="test",
unique_id="lock_2",
@ -1174,10 +1193,12 @@ async def lock_fixture(hass, registry):
@pytest.fixture(name="cover_entities")
async def cover_fixture(hass, registry):
async def cover_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate cover entities."""
data = {}
cover_open = registry.async_get_or_create(
cover_open = entity_registry.async_get_or_create(
domain=cover.DOMAIN,
platform="test",
unique_id="cover_open",
@ -1187,7 +1208,7 @@ async def cover_fixture(hass, registry):
set_state_with_entry(hass, cover_open, STATE_OPEN)
data["cover_open"] = cover_open
cover_closed = registry.async_get_or_create(
cover_closed = entity_registry.async_get_or_create(
domain=cover.DOMAIN,
platform="test",
unique_id="cover_closed",
@ -1197,7 +1218,7 @@ async def cover_fixture(hass, registry):
set_state_with_entry(hass, cover_closed, STATE_CLOSED)
data["cover_closed"] = cover_closed
cover_closing = registry.async_get_or_create(
cover_closing = entity_registry.async_get_or_create(
domain=cover.DOMAIN,
platform="test",
unique_id="cover_closing",
@ -1207,7 +1228,7 @@ async def cover_fixture(hass, registry):
set_state_with_entry(hass, cover_closing, STATE_CLOSING)
data["cover_closing"] = cover_closing
cover_opening = registry.async_get_or_create(
cover_opening = entity_registry.async_get_or_create(
domain=cover.DOMAIN,
platform="test",
unique_id="cover_opening",
@ -1217,7 +1238,7 @@ async def cover_fixture(hass, registry):
set_state_with_entry(hass, cover_opening, STATE_OPENING)
data["cover_opening"] = cover_opening
cover_position = registry.async_get_or_create(
cover_position = entity_registry.async_get_or_create(
domain=cover.DOMAIN,
platform="test",
unique_id="cover_position",
@ -1228,7 +1249,7 @@ async def cover_fixture(hass, registry):
set_state_with_entry(hass, cover_position, STATE_OPEN, cover_position_attributes)
data["cover_position"] = cover_position
cover_tilt_position = registry.async_get_or_create(
cover_tilt_position = entity_registry.async_get_or_create(
domain=cover.DOMAIN,
platform="test",
unique_id="cover_tilt_position",
@ -1246,10 +1267,12 @@ async def cover_fixture(hass, registry):
@pytest.fixture(name="input_number_entities")
async def input_number_fixture(hass, registry):
async def input_number_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate input_number entities."""
data = {}
input_number_1 = registry.async_get_or_create(
input_number_1 = entity_registry.async_get_or_create(
domain=input_number.DOMAIN,
platform="test",
unique_id="input_number_1",
@ -1259,7 +1282,7 @@ async def input_number_fixture(hass, registry):
set_state_with_entry(hass, input_number_1, 5.2)
data["input_number_1"] = input_number_1
input_number_2 = registry.async_get_or_create(
input_number_2 = entity_registry.async_get_or_create(
domain=input_number.DOMAIN,
platform="test",
unique_id="input_number_2",
@ -1268,7 +1291,7 @@ async def input_number_fixture(hass, registry):
set_state_with_entry(hass, input_number_2, 60)
data["input_number_2"] = input_number_2
input_number_3 = registry.async_get_or_create(
input_number_3 = entity_registry.async_get_or_create(
domain=input_number.DOMAIN,
platform="test",
unique_id="input_number_3",
@ -1284,10 +1307,12 @@ async def input_number_fixture(hass, registry):
@pytest.fixture(name="input_boolean_entities")
async def input_boolean_fixture(hass, registry):
async def input_boolean_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate input_boolean entities."""
data = {}
input_boolean_1 = registry.async_get_or_create(
input_boolean_1 = entity_registry.async_get_or_create(
domain=input_boolean.DOMAIN,
platform="test",
unique_id="input_boolean_1",
@ -1297,7 +1322,7 @@ async def input_boolean_fixture(hass, registry):
set_state_with_entry(hass, input_boolean_1, STATE_ON)
data["input_boolean_1"] = input_boolean_1
input_boolean_2 = registry.async_get_or_create(
input_boolean_2 = entity_registry.async_get_or_create(
domain=input_boolean.DOMAIN,
platform="test",
unique_id="input_boolean_2",
@ -1312,10 +1337,12 @@ async def input_boolean_fixture(hass, registry):
@pytest.fixture(name="binary_sensor_entities")
async def binary_sensor_fixture(hass, registry):
async def binary_sensor_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate binary_sensor entities."""
data = {}
binary_sensor_1 = registry.async_get_or_create(
binary_sensor_1 = entity_registry.async_get_or_create(
domain=binary_sensor.DOMAIN,
platform="test",
unique_id="binary_sensor_1",
@ -1325,7 +1352,7 @@ async def binary_sensor_fixture(hass, registry):
set_state_with_entry(hass, binary_sensor_1, STATE_ON)
data["binary_sensor_1"] = binary_sensor_1
binary_sensor_2 = registry.async_get_or_create(
binary_sensor_2 = entity_registry.async_get_or_create(
domain=binary_sensor.DOMAIN,
platform="test",
unique_id="binary_sensor_2",
@ -1340,10 +1367,12 @@ async def binary_sensor_fixture(hass, registry):
@pytest.fixture(name="light_entities")
async def light_fixture(hass, registry):
async def light_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate light entities."""
data = {}
light_1 = registry.async_get_or_create(
light_1 = entity_registry.async_get_or_create(
domain=light.DOMAIN,
platform="test",
unique_id="light_1",
@ -1353,7 +1382,7 @@ async def light_fixture(hass, registry):
set_state_with_entry(hass, light_1, STATE_ON)
data["light_1"] = light_1
light_2 = registry.async_get_or_create(
light_2 = entity_registry.async_get_or_create(
domain=light.DOMAIN,
platform="test",
unique_id="light_2",
@ -1363,7 +1392,7 @@ async def light_fixture(hass, registry):
set_state_with_entry(hass, light_2, STATE_OFF)
data["light_2"] = light_2
light_3 = registry.async_get_or_create(
light_3 = entity_registry.async_get_or_create(
domain=light.DOMAIN,
platform="test",
unique_id="light_3",
@ -1375,7 +1404,7 @@ async def light_fixture(hass, registry):
data["light_3"] = light_3
data["light_3_attributes"] = light_3_attributes
light_4 = registry.async_get_or_create(
light_4 = entity_registry.async_get_or_create(
domain=light.DOMAIN,
platform="test",
unique_id="light_4",
@ -1392,10 +1421,12 @@ async def light_fixture(hass, registry):
@pytest.fixture(name="switch_entities")
async def switch_fixture(hass, registry):
async def switch_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry | dict[str, Any]]:
"""Simulate switch entities."""
data = {}
switch_1 = registry.async_get_or_create(
switch_1 = entity_registry.async_get_or_create(
domain=switch.DOMAIN,
platform="test",
unique_id="switch_1",
@ -1407,7 +1438,7 @@ async def switch_fixture(hass, registry):
data["switch_1"] = switch_1
data["switch_1_attributes"] = switch_1_attributes
switch_2 = registry.async_get_or_create(
switch_2 = entity_registry.async_get_or_create(
domain=switch.DOMAIN,
platform="test",
unique_id="switch_2",
@ -1424,10 +1455,12 @@ async def switch_fixture(hass, registry):
@pytest.fixture(name="person_entities")
async def person_fixture(hass, registry):
async def person_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate person entities."""
data = {}
person_1 = registry.async_get_or_create(
person_1 = entity_registry.async_get_or_create(
domain=person.DOMAIN,
platform="test",
unique_id="person_1",
@ -1437,7 +1470,7 @@ async def person_fixture(hass, registry):
set_state_with_entry(hass, person_1, STATE_HOME)
data["person_1"] = person_1
person_2 = registry.async_get_or_create(
person_2 = entity_registry.async_get_or_create(
domain=person.DOMAIN,
platform="test",
unique_id="person_2",
@ -1452,10 +1485,12 @@ async def person_fixture(hass, registry):
@pytest.fixture(name="device_tracker_entities")
async def device_tracker_fixture(hass, registry):
async def device_tracker_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate device_tracker entities."""
data = {}
device_tracker_1 = registry.async_get_or_create(
device_tracker_1 = entity_registry.async_get_or_create(
domain=device_tracker.DOMAIN,
platform="test",
unique_id="device_tracker_1",
@ -1465,7 +1500,7 @@ async def device_tracker_fixture(hass, registry):
set_state_with_entry(hass, device_tracker_1, STATE_HOME)
data["device_tracker_1"] = device_tracker_1
device_tracker_2 = registry.async_get_or_create(
device_tracker_2 = entity_registry.async_get_or_create(
domain=device_tracker.DOMAIN,
platform="test",
unique_id="device_tracker_2",
@ -1480,10 +1515,12 @@ async def device_tracker_fixture(hass, registry):
@pytest.fixture(name="counter_entities")
async def counter_fixture(hass, registry):
async def counter_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> dict[str, er.RegistryEntry]:
"""Simulate counter entities."""
data = {}
counter_1 = registry.async_get_or_create(
counter_1 = entity_registry.async_get_or_create(
domain=counter.DOMAIN,
platform="test",
unique_id="counter_1",
@ -1497,8 +1534,8 @@ async def counter_fixture(hass, registry):
def set_state_with_entry(
hass,
entry: entity_registry.RegistryEntry,
hass: HomeAssistant,
entry: er.RegistryEntry,
state,
additional_attributes=None,
new_entity_id=None,

View File

@ -5,18 +5,19 @@ from unittest.mock import AsyncMock
from homeassistant.components.qnap_qsw.const import ATTR_MESSAGE
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from .util import async_init_integration
async def test_qnap_qsw_create_binary_sensors(
hass: HomeAssistant, entity_registry_enabled_by_default: AsyncMock
hass: HomeAssistant,
entity_registry_enabled_by_default: AsyncMock,
entity_registry: er.EntityRegistry,
) -> None:
"""Test creation of binary sensors."""
await async_init_integration(hass)
er = entity_registry.async_get(hass)
state = hass.states.get("binary_sensor.qsw_m408_4c_anomaly")
assert state.state == STATE_OFF
@ -24,7 +25,7 @@ async def test_qnap_qsw_create_binary_sensors(
state = hass.states.get("binary_sensor.qsw_m408_4c_lacp_port_1_link")
assert state.state == STATE_OFF
entry = er.async_get(state.entity_id)
entry = entity_registry.async_get(state.entity_id)
assert entry.unique_id == "qsw_unique_id_ports-status_lacp_port_1_link"
state = hass.states.get("binary_sensor.qsw_m408_4c_lacp_port_2_link")
@ -44,7 +45,7 @@ async def test_qnap_qsw_create_binary_sensors(
state = hass.states.get("binary_sensor.qsw_m408_4c_port_1_link")
assert state.state == STATE_ON
entry = er.async_get(state.entity_id)
entry = entity_registry.async_get(state.entity_id)
assert entry.unique_id == "qsw_unique_id_ports-status_port_1_link"
state = hass.states.get("binary_sensor.qsw_m408_4c_port_2_link")

View File

@ -3,7 +3,7 @@ from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.components.rituals_perfume_genie.binary_sensor import CHARGING_SUFFIX
from homeassistant.const import ATTR_DEVICE_CLASS, STATE_ON, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from .common import (
init_integration,
@ -12,12 +12,13 @@ from .common import (
)
async def test_binary_sensors(hass: HomeAssistant) -> None:
async def test_binary_sensors(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test the creation and values of the Rituals Perfume Genie binary sensor."""
config_entry = mock_config_entry(unique_id="binary_sensor_test_diffuser_v1")
diffuser = mock_diffuser_v1_battery_cartridge()
await init_integration(hass, config_entry, [diffuser])
registry = entity_registry.async_get(hass)
hublot = diffuser.hublot
state = hass.states.get("binary_sensor.genie_battery_charging")
@ -27,7 +28,7 @@ async def test_binary_sensors(hass: HomeAssistant) -> None:
state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.BATTERY_CHARGING
)
entry = registry.async_get("binary_sensor.genie_battery_charging")
entry = entity_registry.async_get("binary_sensor.genie_battery_charging")
assert entry
assert entry.unique_id == f"{hublot}{CHARGING_SUFFIX}"
assert entry.entity_category == EntityCategory.DIAGNOSTIC

View File

@ -18,7 +18,7 @@ from homeassistant.components.rituals_perfume_genie.number import (
)
from homeassistant.const import ATTR_ENTITY_ID, ATTR_ICON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from .common import (
@ -29,14 +29,14 @@ from .common import (
)
async def test_number_entity(hass: HomeAssistant) -> None:
async def test_number_entity(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test the creation and values of the diffuser number entity."""
config_entry = mock_config_entry(unique_id="number_test")
diffuser = mock_diffuser(hublot="lot123", perfume_amount=2)
await init_integration(hass, config_entry, [diffuser])
registry = entity_registry.async_get(hass)
state = hass.states.get("number.genie_perfume_amount")
assert state
assert state.state == str(diffuser.perfume_amount)
@ -44,7 +44,7 @@ async def test_number_entity(hass: HomeAssistant) -> None:
assert state.attributes[ATTR_MIN] == MIN_PERFUME_AMOUNT
assert state.attributes[ATTR_MAX] == MAX_PERFUME_AMOUNT
entry = registry.async_get("number.genie_perfume_amount")
entry = entity_registry.async_get("number.genie_perfume_amount")
assert entry
assert entry.unique_id == f"{diffuser.hublot}{PERFUME_AMOUNT_SUFFIX}"

View File

@ -16,27 +16,27 @@ from homeassistant.const import (
EntityCategory,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from .common import init_integration, mock_config_entry, mock_diffuser
async def test_select_entity(hass: HomeAssistant) -> None:
async def test_select_entity(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test the creation and state of the diffuser select entity."""
config_entry = mock_config_entry(unique_id="select_test")
diffuser = mock_diffuser(hublot="lot123", room_size_square_meter=60)
await init_integration(hass, config_entry, [diffuser])
registry = entity_registry.async_get(hass)
state = hass.states.get("select.genie_room_size")
assert state
assert state.state == str(diffuser.room_size_square_meter)
assert state.attributes[ATTR_ICON] == "mdi:ruler-square"
assert state.attributes[ATTR_OPTIONS] == ["15", "30", "60", "100"]
entry = registry.async_get("select.genie_room_size")
entry = entity_registry.async_get("select.genie_room_size")
assert entry
assert entry.unique_id == f"{diffuser.hublot}{ROOM_SIZE_SUFFIX}"
assert entry.unit_of_measurement == AREA_SQUARE_METERS

View File

@ -14,7 +14,7 @@ from homeassistant.const import (
EntityCategory,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from .common import (
init_integration,
@ -24,12 +24,13 @@ from .common import (
)
async def test_sensors_diffuser_v1_battery_cartridge(hass: HomeAssistant) -> None:
async def test_sensors_diffuser_v1_battery_cartridge(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test the creation and values of the Rituals Perfume Genie sensors."""
config_entry = mock_config_entry(unique_id="id_123_sensor_test_diffuser_v1")
diffuser = mock_diffuser_v1_battery_cartridge()
await init_integration(hass, config_entry, [diffuser])
registry = entity_registry.async_get(hass)
hublot = diffuser.hublot
state = hass.states.get("sensor.genie_perfume")
@ -37,7 +38,7 @@ async def test_sensors_diffuser_v1_battery_cartridge(hass: HomeAssistant) -> Non
assert state.state == diffuser.perfume
assert state.attributes.get(ATTR_ICON) == "mdi:tag-text"
entry = registry.async_get("sensor.genie_perfume")
entry = entity_registry.async_get("sensor.genie_perfume")
assert entry
assert entry.unique_id == f"{hublot}{PERFUME_SUFFIX}"
@ -46,7 +47,7 @@ async def test_sensors_diffuser_v1_battery_cartridge(hass: HomeAssistant) -> Non
assert state.state == diffuser.fill
assert state.attributes.get(ATTR_ICON) == "mdi:beaker"
entry = registry.async_get("sensor.genie_fill")
entry = entity_registry.async_get("sensor.genie_fill")
assert entry
assert entry.unique_id == f"{hublot}{FILL_SUFFIX}"
@ -56,7 +57,7 @@ async def test_sensors_diffuser_v1_battery_cartridge(hass: HomeAssistant) -> Non
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.BATTERY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
entry = registry.async_get("sensor.genie_battery")
entry = entity_registry.async_get("sensor.genie_battery")
assert entry
assert entry.unique_id == f"{hublot}{BATTERY_SUFFIX}"
assert entry.entity_category == EntityCategory.DIAGNOSTIC
@ -67,7 +68,7 @@ async def test_sensors_diffuser_v1_battery_cartridge(hass: HomeAssistant) -> Non
assert state.attributes.get(ATTR_DEVICE_CLASS) is None
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
entry = registry.async_get("sensor.genie_wifi")
entry = entity_registry.async_get("sensor.genie_wifi")
assert entry
assert entry.unique_id == f"{hublot}{WIFI_SUFFIX}"
assert entry.entity_category == EntityCategory.DIAGNOSTIC

View File

@ -13,7 +13,7 @@ from homeassistant.const import (
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from .common import (
@ -23,20 +23,20 @@ from .common import (
)
async def test_switch_entity(hass: HomeAssistant) -> None:
async def test_switch_entity(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test the creation and values of the Rituals Perfume Genie diffuser switch."""
config_entry = mock_config_entry(unique_id="id_123_switch_test")
diffuser = mock_diffuser_v1_battery_cartridge()
await init_integration(hass, config_entry, [diffuser])
registry = entity_registry.async_get(hass)
state = hass.states.get("switch.genie")
assert state
assert state.state == STATE_ON
assert state.attributes.get(ATTR_ICON) == "mdi:fan"
entry = registry.async_get("switch.genie")
entry = entity_registry.async_get("switch.genie")
assert entry
assert entry.unique_id == diffuser.hublot

View File

@ -27,7 +27,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from . import SnoozFixture, create_mock_snooz, create_mock_snooz_config_entry
@ -172,12 +172,14 @@ async def test_push_events(
assert state.attributes[ATTR_ASSUMED_STATE] is True
async def test_restore_state(hass: HomeAssistant) -> None:
async def test_restore_state(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Tests restoring entity state."""
device = await create_mock_snooz(connected=False, initial_state=UnknownSnoozState)
entry = await create_mock_snooz_config_entry(hass, device)
entity_id = get_fan_entity_id(hass, device)
entity_id = get_fan_entity_id(hass, device, entity_registry)
# call service to store state
await hass.services.async_call(
@ -203,12 +205,14 @@ async def test_restore_state(hass: HomeAssistant) -> None:
assert state.attributes[ATTR_ASSUMED_STATE] is True
async def test_restore_unknown_state(hass: HomeAssistant) -> None:
async def test_restore_unknown_state(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Tests restoring entity state that was unknown."""
device = await create_mock_snooz(connected=False, initial_state=UnknownSnoozState)
entry = await create_mock_snooz_config_entry(hass, device)
entity_id = get_fan_entity_id(hass, device)
entity_id = get_fan_entity_id(hass, device, entity_registry)
# unload entry
await hass.config_entries.async_unload(entry.entry_id)
@ -282,16 +286,18 @@ async def test_command_results(
@pytest.fixture(name="snooz_fan_entity_id")
async def fixture_snooz_fan_entity_id(
hass: HomeAssistant, mock_connected_snooz: SnoozFixture
hass: HomeAssistant,
mock_connected_snooz: SnoozFixture,
entity_registry: er.EntityRegistry,
) -> str:
"""Mock a Snooz fan entity and config entry."""
return get_fan_entity_id(hass, mock_connected_snooz.device)
return get_fan_entity_id(hass, mock_connected_snooz.device, entity_registry)
def get_fan_entity_id(hass: HomeAssistant, device: MockSnoozDevice) -> str:
def get_fan_entity_id(
hass: HomeAssistant, device: MockSnoozDevice, entity_registry: er.EntityRegistry
) -> str:
"""Get the entity ID for a mock device."""
return entity_registry.async_get(hass).async_get_entity_id(
Platform.FAN, DOMAIN, device.address
)
return entity_registry.async_get_entity_id(Platform.FAN, DOMAIN, device.address)

View File

@ -9,7 +9,7 @@ from homeassistant import setup
from homeassistant.components.todoist.calendar import DOMAIN
from homeassistant.const import CONF_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
@ -69,7 +69,9 @@ def mock_api(task) -> AsyncMock:
@patch("homeassistant.components.todoist.calendar.TodoistAPIAsync")
async def test_calendar_entity_unique_id(todoist_api, hass: HomeAssistant, api) -> None:
async def test_calendar_entity_unique_id(
todoist_api, hass: HomeAssistant, api, entity_registry: er.EntityRegistry
) -> None:
"""Test unique id is set to project id."""
todoist_api.return_value = api
assert await setup.async_setup_component(
@ -84,8 +86,7 @@ async def test_calendar_entity_unique_id(todoist_api, hass: HomeAssistant, api)
)
await hass.async_block_till_done()
registry = entity_registry.async_get(hass)
entity = registry.async_get("calendar.name")
entity = entity_registry.async_get("calendar.name")
assert entity.unique_id == "12345"
@ -114,7 +115,7 @@ async def test_update_entity_for_custom_project_with_labels_on(todoist_api, hass
@patch("homeassistant.components.todoist.calendar.TodoistAPIAsync")
async def test_calendar_custom_project_unique_id(
todoist_api, hass: HomeAssistant, api
todoist_api, hass: HomeAssistant, api, entity_registry: er.EntityRegistry
) -> None:
"""Test unique id is None for any custom projects."""
todoist_api.return_value = api
@ -131,8 +132,7 @@ async def test_calendar_custom_project_unique_id(
)
await hass.async_block_till_done()
registry = entity_registry.async_get(hass)
entity = registry.async_get("calendar.all_projects")
entity = entity_registry.async_get("calendar.all_projects")
assert entity is None
state = hass.states.get("calendar.all_projects")