Final clean up of helpers accessed via hass (#72032)

* Final clean up of helpers accessed via hass

* Fix circular dep

* Fix import
This commit is contained in:
Franck Nijhof 2022-05-17 22:42:37 +02:00 committed by GitHub
parent d1d6c6b923
commit bfb47eb212
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 75 additions and 82 deletions

View File

@ -22,6 +22,7 @@ from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, entityfilter from homeassistant.helpers import config_validation as cv, entityfilter
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.dispatcher import ( from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, async_dispatcher_connect,
async_dispatcher_send, async_dispatcher_send,
@ -267,15 +268,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return return
loaded = True loaded = True
await hass.helpers.discovery.async_load_platform( await async_load_platform(hass, Platform.BINARY_SENSOR, DOMAIN, {}, config)
Platform.BINARY_SENSOR, DOMAIN, {}, config await async_load_platform(hass, Platform.STT, DOMAIN, {}, config)
) await async_load_platform(hass, Platform.TTS, DOMAIN, {}, config)
await hass.helpers.discovery.async_load_platform(
Platform.STT, DOMAIN, {}, config
)
await hass.helpers.discovery.async_load_platform(
Platform.TTS, DOMAIN, {}, config
)
async_dispatcher_send( async_dispatcher_send(
hass, SIGNAL_CLOUD_CONNECTION_STATE, CloudConnectionState.CLOUD_CONNECTED hass, SIGNAL_CLOUD_CONNECTION_STATE, CloudConnectionState.CLOUD_CONNECTED

View File

@ -18,6 +18,7 @@ from homeassistant.const import (
) )
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -71,9 +72,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
# Set up demo platforms # Set up demo platforms
for platform in COMPONENTS_WITH_DEMO_PLATFORM: for platform in COMPONENTS_WITH_DEMO_PLATFORM:
hass.async_create_task( hass.async_create_task(async_load_platform(hass, platform, DOMAIN, {}, config))
hass.helpers.discovery.async_load_platform(platform, DOMAIN, {}, config)
)
config.setdefault(ha.DOMAIN, {}) config.setdefault(ha.DOMAIN, {})
config.setdefault(DOMAIN, {}) config.setdefault(DOMAIN, {})

View File

@ -46,7 +46,7 @@ from homeassistant.const import (
) )
from homeassistant.core import CoreState, HomeAssistant, ServiceCall, State, callback from homeassistant.core import CoreState, HomeAssistant, ServiceCall, State, callback
from homeassistant.exceptions import HomeAssistantError, Unauthorized from homeassistant.exceptions import HomeAssistantError, Unauthorized
from homeassistant.helpers import device_registry, entity_registry from homeassistant.helpers import device_registry, entity_registry, instance_id
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entityfilter import ( from homeassistant.helpers.entityfilter import (
BASE_FILTER_SCHEMA, BASE_FILTER_SCHEMA,
@ -726,7 +726,7 @@ class HomeKit:
return return
self.status = STATUS_WAIT self.status = STATUS_WAIT
async_zc_instance = await zeroconf.async_get_async_instance(self.hass) async_zc_instance = await zeroconf.async_get_async_instance(self.hass)
uuid = await self.hass.helpers.instance_id.async_get() uuid = await instance_id.async_get(self.hass)
await self.hass.async_add_executor_job(self.setup, async_zc_instance, uuid) await self.hass.async_add_executor_job(self.setup, async_zc_instance, uuid)
self.aid_storage = AccessoryAidStorage(self.hass, self._entry_id) self.aid_storage = AccessoryAidStorage(self.hass, self._entry_id)
await self.aid_storage.async_initialize() await self.aid_storage.async_initialize()

View File

@ -16,6 +16,7 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant, ServiceCall
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import load_platform
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from .const import ( from .const import (
@ -152,6 +153,6 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
submit_tv_request, submit_tv_request,
schema=SUBMIT_TV_REQUEST_SERVICE_SCHEMA, schema=SUBMIT_TV_REQUEST_SERVICE_SCHEMA,
) )
hass.helpers.discovery.load_platform(Platform.SENSOR, DOMAIN, {}, config) load_platform(hass, Platform.SENSOR, DOMAIN, {}, config)
return True return True

View File

@ -14,6 +14,7 @@ from homeassistant.components.http.const import KEY_HASS_REFRESH_TOKEN_ID
from homeassistant.components.http.data_validator import RequestDataValidator from homeassistant.components.http.data_validator import RequestDataValidator
from homeassistant.components.http.view import HomeAssistantView from homeassistant.components.http.view import HomeAssistantView
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import area_registry as ar
from homeassistant.helpers.system_info import async_get_system_info from homeassistant.helpers.system_info import async_get_system_info
from homeassistant.helpers.translation import async_get_translations from homeassistant.helpers.translation import async_get_translations
@ -152,7 +153,7 @@ class UserOnboardingView(_BaseOnboardingView):
hass, data["language"], "area", {DOMAIN} hass, data["language"], "area", {DOMAIN}
) )
area_registry = await hass.helpers.area_registry.async_get_registry() area_registry = ar.async_get(hass)
for area in DEFAULT_AREAS: for area in DEFAULT_AREAS:
area_registry.async_create( area_registry.async_create(

View File

@ -21,6 +21,7 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
@ -178,9 +179,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
for component in PLATFORMS: for component in PLATFORMS:
await hass.async_create_task( await hass.async_create_task(
hass.helpers.discovery.async_load_platform( async_load_platform(hass, component, DOMAIN, {"config": config}, config)
component, DOMAIN, {"config": config}, config
)
) )
return True return True

View File

@ -16,8 +16,9 @@ from homeassistant.const import (
__version__ as current_version, __version__ as current_version,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers import config_validation as cv, entity_platform, instance_id
from homeassistant.helpers.event import async_call_later from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.system_info import async_get_system_info
from homeassistant.loader import Integration, async_get_custom_components from homeassistant.loader import Integration, async_get_custom_components
from .const import ( from .const import (
@ -67,8 +68,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Additional/extra data collection # Additional/extra data collection
channel = get_channel(current_version) channel = get_channel(current_version)
huuid = await hass.helpers.instance_id.async_get() huuid = await instance_id.async_get(hass)
system_info = await hass.helpers.system_info.async_get_system_info() system_info = await async_get_system_info(hass)
custom_components = await async_get_custom_components(hass) custom_components = await async_get_custom_components(hass)
tracing = {} tracing = {}
@ -100,7 +101,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def update_system_info(now): async def update_system_info(now):
nonlocal system_info nonlocal system_info
system_info = await hass.helpers.system_info.async_get_system_info() system_info = await async_get_system_info(hass)
# Update system info every hour # Update system info every hour
async_call_later(hass, 3600, update_system_info) async_call_later(hass, 3600, update_system_info)

View File

@ -28,7 +28,7 @@ from homeassistant.const import (
) )
from homeassistant.core import Event, HomeAssistant, callback from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.data_entry_flow import BaseServiceInfo from homeassistant.data_entry_flow import BaseServiceInfo
from homeassistant.helpers import discovery_flow from homeassistant.helpers import discovery_flow, instance_id
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.network import NoURLAvailableError, get_url from homeassistant.helpers.network import NoURLAvailableError, get_url
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
@ -198,7 +198,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
Wait till started or otherwise HTTP is not up and running. Wait till started or otherwise HTTP is not up and running.
""" """
uuid = await hass.helpers.instance_id.async_get() uuid = await instance_id.async_get(hass)
await _async_register_hass_zc_service(hass, aio_zc, uuid) await _async_register_hass_zc_service(hass, aio_zc, uuid)
async def _async_zeroconf_hass_stop(_event: Event) -> None: async def _async_zeroconf_hass_stop(_event: Event) -> None:

View File

@ -420,6 +420,10 @@ class DeviceRegistry:
via_device_id: str | None | UndefinedType = UNDEFINED, via_device_id: str | None | UndefinedType = UNDEFINED,
) -> DeviceEntry | None: ) -> DeviceEntry | None:
"""Update device attributes.""" """Update device attributes."""
# Circular dep
# pylint: disable=import-outside-toplevel
from . import area_registry as ar
old = self.devices[device_id] old = self.devices[device_id]
new_values: dict[str, Any] = {} # Dict with new key/value pairs new_values: dict[str, Any] = {} # Dict with new key/value pairs
@ -442,13 +446,13 @@ class DeviceRegistry:
disabled_by = DeviceEntryDisabler(disabled_by) disabled_by = DeviceEntryDisabler(disabled_by)
if ( if (
suggested_area not in (UNDEFINED, None, "") suggested_area is not None
and suggested_area is not UNDEFINED
and suggested_area != ""
and area_id is UNDEFINED and area_id is UNDEFINED
and old.area_id is None and old.area_id is None
): ):
area = self.hass.helpers.area_registry.async_get( area = ar.async_get(self.hass).async_get_or_create(suggested_area)
self.hass
).async_get_or_create(suggested_area)
area_id = area.id area_id = area.id
if ( if (

View File

@ -2,6 +2,7 @@
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from homeassistant.components.cloud.const import DISPATCHER_REMOTE_UPDATE from homeassistant.components.cloud.const import DISPATCHER_REMOTE_UPDATE
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -14,9 +15,7 @@ async def test_remote_connection_sensor(hass):
assert hass.states.get("binary_sensor.remote_ui") is None assert hass.states.get("binary_sensor.remote_ui") is None
# Fake connection/discovery # Fake connection/discovery
await hass.helpers.discovery.async_load_platform( await async_load_platform(hass, "binary_sensor", "cloud", {}, {"cloud": {}})
"binary_sensor", "cloud", {}, {"cloud": {}}
)
# Mock test env # Mock test env
cloud = hass.data["cloud"] = Mock() cloud = hass.data["cloud"] = Mock()

View File

@ -12,7 +12,7 @@ from homeassistant.components.config import config_entries
from homeassistant.config_entries import HANDLERS, ConfigFlow from homeassistant.config_entries import HANDLERS, ConfigFlow
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.generated import config_flows from homeassistant.generated import config_flows
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_entry_flow, config_validation as cv
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import ( from tests.common import (
@ -68,9 +68,7 @@ async def test_get_entries(hass, client, clear_handlers):
"""Return options flow support for this handler.""" """Return options flow support for this handler."""
return True return True
hass.helpers.config_entry_flow.register_discovery_flow( config_entry_flow.register_discovery_flow("comp2", "Comp 2", lambda: None)
"comp2", "Comp 2", lambda: None
)
entry = MockConfigEntry( entry = MockConfigEntry(
domain="comp1", domain="comp1",

View File

@ -183,8 +183,8 @@ async def test_modern_turn_on_invalid(hass, start_state):
set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode") set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode")
# Turn on with an invalid config (speed, percentage, preset_modes all None) # Turn on with an invalid config (speed, percentage, preset_modes all None)
await hass.helpers.state.async_reproduce_state( await async_reproduce_state(
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_INVALID_STATE)] hass, [State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_INVALID_STATE)]
) )
assert len(turn_on_calls) == 1 assert len(turn_on_calls) == 1
@ -227,8 +227,8 @@ async def test_modern_turn_on_percentage_from_different_speed(hass, start_state)
set_percentage_mode = async_mock_service(hass, "fan", "set_percentage") set_percentage_mode = async_mock_service(hass, "fan", "set_percentage")
set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode") set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode")
await hass.helpers.state.async_reproduce_state( await async_reproduce_state(
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PERCENTAGE15_STATE)] hass, [State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PERCENTAGE15_STATE)]
) )
assert len(turn_on_calls) == 1 assert len(turn_on_calls) == 1
@ -256,8 +256,8 @@ async def test_modern_turn_on_percentage_from_same_speed(hass):
set_percentage_mode = async_mock_service(hass, "fan", "set_percentage") set_percentage_mode = async_mock_service(hass, "fan", "set_percentage")
set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode") set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode")
await hass.helpers.state.async_reproduce_state( await async_reproduce_state(
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PERCENTAGE15_STATE)] hass, [State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PERCENTAGE15_STATE)]
) )
assert len(turn_on_calls) == 1 assert len(turn_on_calls) == 1
@ -293,8 +293,8 @@ async def test_modern_turn_on_preset_mode_from_different_speed(hass, start_state
set_percentage_mode = async_mock_service(hass, "fan", "set_percentage") set_percentage_mode = async_mock_service(hass, "fan", "set_percentage")
set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode") set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode")
await hass.helpers.state.async_reproduce_state( await async_reproduce_state(
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PRESET_MODE_AUTO_STATE)] hass, [State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PRESET_MODE_AUTO_STATE)]
) )
assert len(turn_on_calls) == 1 assert len(turn_on_calls) == 1
@ -324,8 +324,8 @@ async def test_modern_turn_on_preset_mode_from_same_speed(hass):
set_percentage_mode = async_mock_service(hass, "fan", "set_percentage") set_percentage_mode = async_mock_service(hass, "fan", "set_percentage")
set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode") set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode")
await hass.helpers.state.async_reproduce_state( await async_reproduce_state(
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PRESET_MODE_AUTO_STATE)] hass, [State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PRESET_MODE_AUTO_STATE)]
) )
assert len(turn_on_calls) == 1 assert len(turn_on_calls) == 1
@ -361,8 +361,9 @@ async def test_modern_turn_on_preset_mode_reverse(hass, start_state):
set_percentage_mode = async_mock_service(hass, "fan", "set_percentage") set_percentage_mode = async_mock_service(hass, "fan", "set_percentage")
set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode") set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode")
await hass.helpers.state.async_reproduce_state( await async_reproduce_state(
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_PRESET_MODE_AUTO_REVERSE_STATE)] hass,
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_PRESET_MODE_AUTO_REVERSE_STATE)],
) )
assert len(turn_on_calls) == 1 assert len(turn_on_calls) == 1
@ -403,8 +404,8 @@ async def test_modern_to_preset(hass, start_state):
set_percentage_mode = async_mock_service(hass, "fan", "set_percentage") set_percentage_mode = async_mock_service(hass, "fan", "set_percentage")
set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode") set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode")
await hass.helpers.state.async_reproduce_state( await async_reproduce_state(
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PRESET_MODE_AUTO_STATE)] hass, [State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PRESET_MODE_AUTO_STATE)]
) )
assert len(turn_on_calls) == 0 assert len(turn_on_calls) == 0
@ -439,8 +440,8 @@ async def test_modern_to_percentage(hass, start_state):
set_percentage_mode = async_mock_service(hass, "fan", "set_percentage") set_percentage_mode = async_mock_service(hass, "fan", "set_percentage")
set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode") set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode")
await hass.helpers.state.async_reproduce_state( await async_reproduce_state(
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PERCENTAGE15_STATE)] hass, [State(MODERN_FAN_ENTITY, "on", MODERN_FAN_ON_PERCENTAGE15_STATE)]
) )
assert len(turn_on_calls) == 0 assert len(turn_on_calls) == 0
@ -467,8 +468,9 @@ async def test_modern_direction(hass):
set_percentage_mode = async_mock_service(hass, "fan", "set_percentage") set_percentage_mode = async_mock_service(hass, "fan", "set_percentage")
set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode") set_preset_mode = async_mock_service(hass, "fan", "set_preset_mode")
await hass.helpers.state.async_reproduce_state( await async_reproduce_state(
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_PRESET_MODE_AUTO_REVERSE_STATE)] hass,
[State(MODERN_FAN_ENTITY, "on", MODERN_FAN_PRESET_MODE_AUTO_REVERSE_STATE)],
) )
assert len(turn_on_calls) == 0 assert len(turn_on_calls) == 0

