25
25
from unittest .mock import AsyncMock , Mock , patch
26
26
27
27
from aiohttp .test_utils import unused_port as get_test_instance_port # noqa: F401
28
+ import voluptuous as vol
28
29
29
30
from homeassistant import auth , config_entries , core as ha , loader
30
31
from homeassistant .auth import (
47
48
STATE_OFF ,
48
49
STATE_ON ,
49
50
)
50
- from homeassistant .core import BLOCK_LOG_TIMEOUT , HomeAssistant
51
+ from homeassistant .core import BLOCK_LOG_TIMEOUT , HomeAssistant , ServiceCall , State
51
52
from homeassistant .helpers import (
52
53
area_registry ,
53
54
device_registry ,
62
63
)
63
64
from homeassistant .helpers .dispatcher import async_dispatcher_connect
64
65
from homeassistant .helpers .json import JSONEncoder
66
+ from homeassistant .helpers .typing import ConfigType
65
67
from homeassistant .setup import setup_component
66
68
from homeassistant .util .async_ import run_callback_threadsafe
67
69
import homeassistant .util .dt as date_util
@@ -333,7 +335,9 @@ def clear_instance(event):
333
335
return hass
334
336
335
337
336
- def async_mock_service (hass , domain , service , schema = None ):
338
+ def async_mock_service (
339
+ hass : HomeAssistant , domain : str , service : str , schema : vol .Schema | None = None
340
+ ) -> list [ServiceCall ]:
337
341
"""Set up a fake service & return a calls log list to this service."""
338
342
calls = []
339
343
@@ -423,18 +427,20 @@ def get_fixture_path(filename: str, integration: str | None = None) -> pathlib.P
423
427
424
428
if integration is None :
425
429
return pathlib .Path (start_path ).parent .joinpath ("fixtures" , filename )
426
- else :
427
- return pathlib .Path (start_path ).parent .joinpath (
428
- "components" , integration , "fixtures" , filename
429
- )
430
+
431
+ return pathlib .Path (start_path ).parent .joinpath (
432
+ "components" , integration , "fixtures" , filename
433
+ )
430
434
431
435
432
- def load_fixture (filename , integration = None ):
436
+ def load_fixture (filename : str , integration : str | None = None ) -> str :
433
437
"""Load a fixture."""
434
438
return get_fixture_path (filename , integration ).read_text ()
435
439
436
440
437
- def mock_state_change_event (hass , new_state , old_state = None ):
441
+ def mock_state_change_event (
442
+ hass : HomeAssistant , new_state : State , old_state : State | None = None
443
+ ) -> None :
438
444
"""Mock state change envent."""
439
445
event_data = {"entity_id" : new_state .entity_id , "new_state" : new_state }
440
446
@@ -445,15 +451,18 @@ def mock_state_change_event(hass, new_state, old_state=None):
445
451
446
452
447
453
@ha .callback
448
- def mock_component (hass , component ) :
454
+ def mock_component (hass : HomeAssistant , component : str ) -> None :
449
455
"""Mock a component is setup."""
450
456
if component in hass .config .components :
451
457
AssertionError (f"Integration { component } is already setup" )
452
458
453
459
hass .config .components .add (component )
454
460
455
461
456
- def mock_registry (hass , mock_entries = None ):
462
+ def mock_registry (
463
+ hass : HomeAssistant ,
464
+ mock_entries : dict [str , entity_registry .RegistryEntry ] | None = None ,
465
+ ) -> entity_registry .EntityRegistry :
457
466
"""Mock the Entity Registry."""
458
467
registry = entity_registry .EntityRegistry (hass )
459
468
if mock_entries is None :
@@ -466,7 +475,9 @@ def mock_registry(hass, mock_entries=None):
466
475
return registry
467
476
468
477
469
- def mock_area_registry (hass , mock_entries = None ):
478
+ def mock_area_registry (
479
+ hass : HomeAssistant , mock_entries : dict [str , area_registry .AreaEntry ] | None = None
480
+ ) -> area_registry .AreaRegistry :
470
481
"""Mock the Area Registry."""
471
482
registry = area_registry .AreaRegistry (hass )
472
483
registry .areas = mock_entries or OrderedDict ()
@@ -475,7 +486,10 @@ def mock_area_registry(hass, mock_entries=None):
475
486
return registry
476
487
477
488
478
- def mock_device_registry (hass , mock_entries = None ):
489
+ def mock_device_registry (
490
+ hass : HomeAssistant ,
491
+ mock_entries : dict [str , device_registry .DeviceEntry ] | None = None ,
492
+ ) -> device_registry .DeviceRegistry :
479
493
"""Mock the Device Registry."""
480
494
registry = device_registry .DeviceRegistry (hass )
481
495
registry .devices = device_registry .DeviceRegistryItems ()
@@ -551,7 +565,9 @@ def mock_policy(self, policy):
551
565
self ._permissions = auth_permissions .PolicyPermissions (policy , self .perm_lookup )
552
566
553
567
554
- async def register_auth_provider (hass , config ):
568
+ async def register_auth_provider (
569
+ hass : HomeAssistant , config : ConfigType
570
+ ) -> auth_providers .AuthProvider :
555
571
"""Register an auth provider."""
556
572
provider = await auth_providers .auth_provider_from_config (
557
573
hass , hass .auth ._store , config
@@ -915,17 +931,15 @@ async def mock_psc(hass, config_input, integration):
915
931
SetupRecorderInstanceT = Callable [..., Awaitable [recorder .Recorder ]]
916
932
917
933
918
- def init_recorder_component (hass , add_config = None ):
934
+ def init_recorder_component (hass , add_config = None , db_url = "sqlite://" ):
919
935
"""Initialize the recorder."""
920
936
config = dict (add_config ) if add_config else {}
921
937
if recorder .CONF_DB_URL not in config :
922
- config [recorder .CONF_DB_URL ] = "sqlite://" # In memory DB
938
+ config [recorder .CONF_DB_URL ] = db_url
923
939
if recorder .CONF_COMMIT_INTERVAL not in config :
924
940
config [recorder .CONF_COMMIT_INTERVAL ] = 0
925
941
926
- with patch ("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB" , True ), patch (
927
- "homeassistant.components.recorder.migration.migrate_schema"
928
- ):
942
+ with patch ("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB" , True ):
929
943
if recorder .DOMAIN not in hass .data :
930
944
recorder_helper .async_initialize_recorder (hass )
931
945
assert setup_component (hass , recorder .DOMAIN , {recorder .DOMAIN : config })
0 commit comments