Small speed up to checking core state (#107845)

This commit is contained in:
J. Nick Koston 2024-01-18 08:41:32 -10:00 committed by GitHub
parent 32b0bf6b4e
commit c399cab427
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
68 changed files with 176 additions and 165 deletions

View file

@ -393,16 +393,23 @@ class HomeAssistant:
self._stop_future: concurrent.futures.Future[None] | None = None
self._shutdown_jobs: list[HassJobWithArgs] = []
@property
@cached_property
def is_running(self) -> bool:
"""Return if Home Assistant is running."""
return self.state in (CoreState.starting, CoreState.running)
@property
@cached_property
def is_stopping(self) -> bool:
"""Return if Home Assistant is stopping."""
return self.state in (CoreState.stopping, CoreState.final_write)
def set_state(self, state: CoreState) -> None:
"""Set the current state."""
self.state = state
for prop in ("is_running", "is_stopping"):
with suppress(AttributeError):
delattr(self, prop)
def start(self) -> int:
"""Start Home Assistant.
@ -451,7 +458,7 @@ class HomeAssistant:
_LOGGER.info("Starting Home Assistant")
setattr(self.loop, "_thread_ident", threading.get_ident())
self.state = CoreState.starting
self.set_state(CoreState.starting)
self.bus.async_fire(EVENT_CORE_CONFIG_UPDATE)
self.bus.async_fire(EVENT_HOMEASSISTANT_START)
@ -483,7 +490,7 @@ class HomeAssistant:
)
return
self.state = CoreState.running
self.set_state(CoreState.running)
self.bus.async_fire(EVENT_CORE_CONFIG_UPDATE)
self.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
@ -894,7 +901,7 @@ class HomeAssistant:
self.exit_code = exit_code
self.state = CoreState.stopping
self.set_state(CoreState.stopping)
self.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
try:
async with self.timeout.async_timeout(STOP_STAGE_SHUTDOWN_TIMEOUT):
@ -907,7 +914,7 @@ class HomeAssistant:
self._async_log_running_tasks("stop integrations")
# Stage 3 - Final write
self.state = CoreState.final_write
self.set_state(CoreState.final_write)
self.bus.async_fire(EVENT_HOMEASSISTANT_FINAL_WRITE)
try:
async with self.timeout.async_timeout(FINAL_WRITE_STAGE_SHUTDOWN_TIMEOUT):
@ -920,7 +927,7 @@ class HomeAssistant:
self._async_log_running_tasks("final write")
# Stage 4 - Close
self.state = CoreState.not_running
self.set_state(CoreState.not_running)
self.bus.async_fire(EVENT_HOMEASSISTANT_CLOSE)
# Make a copy of running_tasks since a task can finish
@ -971,7 +978,7 @@ class HomeAssistant:
)
self._async_log_running_tasks("close")
self.state = CoreState.stopped
self.set_state(CoreState.stopped)
if self._stopped is not None:
self._stopped.set()

View file

@ -285,7 +285,7 @@ async def async_test_home_assistant(event_loop, load_registries=True):
)
hass.data[bootstrap.DATA_REGISTRIES_LOADED] = None
hass.state = CoreState.running
hass.set_state(CoreState.running)
@callback
def clear_instance(event):

View file

@ -735,9 +735,12 @@ async def test_proactive_mode_filter_states(
"off",
{"friendly_name": "Test Contact Sensor", "device_class": "door"},
)
with patch.object(hass, "state", core.CoreState.stopping):
await hass.async_block_till_done()
await hass.async_block_till_done()
current_state = hass.state
hass.set_state(core.CoreState.stopping)
await hass.async_block_till_done()
await hass.async_block_till_done()
hass.set_state(current_state)
assert len(aioclient_mock.mock_calls) == 0
# unsupported entity should not report

View file

@ -340,7 +340,7 @@ async def test_restored_state(
)
# Home assistant is not running yet
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
mock_restore_cache_with_extra_data(
hass,
[

View file

@ -1202,7 +1202,7 @@ async def test_initial_value_off(hass: HomeAssistant) -> None:
async def test_initial_value_on(hass: HomeAssistant) -> None:
"""Test initial value on."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
calls = async_mock_service(hass, "test", "automation")
assert await async_setup_component(
@ -1231,7 +1231,7 @@ async def test_initial_value_on(hass: HomeAssistant) -> None:
async def test_initial_value_off_but_restore_on(hass: HomeAssistant) -> None:
"""Test initial value off and restored state is turned on."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
calls = async_mock_service(hass, "test", "automation")
mock_restore_cache(hass, (State("automation.hello", STATE_ON),))
@ -1328,7 +1328,7 @@ async def test_automation_is_on_if_no_initial_state_or_restore(
async def test_automation_not_trigger_on_bootstrap(hass: HomeAssistant) -> None:
"""Test if automation is not trigger on bootstrap."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
calls = async_mock_service(hass, "test", "automation")
assert await async_setup_component(
@ -2460,7 +2460,7 @@ async def test_recursive_automation_starting_script(
await asyncio.wait_for(script_done_event.wait(), 10)
# Trigger 1st stage script shutdown
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
hass.bus.async_fire("homeassistant_stop")
await asyncio.wait_for(stop_scripts_at_shutdown_called.wait(), 10)
@ -2521,7 +2521,7 @@ async def test_recursive_automation(
await asyncio.wait_for(service_called.wait(), 1)
# Trigger 1st stage script shutdown
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
hass.bus.async_fire("homeassistant_stop")
await asyncio.wait_for(stop_scripts_at_shutdown_called.wait(), 1)

View file

@ -439,7 +439,7 @@ async def test_no_polling_after_stop_event(
assert needs_poll_calls == 1
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
await hass.async_block_till_done()
assert needs_poll_calls == 1

View file

@ -436,7 +436,7 @@ async def test_no_polling_after_stop_event(
assert async_handle_update.mock_calls[0] == call({"testdata": 0}, False)
assert async_handle_update.mock_calls[1] == call({"testdata": 1})
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
await hass.async_block_till_done()
assert needs_poll_calls == 1

View file

@ -553,7 +553,7 @@ async def test_no_updates_once_stopping(
inject_bluetooth_service_info(hass, GENERIC_BLUETOOTH_SERVICE_INFO)
assert len(all_events) == 1
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
# We should stop processing events once hass is stopping
inject_bluetooth_service_info(hass, GENERIC_BLUETOOTH_SERVICE_INFO_2)

View file

@ -206,7 +206,7 @@ async def test_polling_stops_at_the_stop_event(hass: HomeAssistant) -> None:
assert hass.states.get("fan.name_1").state == STATE_UNAVAILABLE
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
await hass.async_block_till_done()
with patch_bond_device_state(return_value={"power": 1, "speed": 1}):

View file

@ -120,7 +120,7 @@ async def test_unload_config_entry(hass: HomeAssistant) -> None:
async def test_delay_load_during_startup(hass: HomeAssistant) -> None:
"""Test delayed loading of a config entry during startup."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entry = MockConfigEntry(domain=DOMAIN, data={CONF_HOST: HOST, CONF_PORT: PORT})
entry.add_to_hass(hass)

View file

@ -551,7 +551,7 @@ async def test_alexa_config_migrate_expose_entity_prefs(
alexa_settings_version: int,
) -> None:
"""Test migrating Alexa entity config."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(hass, "homeassistant", {})
hass.states.async_set("light.state_only", "on")
@ -649,7 +649,7 @@ async def test_alexa_config_migrate_expose_entity_prefs_v2_no_exposed(
entity_registry: er.EntityRegistry,
) -> None:
"""Test migrating Alexa entity config from v2 to v3 when no entity is exposed."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(hass, "homeassistant", {})
hass.states.async_set("light.state_only", "on")
@ -696,7 +696,7 @@ async def test_alexa_config_migrate_expose_entity_prefs_v2_exposed(
entity_registry: er.EntityRegistry,
) -> None:
"""Test migrating Alexa entity config from v2 to v3 when an entity is exposed."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(hass, "homeassistant", {})
hass.states.async_set("light.state_only", "on")
@ -744,7 +744,7 @@ async def test_alexa_config_migrate_expose_entity_prefs_default_none(
entity_registry: er.EntityRegistry,
) -> None:
"""Test migrating Alexa entity config."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(hass, "homeassistant", {})
entity_default = entity_registry.async_get_or_create(
@ -782,7 +782,7 @@ async def test_alexa_config_migrate_expose_entity_prefs_default(
entity_registry: er.EntityRegistry,
) -> None:
"""Test migrating Alexa entity config."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(hass, "homeassistant", {})

View file

@ -236,7 +236,7 @@ async def test_google_entity_registry_sync(
assert len(mock_sync.mock_calls) == 3
# When hass is not started yet we wait till started
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
hass.bus.async_fire(
er.EVENT_ENTITY_REGISTRY_UPDATED,
{"action": "create", "entity_id": entry.entity_id},
@ -338,7 +338,7 @@ async def test_sync_google_on_home_assistant_start(
config = CloudGoogleConfig(
hass, GACTIONS_SCHEMA({}), "mock-user-id", cloud_prefs, hass.data["cloud"]
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
with patch.object(config, "async_sync_entities_all") as mock_sync:
await config.async_initialize()
await config.async_connect_agent_user("mock-user-id")
@ -498,7 +498,7 @@ async def test_google_config_migrate_expose_entity_prefs(
google_settings_version: int,
) -> None:
"""Test migrating Google entity config."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(hass, "homeassistant", {})
hass.states.async_set("light.state_only", "on")
@ -611,7 +611,7 @@ async def test_google_config_migrate_expose_entity_prefs_v2_no_exposed(
entity_registry: er.EntityRegistry,
) -> None:
"""Test migrating Google entity config from v2 to v3 when no entity is exposed."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(hass, "homeassistant", {})
hass.states.async_set("light.state_only", "on")
@ -658,7 +658,7 @@ async def test_google_config_migrate_expose_entity_prefs_v2_exposed(
entity_registry: er.EntityRegistry,
) -> None:
"""Test migrating Google entity config from v2 to v3 when an entity is exposed."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(hass, "homeassistant", {})
hass.states.async_set("light.state_only", "on")
@ -705,7 +705,7 @@ async def test_google_config_migrate_expose_entity_prefs_default_none(
entity_registry: er.EntityRegistry,
) -> None:
"""Test migrating Google entity config."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(hass, "homeassistant", {})
entity_default = entity_registry.async_get_or_create(
@ -742,7 +742,7 @@ async def test_google_config_migrate_expose_entity_prefs_default(
entity_registry: er.EntityRegistry,
) -> None:
"""Test migrating Google entity config."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(hass, "homeassistant", {})

View file

@ -282,7 +282,7 @@ async def test_initial_state_overrules_restore_state(hass: HomeAssistant) -> Non
hass, (State("counter.test1", "11"), State("counter.test2", "-22"))
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,
@ -315,7 +315,7 @@ async def test_restore_state_overrules_initial_state(hass: HomeAssistant) -> Non
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass, DOMAIN, {DOMAIN: {"test1": {}, "test2": {CONF_INITIAL: 10}, "test3": {}}}
@ -332,7 +332,7 @@ async def test_restore_state_overrules_initial_state(hass: HomeAssistant) -> Non
async def test_no_initial_state_and_no_restore_state(hass: HomeAssistant) -> None:
"""Ensure that entity is create without initial and restore feature."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(hass, DOMAIN, {DOMAIN: {"test1": {CONF_STEP: 5}}})

View file

@ -238,7 +238,7 @@ async def test_lights_turn_on_when_coming_home_after_sun_set_person(
async def test_initialize_start(hass: HomeAssistant) -> None:
"""Test we initialize when HA starts."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
assert await async_setup_component(
hass,
device_sun_light_trigger.DOMAIN,

View file

@ -64,11 +64,12 @@ async def test_delayed_speedtest_during_startup(
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.fastdotcom.coordinator.fast_com"
), patch.object(hass, "state", CoreState.starting):
original_state = hass.state
hass.set_state(CoreState.starting)
with patch("homeassistant.components.fastdotcom.coordinator.fast_com"):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
hass.set_state(original_state)
assert config_entry.state == config_entries.ConfigEntryState.LOADED
state = hass.states.get("sensor.fast_com_download")

View file

@ -1376,7 +1376,7 @@ async def test_restore_state(hass: HomeAssistant) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,
@ -1414,7 +1414,7 @@ async def test_restore_state_target_humidity(hass: HomeAssistant) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,
@ -1457,7 +1457,7 @@ async def test_restore_state_and_return_to_normal(hass: HomeAssistant) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,
@ -1512,7 +1512,7 @@ async def test_no_restore_state(hass: HomeAssistant) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,

View file

@ -1317,7 +1317,7 @@ async def test_restore_state(hass: HomeAssistant, hvac_mode) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,
@ -1355,7 +1355,7 @@ async def test_no_restore_state(hass: HomeAssistant) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,
@ -1432,7 +1432,7 @@ async def test_restore_will_turn_off_(hass: HomeAssistant) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(
hass, input_boolean.DOMAIN, {"input_boolean": {"test": None}}
@ -1480,7 +1480,7 @@ async def test_restore_will_turn_off_when_loaded_second(hass: HomeAssistant) ->
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await hass.async_block_till_done()
assert hass.states.get(heater_switch) is None

View file

@ -386,7 +386,7 @@ async def test_state_missing_entity_id(hass: HomeAssistant, setup_comp) -> None:
async def test_setup_before_started(hass: HomeAssistant) -> None:
"""Test we can setup before starting."""
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
assert await async_setup_component(hass, DOMAIN, CONFIG_MISSING_FAN)
await hass.async_block_till_done()

View file

@ -1139,7 +1139,7 @@ async def test_group_alarm(hass: HomeAssistant) -> None:
hass.states.async_set("alarm_control_panel.one", "armed_away")
hass.states.async_set("alarm_control_panel.two", "armed_home")
hass.states.async_set("alarm_control_panel.three", "armed_away")
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
assert await async_setup_component(
hass,
@ -1187,7 +1187,7 @@ async def test_group_vacuum_off(hass: HomeAssistant) -> None:
hass.states.async_set("vacuum.one", "docked")
hass.states.async_set("vacuum.two", "off")
hass.states.async_set("vacuum.three", "off")
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
assert await async_setup_component(
hass,
@ -1280,7 +1280,7 @@ async def test_switch_removed(hass: HomeAssistant) -> None:
hass.states.async_set("switch.two", "off")
hass.states.async_set("switch.three", "on")
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
assert await async_setup_component(
hass,
"group",
@ -1409,7 +1409,7 @@ async def test_group_that_references_a_group_of_lights(hass: HomeAssistant) -> N
"light.living_front_ri",
"light.living_back_lef",
]
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
for entity_id in entity_ids:
hass.states.async_set(entity_id, "off")
@ -1443,7 +1443,7 @@ async def test_group_that_references_a_group_of_covers(hass: HomeAssistant) -> N
"cover.living_front_ri",
"cover.living_back_lef",
]
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
for entity_id in entity_ids:
hass.states.async_set(entity_id, "closed")
@ -1479,7 +1479,7 @@ async def test_group_that_references_two_groups_of_covers(hass: HomeAssistant) -
"cover.living_front_ri",
"cover.living_back_lef",
]
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
for entity_id in entity_ids:
hass.states.async_set(entity_id, "closed")
@ -1523,7 +1523,7 @@ async def test_group_that_references_two_types_of_groups(hass: HomeAssistant) ->
"device_tracker.living_front_ri",
"device_tracker.living_back_lef",
]
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
for entity_id in group_1_entity_ids:
hass.states.async_set(entity_id, "closed")

View file

@ -58,7 +58,7 @@ def hassio_stubs(hassio_env, hass, hass_client, aioclient_mock):
), patch(
"homeassistant.components.hassio.HassIO.refresh_updates",
):
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
hass.loop.run_until_complete(async_setup_component(hass, "hassio", {}))
return hass_api.call_args[0][1]

View file

@ -486,7 +486,7 @@ async def test_route_not_found(
async def test_restore_state(hass: HomeAssistant) -> None:
"""Test sensor restore state."""
# Home assistant is not running yet
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
last_reset = "2022-11-29T00:00:00.000000+00:00"
mock_restore_cache_with_extra_data(
hass,

View file

@ -31,7 +31,7 @@ async def test_if_fires_on_hass_start(
) -> None:
"""Test the firing when Home Assistant starts."""
calls = async_mock_service(hass, "test", "automation")
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
assert await async_setup_component(hass, automation.DOMAIN, hass_config)
assert automation.is_on(hass, "automation.hello")
@ -54,7 +54,7 @@ async def test_if_fires_on_hass_start(
async def test_if_fires_on_hass_shutdown(hass: HomeAssistant) -> None:
"""Test the firing when Home Assistant shuts down."""
calls = async_mock_service(hass, "test", "automation")
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
assert await async_setup_component(
hass,

View file

@ -610,7 +610,7 @@ async def test_windowcovering_basic_restore(
hass: HomeAssistant, entity_registry: er.EntityRegistry, hk_driver, events
) -> None:
"""Test setting up an entity from state in the event registry."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entity_registry.async_get_or_create(
"cover",
@ -648,7 +648,7 @@ async def test_windowcovering_restore(
hass: HomeAssistant, entity_registry: er.EntityRegistry, hk_driver, events
) -> None:
"""Test setting up an entity from state in the event entity_registry."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entity_registry.async_get_or_create(
"cover",

View file

@ -557,7 +557,7 @@ async def test_fan_restore(
hass: HomeAssistant, entity_registry: er.EntityRegistry, hk_driver, events
) -> None:
"""Test setting up an entity from state in the event registry."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entity_registry.async_get_or_create(
"fan",

View file

@ -580,7 +580,7 @@ async def test_light_restore(
hass: HomeAssistant, entity_registry: er.EntityRegistry, hk_driver, events
) -> None:
"""Test setting up an entity from state in the event registry."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entity_registry.async_get_or_create(
"light", "hue", "1234", suggested_object_id="simple"

View file

@ -432,7 +432,7 @@ async def test_tv_restore(
hass: HomeAssistant, entity_registry: er.EntityRegistry, hk_driver, events
) -> None:
"""Test setting up an entity from state in the event registry."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entity_registry.async_get_or_create(
"media_player",

View file

@ -545,7 +545,7 @@ async def test_sensor_restore(
hass: HomeAssistant, entity_registry: er.EntityRegistry, hk_driver, events
) -> None:
"""Test setting up an entity from state in the event registry."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entity_registry.async_get_or_create(
"sensor",

View file

@ -971,7 +971,7 @@ async def test_thermostat_restore(
hass: HomeAssistant, entity_registry: er.EntityRegistry, hk_driver, events
) -> None:
"""Test setting up an entity from state in the event registry."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entity_registry.async_get_or_create(
"climate", "generic", "1234", suggested_object_id="simple"
@ -1801,7 +1801,7 @@ async def test_water_heater_restore(
hass: HomeAssistant, entity_registry: er.EntityRegistry, hk_driver, events
) -> None:
"""Test setting up an entity from state in the event registry."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entity_registry.async_get_or_create(
"water_heater", "generic", "1234", suggested_object_id="simple"

View file

@ -133,7 +133,7 @@ async def test_restore_state(hass: HomeAssistant) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
mock_component(hass, "recorder")
await async_setup_component(hass, DOMAIN, {DOMAIN: {"b1": None, "b2": None}})
@ -153,7 +153,7 @@ async def test_initial_state_overrules_restore_state(hass: HomeAssistant) -> Non
hass, (State("input_boolean.b1", "on"), State("input_boolean.b2", "off"))
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,

View file

@ -96,7 +96,7 @@ async def test_restore_state(hass: HomeAssistant) -> None:
(State("input_button.b1", "2021-01-01T23:59:59+00:00"),),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
mock_component(hass, "recorder")
await async_setup_component(hass, DOMAIN, {DOMAIN: {"b1": None, "b2": None}})

View file

@ -325,7 +325,7 @@ async def test_restore_state(hass: HomeAssistant) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
initial = datetime.datetime(2017, 1, 1, 23, 42)
default = datetime.datetime.combine(datetime.date.today(), DEFAULT_TIME)

View file

@ -238,7 +238,7 @@ async def test_restore_state(hass: HomeAssistant) -> None:
hass, (State("input_number.b1", "70"), State("input_number.b2", "200"))
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,
@ -261,7 +261,7 @@ async def test_restore_invalid_state(hass: HomeAssistant) -> None:
hass, (State("input_number.b1", "="), State("input_number.b2", "200"))
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,
@ -284,7 +284,7 @@ async def test_initial_state_overrules_restore_state(hass: HomeAssistant) -> Non
hass, (State("input_number.b1", "70"), State("input_number.b2", "200"))
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,
@ -308,7 +308,7 @@ async def test_initial_state_overrules_restore_state(hass: HomeAssistant) -> Non
async def test_no_initial_state_and_no_restore_state(hass: HomeAssistant) -> None:
"""Ensure that entity is create without initial and restore feature."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(hass, DOMAIN, {DOMAIN: {"b1": {"min": 0, "max": 100}}})

View file

@ -165,7 +165,7 @@ async def test_restore_state(hass: HomeAssistant) -> None:
(State("input_text.b1", "test"), State("input_text.b2", "testing too long")),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(
hass, DOMAIN, {DOMAIN: {"b1": None, "b2": {"min": 0, "max": 10}}}
@ -187,7 +187,7 @@ async def test_initial_state_overrules_restore_state(hass: HomeAssistant) -> Non
(State("input_text.b1", "testing"), State("input_text.b2", "testing too long")),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(
hass,
@ -211,7 +211,7 @@ async def test_initial_state_overrules_restore_state(hass: HomeAssistant) -> Non
async def test_no_initial_state_and_no_restore_state(hass: HomeAssistant) -> None:
"""Ensure that entity is create without initial and restore feature."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(hass, DOMAIN, {DOMAIN: {"b1": {"min": 0, "max": 100}}})

View file

@ -1221,7 +1221,7 @@ async def test_restore_state(hass: HomeAssistant, expected_state) -> None:
"""Ensure state is restored on startup."""
mock_restore_cache(hass, (State("alarm_control_panel.test", expected_state),))
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
mock_component(hass, "recorder")
assert await async_setup_component(
@ -1266,7 +1266,7 @@ async def test_restore_state_arming(hass: HomeAssistant, expected_state) -> None
hass, (State(entity_id, expected_state, attributes, last_updated=time),)
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
mock_component(hass, "recorder")
assert await async_setup_component(
@ -1323,7 +1323,7 @@ async def test_restore_state_pending(hass: HomeAssistant, previous_state) -> Non
(State(entity_id, STATE_ALARM_TRIGGERED, attributes, last_updated=time),),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
mock_component(hass, "recorder")
assert await async_setup_component(
@ -1388,7 +1388,7 @@ async def test_restore_state_triggered(hass: HomeAssistant, previous_state) -> N
(State(entity_id, STATE_ALARM_TRIGGERED, attributes, last_updated=time),),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
mock_component(hass, "recorder")
assert await async_setup_component(
@ -1434,7 +1434,7 @@ async def test_restore_state_triggered_long_ago(hass: HomeAssistant) -> None:
(State(entity_id, STATE_ALARM_TRIGGERED, attributes, last_updated=time),),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
mock_component(hass, "recorder")
assert await async_setup_component(

View file

@ -2452,7 +2452,7 @@ async def test_delayed_birth_message(
"""Test sending birth message does not happen until Home Assistant starts."""
mqtt_mock = await mqtt_mock_entry()
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
birth = asyncio.Event()
await hass.async_block_till_done()

View file

@ -315,7 +315,7 @@ async def test_default_entity_and_device_name(
"""
events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await hass.async_block_till_done()
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "mock-broker"})

View file

@ -140,7 +140,7 @@ async def test_waiting_for_client_not_loaded(
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test waiting for client while mqtt entry is not yet loaded."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await hass.async_block_till_done()
entry = MockConfigEntry(
@ -199,7 +199,7 @@ async def test_waiting_for_client_entry_fails(
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test waiting for client where mqtt entry is failing."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await hass.async_block_till_done()
entry = MockConfigEntry(
@ -227,7 +227,7 @@ async def test_waiting_for_client_setup_fails(
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test waiting for client where mqtt entry is failing during setup."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await hass.async_block_till_done()
entry = MockConfigEntry(
@ -254,7 +254,7 @@ async def test_waiting_for_client_timeout(
hass: HomeAssistant,
) -> None:
"""Test waiting for client with timeout."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await hass.async_block_till_done()
entry = MockConfigEntry(
@ -273,7 +273,7 @@ async def test_waiting_for_client_with_disabled_entry(
hass: HomeAssistant,
) -> None:
"""Test waiting for client with timeout."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await hass.async_block_till_done()
entry = MockConfigEntry(

View file

@ -58,7 +58,7 @@ async def test_setup_and_stop_waits_for_ha(
e_id = "fake.entity"
# HA is not running
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
assert await add_statestream(hass, base_topic="pub")
await hass.async_block_till_done()

View file

@ -310,7 +310,7 @@ async def test_setup_component_with_delay(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test setup of the netatmo component with delayed startup."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
with patch(
"pyatmo.AbstractAsyncAuth.async_addwebhook", side_effect=AsyncMock()

View file

@ -195,7 +195,7 @@ async def test_options_flow(hass: HomeAssistant, mock_get_source_ip) -> None:
},
)
config_entry.add_to_hass(hass)
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

View file

@ -99,7 +99,7 @@ async def test_valid_invalid_user_ids(
async def test_setup_tracker(hass: HomeAssistant, hass_admin_user: MockUser) -> None:
"""Test set up person with one device tracker."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
user_id = hass_admin_user.id
config = {
DOMAIN: {
@ -159,7 +159,7 @@ async def test_setup_two_trackers(
hass: HomeAssistant, hass_admin_user: MockUser
) -> None:
"""Test set up person with two device trackers."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
user_id = hass_admin_user.id
config = {
DOMAIN: {
@ -247,7 +247,7 @@ async def test_ignore_unavailable_states(
hass: HomeAssistant, hass_admin_user: MockUser
) -> None:
"""Test set up person with two device trackers, one unavailable."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
user_id = hass_admin_user.id
config = {
DOMAIN: {
@ -302,7 +302,7 @@ async def test_restore_home_state(
}
state = State("person.tracked_person", "home", attrs)
mock_restore_cache(hass, (state,))
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
mock_component(hass, "recorder")
config = {
DOMAIN: {

View file

@ -137,7 +137,7 @@ async def test_shutdown_before_startup_finishes(
recorder.CONF_DB_URL: recorder_db_url,
recorder.CONF_COMMIT_INTERVAL: 1,
}
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
recorder_helper.async_initialize_recorder(hass)
hass.create_task(async_setup_recorder_instance(hass, config))
@ -168,7 +168,7 @@ async def test_canceled_before_startup_finishes(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test recorder shuts down when its startup future is canceled out from under it."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
recorder_helper.async_initialize_recorder(hass)
hass.create_task(async_setup_recorder_instance(hass))
await recorder_helper.async_wait_recorder(hass)
@ -192,7 +192,7 @@ async def test_shutdown_closes_connections(
) -> None:
"""Test shutdown closes connections."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
instance = get_instance(hass)
await instance.async_db_ready
@ -219,7 +219,7 @@ async def test_state_gets_saved_when_set_before_start_event(
) -> None:
"""Test we can record an event when starting with not running."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
recorder_helper.async_initialize_recorder(hass)
hass.create_task(async_setup_recorder_instance(hass))

View file

@ -193,7 +193,7 @@ async def test_restore_state(hass: HomeAssistant, monkeypatch) -> None:
hass, (State(f"{DOMAIN}.test", STATE_ON), State(f"{DOMAIN}.test2", STATE_ON))
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
# setup mocking rflink module
_, _, _, _ = await mock_rflink(hass, CONFIG, DOMAIN, monkeypatch)

View file

@ -349,7 +349,7 @@ async def test_restore_state(hass: HomeAssistant, monkeypatch) -> None:
hass, (State(f"{DOMAIN}.c1", STATE_OPEN), State(f"{DOMAIN}.c2", STATE_CLOSED))
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
# setup mocking rflink module
_, _, _, _ = await mock_rflink(hass, config, DOMAIN, monkeypatch)

View file

@ -575,7 +575,7 @@ async def test_restore_state(hass: HomeAssistant, monkeypatch) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
# setup mocking rflink module
_, _, _, _ = await mock_rflink(hass, config, DOMAIN, monkeypatch)

View file

@ -263,7 +263,7 @@ async def test_restore_state(hass: HomeAssistant, monkeypatch) -> None:
hass, (State(f"{DOMAIN}.s1", STATE_ON), State(f"{DOMAIN}.s2", STATE_OFF))
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
# setup mocking rflink module
_, _, _, _ = await mock_rflink(hass, config, DOMAIN, monkeypatch)

View file

@ -1152,7 +1152,7 @@ async def test_script_restore_last_triggered(hass: HomeAssistant) -> None:
State("script.last_triggered", STATE_OFF, {"last_triggered": time}),
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert await async_setup_component(
hass,
@ -1373,7 +1373,7 @@ async def test_recursive_script_turn_on(
await asyncio.wait_for(service_called.wait(), 1)
# Trigger 1st stage script shutdown
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
hass.bus.async_fire("homeassistant_stop")
await asyncio.wait_for(stop_scripts_at_shutdown_called.wait(), 1)

View file

@ -52,7 +52,7 @@ def test_compile_missing_statistics(
start_time = three_days_ago + timedelta(days=3)
freezer.move_to(three_days_ago)
hass: HomeAssistant = get_test_home_assistant()
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
recorder_helper.async_initialize_recorder(hass)
setup_component(hass, "sensor", {})
setup_component(hass, "recorder", {"recorder": config})
@ -90,7 +90,7 @@ def test_compile_missing_statistics(
hass.stop()
freezer.move_to(start_time)
hass: HomeAssistant = get_test_home_assistant()
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
recorder_helper.async_initialize_recorder(hass)
setup_component(hass, "sensor", {})
hass.states.set("sensor.test1", "0", POWER_SENSOR_ATTRIBUTES)

View file

@ -764,7 +764,7 @@ async def test_no_update_template_match_all(
) -> None:
"""Test that we do not update sensors that match on all."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
await setup.async_setup_component(
hass,

View file

@ -507,7 +507,7 @@ async def test_no_template_match_all(
"""Test that we allow static templates."""
hass.states.async_set("sensor.test_sensor", "startup")
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
await async_setup_component(
hass,
@ -752,7 +752,7 @@ async def test_this_variable_early_hass_not_running(
"""
entity_id = "sensor.none_false"
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
# Setup template
with assert_setup_component(count, domain):

View file

@ -525,7 +525,7 @@ async def test_restore_state(hass: HomeAssistant) -> None:
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
mock_component(hass, "recorder")
await async_setup_component(

View file

@ -147,7 +147,7 @@ async def test_config_options(hass: HomeAssistant) -> None:
async def test_methods_and_events(hass: HomeAssistant) -> None:
"""Test methods and events."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(hass, DOMAIN, {DOMAIN: {"test1": {CONF_DURATION: 10}}})
@ -393,7 +393,7 @@ async def test_start_service(hass: HomeAssistant) -> None:
async def test_wait_till_timer_expires(hass: HomeAssistant) -> None:
"""Test for a timer to end."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(hass, DOMAIN, {DOMAIN: {"test1": {CONF_DURATION: 20}}})
@ -460,7 +460,7 @@ async def test_wait_till_timer_expires(hass: HomeAssistant) -> None:
async def test_no_initial_state_and_no_restore_state(hass: HomeAssistant) -> None:
"""Ensure that entity is create without initial and restore feature."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(hass, DOMAIN, {DOMAIN: {"test1": {CONF_DURATION: 10}}})
@ -569,7 +569,7 @@ async def test_config_reload(
async def test_timer_restarted_event(hass: HomeAssistant) -> None:
"""Ensure restarted event is called after starting a paused or running timer."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(hass, DOMAIN, {DOMAIN: {"test1": {CONF_DURATION: 10}}})
@ -636,7 +636,7 @@ async def test_timer_restarted_event(hass: HomeAssistant) -> None:
async def test_state_changed_when_timer_restarted(hass: HomeAssistant) -> None:
"""Ensure timer's state changes when it restarted."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
await async_setup_component(hass, DOMAIN, {DOMAIN: {"test1": {CONF_DURATION: 10}}})

View file

@ -426,7 +426,7 @@ async def test_restore_traces(
hass: HomeAssistant, hass_storage: dict[str, Any], hass_ws_client, domain
) -> None:
"""Test restored traces."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
id = 1
def next_id():
@ -598,7 +598,7 @@ async def test_restore_traces_overflow(
num_restored_moon_traces,
) -> None:
"""Test restored traces are evicted first."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
id = 1
trace_uuids = []
@ -679,7 +679,7 @@ async def test_restore_traces_late_overflow(
restored_run_id,
) -> None:
"""Test restored traces are evicted first."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
id = 1
trace_uuids = []

View file

@ -534,7 +534,7 @@ async def test_restore_state(
) -> None:
"""Test utility sensor restore state."""
# Home assistant is not runnit yet
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
last_reset = "2020-12-21T00:00:00.013073+00:00"
@ -865,7 +865,7 @@ async def test_delta_values(
) -> None:
"""Test utility meter "delta_values" mode."""
# Home assistant is not runnit yet
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
now = dt_util.utcnow()
with freeze_time(now):
@ -974,7 +974,7 @@ async def test_non_periodically_resetting(
) -> None:
"""Test utility meter "non periodically resetting" mode."""
# Home assistant is not runnit yet
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
now = dt_util.utcnow()
with freeze_time(now):

View file

@ -49,7 +49,7 @@ async def test_dryer_sensor_values(
entity_registry: er.EntityRegistry,
) -> None:
"""Test the sensor value callbacks."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
thetimestamp: datetime = datetime(2022, 11, 29, 00, 00, 00, 00, UTC)
mock_restore_cache_with_extra_data(
hass,
@ -113,7 +113,7 @@ async def test_washer_sensor_values(
entity_registry: er.EntityRegistry,
) -> None:
"""Test the sensor value callbacks."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
thetimestamp: datetime = datetime(2022, 11, 29, 00, 00, 00, 00, UTC)
mock_restore_cache_with_extra_data(
hass,
@ -280,7 +280,7 @@ async def test_restore_state(
) -> None:
"""Test sensor restore state."""
# Home assistant is not running yet
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
thetimestamp: datetime = datetime(2022, 11, 29, 00, 00, 00, 00, UTC)
mock_restore_cache_with_extra_data(
hass,
@ -333,7 +333,7 @@ async def test_callback(
mock_sensor1_api: MagicMock,
) -> None:
"""Test callback timestamp callback function."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
thetimestamp: datetime = datetime(2022, 11, 29, 00, 00, 00, 00, UTC)
mock_restore_cache_with_extra_data(
hass,

View file

@ -685,7 +685,7 @@ async def test_shade_restore_state(
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
zha_device = await zha_device_restored(zigpy_shade_device)
entity_id = find_entity_id(Platform.COVER, zha_device, hass)
@ -711,7 +711,7 @@ async def test_cover_restore_state(
),
)
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
zha_device = await zha_device_restored(zigpy_cover_device)
entity_id = find_entity_id(Platform.COVER, zha_device, hass)

View file

@ -279,7 +279,7 @@ async def test_shutdown_on_ha_stop(
zha_data.gateway, "shutdown", wraps=zha_data.gateway.shutdown
) as mock_shutdown:
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
await hass.async_block_till_done()
assert len(mock_shutdown.mock_calls) == 1

View file

@ -327,14 +327,14 @@ async def test_update_entity_ha_not_running(
assert len(client.async_send_command.call_args_list) == 1
# Update should be delayed by a day because HA is not running
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5))
await hass.async_block_till_done()
assert len(client.async_send_command.call_args_list) == 1
hass.state = CoreState.running
hass.set_state(CoreState.running)
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5, days=1))
await hass.async_block_till_done()

View file

@ -1408,7 +1408,7 @@ async def test_cleanup_device_registry_removes_expired_orphaned_devices(
async def test_cleanup_startup(hass: HomeAssistant) -> None:
"""Test we run a cleanup on startup."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
with patch(
"homeassistant.helpers.device_registry.Debouncer.async_call"

View file

@ -38,7 +38,7 @@ async def test_async_create_flow_deferred_until_started(
hass: HomeAssistant, mock_flow_init
) -> None:
"""Test flows are deferred until started."""
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
discovery_flow.async_create_flow(
hass,
"hue",
@ -79,7 +79,7 @@ async def test_async_create_flow_checks_existing_flows_before_startup(
hass: HomeAssistant, mock_flow_init
) -> None:
"""Test existing flows prevent an identical ones from being created before startup."""
hass.state = CoreState.stopped
hass.set_state(CoreState.stopped)
for _ in range(2):
discovery_flow.async_create_flow(
hass,
@ -104,7 +104,7 @@ async def test_async_create_flow_does_nothing_after_stop(
"""Test we no longer create flows when hass is stopping."""
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
mock_flow_init.reset_mock()
discovery_flow.async_create_flow(
hass,

View file

@ -937,7 +937,7 @@ async def test_reset_cancels_retry_setup(hass: HomeAssistant) -> None:
async def test_reset_cancels_retry_setup_when_not_started(hass: HomeAssistant) -> None:
"""Test that resetting a platform will cancel scheduled a setup retry when not yet started."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
async_setup_entry = Mock(side_effect=PlatformNotReady)
initial_listeners = hass.bus.async_listeners()[EVENT_HOMEASSISTANT_STARTED]

View file

@ -870,7 +870,7 @@ async def test_restore_states(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test restoring states."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entity_registry.async_get_or_create(
"light",
@ -936,7 +936,7 @@ async def test_async_get_device_class_lookup(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test registry device class lookup."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
entity_registry.async_get_or_create(
"binary_sensor",

View file

@ -210,7 +210,7 @@ async def test_save_persistent_states(hass: HomeAssistant) -> None:
async def test_hass_starting(hass: HomeAssistant) -> None:
"""Test that we cache data."""
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
now = dt_util.utcnow()
stored_states = [
@ -224,7 +224,7 @@ async def test_hass_starting(hass: HomeAssistant) -> None:
await data.store.async_save([state.as_dict() for state in stored_states])
# Emulate a fresh load
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
hass.data.pop(DATA_RESTORE_STATE)
await async_load(hass)
data = async_get(hass)

View file

@ -4340,7 +4340,7 @@ async def test_shutdown_after(
script_obj = script.Script(hass, sequence, "test script", "test_domain")
delay_started_flag = async_watch_for_action(script_obj, delay_alias)
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
hass.bus.async_fire("homeassistant_stop")
await hass.async_block_till_done()
@ -4379,7 +4379,7 @@ async def test_start_script_after_shutdown(
script_obj = script.Script(hass, sequence, "test script", "test_domain")
# Trigger 1st stage script shutdown
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
hass.bus.async_fire("homeassistant_stop")
await hass.async_block_till_done()
# Trigger 2nd stage script shutdown

View file

@ -21,7 +21,7 @@ async def test_at_start_when_running_awaitable(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
assert len(calls) == 1
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert hass.is_running
start.async_at_start(hass, cb_at_start)
@ -46,7 +46,7 @@ async def test_at_start_when_running_callback(
start.async_at_start(hass, cb_at_start)()
assert len(calls) == 1
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
assert hass.is_running
start.async_at_start(hass, cb_at_start)()
@ -59,7 +59,7 @@ async def test_at_start_when_running_callback(
async def test_at_start_when_starting_awaitable(hass: HomeAssistant) -> None:
"""Test at start when yet to start."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
assert not hass.is_running
calls = []
@ -81,7 +81,7 @@ async def test_at_start_when_starting_callback(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test at start when yet to start."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
assert not hass.is_running
calls = []
@ -130,7 +130,7 @@ async def test_cancelling_at_start_when_running(
async def test_cancelling_at_start_when_starting(hass: HomeAssistant) -> None:
"""Test cancelling at start when yet to start."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
assert not hass.is_running
calls = []
@ -164,7 +164,7 @@ async def test_at_started_when_running_awaitable(hass: HomeAssistant) -> None:
assert len(calls) == 1
# Test the job is not run if state is CoreState.starting
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
start.async_at_started(hass, cb_at_start)
await hass.async_block_till_done()
@ -188,7 +188,7 @@ async def test_at_started_when_running_callback(
assert len(calls) == 1
# Test the job is not run if state is CoreState.starting
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
start.async_at_started(hass, cb_at_start)()
assert len(calls) == 1
@ -200,7 +200,7 @@ async def test_at_started_when_running_callback(
async def test_at_started_when_starting_awaitable(hass: HomeAssistant) -> None:
"""Test at started when yet to start."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
calls = []
@ -225,7 +225,7 @@ async def test_at_started_when_starting_callback(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test at started when yet to start."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
calls = []
@ -277,7 +277,7 @@ async def test_cancelling_at_started_when_running(
async def test_cancelling_at_started_when_starting(hass: HomeAssistant) -> None:
"""Test cancelling at start when yet to start."""
hass.state = CoreState.not_running
hass.set_state(CoreState.not_running)
assert not hass.is_running
calls = []

View file

@ -140,7 +140,7 @@ async def test_saving_on_final_write(
assert store.key not in hass_storage
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
await hass.async_block_till_done()
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
@ -164,7 +164,7 @@ async def test_not_delayed_saving_while_stopping(
store = storage.Store(hass, MOCK_VERSION, MOCK_KEY)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
store.async_delay_save(lambda: MOCK_DATA, 1)
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=2))
@ -181,7 +181,7 @@ async def test_not_delayed_saving_after_stopping(
assert store.key not in hass_storage
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
await hass.async_block_till_done()
assert store.key not in hass_storage
@ -195,7 +195,7 @@ async def test_not_saving_while_stopping(
) -> None:
"""Test saves don't write when stopping Home Assistant."""
store = storage.Store(hass, MOCK_VERSION, MOCK_KEY)
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
await store.async_save(MOCK_DATA)
assert store.key not in hass_storage
@ -723,7 +723,7 @@ async def test_read_only_store(
assert read_only_store.key not in hass_storage
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
await hass.async_block_till_done()
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))

View file

@ -506,7 +506,7 @@ async def test_stop_refresh_on_ha_stop(
# Fire Home Assistant stop event
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
await hass.async_block_till_done()
# Make sure no update with subscriber after stop event

View file

@ -1084,7 +1084,7 @@ async def test_setup_retrying_during_unload(hass: HomeAssistant) -> None:
async def test_setup_retrying_during_unload_before_started(hass: HomeAssistant) -> None:
"""Test if we unload an entry that is in retry mode before started."""
entry = MockConfigEntry(domain="test")
hass.state = CoreState.starting
hass.set_state(CoreState.starting)
initial_listeners = hass.bus.async_listeners()[EVENT_HOMEASSISTANT_STARTED]
mock_setup_entry = AsyncMock(side_effect=ConfigEntryNotReady)
@ -1121,7 +1121,7 @@ async def test_setup_does_not_retry_during_shutdown(hass: HomeAssistant) -> None
assert entry.state is config_entries.ConfigEntryState.SETUP_RETRY
assert len(mock_setup_entry.mock_calls) == 1
hass.state = CoreState.stopping
hass.set_state(CoreState.stopping)
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5))
await hass.async_block_till_done()