View File

@ -49,7 +49,7 @@ from homeassistant.const import (
STATE_ON, STATE_ON,
) )
from homeassistant.core import HomeAssistantError, State from homeassistant.core import HomeAssistantError, State
from homeassistant.helpers import device_registry, entity_registry as er from homeassistant.helpers import device_registry, entity_registry as er, instance_id
from homeassistant.helpers.entityfilter import ( from homeassistant.helpers.entityfilter import (
CONF_EXCLUDE_DOMAINS, CONF_EXCLUDE_DOMAINS,
CONF_EXCLUDE_ENTITIES, CONF_EXCLUDE_ENTITIES,
@ -201,7 +201,7 @@ async def test_homekit_setup(hass, hk_driver, mock_async_zeroconf):
hass.states.async_set("light.demo", "on") hass.states.async_set("light.demo", "on")
hass.states.async_set("light.demo2", "on") hass.states.async_set("light.demo2", "on")
zeroconf_mock = MagicMock() zeroconf_mock = MagicMock()
uuid = await hass.helpers.instance_id.async_get() uuid = await instance_id.async_get(hass)
with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver: with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver:
await hass.async_add_executor_job(homekit.setup, zeroconf_mock, uuid) await hass.async_add_executor_job(homekit.setup, zeroconf_mock, uuid)
@ -245,7 +245,7 @@ async def test_homekit_setup_ip_address(hass, hk_driver, mock_async_zeroconf):
) )
path = get_persist_fullpath_for_entry_id(hass, entry.entry_id) path = get_persist_fullpath_for_entry_id(hass, entry.entry_id)
uuid = await hass.helpers.instance_id.async_get() uuid = await instance_id.async_get(hass)
with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver: with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver:
await hass.async_add_executor_job(homekit.setup, mock_async_zeroconf, uuid) await hass.async_add_executor_job(homekit.setup, mock_async_zeroconf, uuid)
mock_driver.assert_called_with( mock_driver.assert_called_with(
@ -287,7 +287,7 @@ async def test_homekit_setup_advertise_ip(hass, hk_driver, mock_async_zeroconf):
async_zeroconf_instance = MagicMock() async_zeroconf_instance = MagicMock()
path = get_persist_fullpath_for_entry_id(hass, entry.entry_id) path = get_persist_fullpath_for_entry_id(hass, entry.entry_id)
uuid = await hass.helpers.instance_id.async_get() uuid = await instance_id.async_get(hass)
with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver: with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver:
await hass.async_add_executor_job(homekit.setup, async_zeroconf_instance, uuid) await hass.async_add_executor_job(homekit.setup, async_zeroconf_instance, uuid)
mock_driver.assert_called_with( mock_driver.assert_called_with(

View File

@ -88,16 +88,12 @@ async def test_migrate_transient_devices(
) )
assert ( assert (
len( len(er.async_entries_for_device(entity_registry, device_id=plexweb_device.id))
hass.helpers.entity_registry.async_entries_for_device(
entity_registry, device_id=plexweb_device.id
)
)
== 1 == 1
) )
assert ( assert (
len( len(
hass.helpers.entity_registry.async_entries_for_device( er.async_entries_for_device(
entity_registry, device_id=non_plexweb_device.id entity_registry, device_id=non_plexweb_device.id
) )
) )
@ -113,16 +109,12 @@ async def test_migrate_transient_devices(
) )
assert ( assert (
len( len(er.async_entries_for_device(entity_registry, device_id=plexweb_device.id))
hass.helpers.entity_registry.async_entries_for_device(
entity_registry, device_id=plexweb_device.id
)
)
== 0 == 0
) )
assert ( assert (
len( len(
hass.helpers.entity_registry.async_entries_for_device( er.async_entries_for_device(
entity_registry, device_id=non_plexweb_device.id entity_registry, device_id=non_plexweb_device.id
) )
) )
@ -130,7 +122,7 @@ async def test_migrate_transient_devices(
) )
assert ( assert (
len( len(
hass.helpers.entity_registry.async_entries_for_device( er.async_entries_for_device(
entity_registry, device_id=plex_service_device.id entity_registry, device_id=plex_service_device.id
) )
) )

View File

@ -1,10 +1,12 @@
"""Tests for instance ID helper.""" """Tests for instance ID helper."""
from unittest.mock import patch from unittest.mock import patch
from homeassistant.helpers import instance_id
async def test_get_id_empty(hass, hass_storage): async def test_get_id_empty(hass, hass_storage):
"""Get unique ID.""" """Get unique ID."""
uuid = await hass.helpers.instance_id.async_get() uuid = await instance_id.async_get(hass)
assert uuid is not None assert uuid is not None
# Assert it's stored # Assert it's stored
assert hass_storage["core.uuid"]["data"]["uuid"] == uuid assert hass_storage["core.uuid"]["data"]["uuid"] == uuid
@ -15,7 +17,7 @@ async def test_get_id_migrate(hass, hass_storage):
with patch( with patch(
"homeassistant.util.json.load_json", return_value={"uuid": "1234"} "homeassistant.util.json.load_json", return_value={"uuid": "1234"}
), patch("os.path.isfile", return_value=True), patch("os.remove") as mock_remove: ), patch("os.path.isfile", return_value=True), patch("os.remove") as mock_remove:
uuid = await hass.helpers.instance_id.async_get() uuid = await instance_id.async_get(hass)
assert uuid == "1234" assert uuid == "1234"

View File

@ -3,6 +3,7 @@ from unittest.mock import Mock
from homeassistant.helpers.integration_platform import ( from homeassistant.helpers.integration_platform import (
async_process_integration_platform_for_component, async_process_integration_platform_for_component,
async_process_integration_platforms,
) )
from homeassistant.setup import ATTR_COMPONENT, EVENT_COMPONENT_LOADED from homeassistant.setup import ATTR_COMPONENT, EVENT_COMPONENT_LOADED
@ -24,8 +25,8 @@ async def test_process_integration_platforms(hass):
"""Process platform.""" """Process platform."""
processed.append((domain, platform)) processed.append((domain, platform))
await hass.helpers.integration_platform.async_process_integration_platforms( await async_process_integration_platforms(
"platform_to_check", _process_platform hass, "platform_to_check", _process_platform
) )
assert len(processed) == 1 assert len(processed) == 1
@ -40,9 +41,7 @@ async def test_process_integration_platforms(hass):
assert processed[1][1] == event_platform assert processed[1][1] == event_platform
# Verify we only process the platform once if we call it manually # Verify we only process the platform once if we call it manually
await hass.helpers.integration_platform.async_process_integration_platform_for_component( await async_process_integration_platform_for_component(hass, "event")
hass, "event"
)
assert len(processed) == 2 assert len(processed) == 2

View File

@ -3,11 +3,12 @@ import json
from unittest.mock import patch from unittest.mock import patch
from homeassistant.const import __version__ as current_version from homeassistant.const import __version__ as current_version
from homeassistant.helpers.system_info import async_get_system_info
async def test_get_system_info(hass): async def test_get_system_info(hass):
"""Test the get system info.""" """Test the get system info."""
info = await hass.helpers.system_info.async_get_system_info() info = await async_get_system_info(hass)
assert isinstance(info, dict) assert isinstance(info, dict)
assert info["version"] == current_version assert info["version"] == current_version
assert info["user"] is not None assert info["user"] is not None
@ -19,18 +20,18 @@ async def test_container_installationtype(hass):
with patch("platform.system", return_value="Linux"), patch( with patch("platform.system", return_value="Linux"), patch(
"os.path.isfile", return_value=True "os.path.isfile", return_value=True
), patch("homeassistant.helpers.system_info.getuser", return_value="root"): ), patch("homeassistant.helpers.system_info.getuser", return_value="root"):
info = await hass.helpers.system_info.async_get_system_info() info = await async_get_system_info(hass)
assert info["installation_type"] == "Home Assistant Container" assert info["installation_type"] == "Home Assistant Container"
with patch("platform.system", return_value="Linux"), patch( with patch("platform.system", return_value="Linux"), patch(
"os.path.isfile", side_effect=lambda file: file == "/.dockerenv" "os.path.isfile", side_effect=lambda file: file == "/.dockerenv"
), patch("homeassistant.helpers.system_info.getuser", return_value="user"): ), patch("homeassistant.helpers.system_info.getuser", return_value="user"):
info = await hass.helpers.system_info.async_get_system_info() info = await async_get_system_info(hass)
assert info["installation_type"] == "Unsupported Third Party Container" assert info["installation_type"] == "Unsupported Third Party Container"
async def test_getuser_keyerror(hass): async def test_getuser_keyerror(hass):
"""Test getuser keyerror.""" """Test getuser keyerror."""
with patch("homeassistant.helpers.system_info.getuser", side_effect=KeyError): with patch("homeassistant.helpers.system_info.getuser", side_effect=KeyError):
info = await hass.helpers.system_info.async_get_system_info() info = await async_get_system_info(hass)
assert info["user"] is None assert info["user"] is None