mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Do not wait for websocket response to be delivered before shutdown (#49323)
- Waiting was unreliable since the websocket response could take a few seconds to get delivered - Alternate frontend fix is https://github.com/home-assistant/frontend/pull/8932
This commit is contained in:
parent
343b8faf9b
commit
673f542cde
@ -21,7 +21,6 @@ from homeassistant.const import (
|
|||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
from homeassistant.exceptions import HomeAssistantError, Unauthorized, UnknownUser
|
from homeassistant.exceptions import HomeAssistantError, Unauthorized, UnknownUser
|
||||||
from homeassistant.helpers import config_validation as cv, recorder
|
from homeassistant.helpers import config_validation as cv, recorder
|
||||||
from homeassistant.helpers.event import async_call_later
|
|
||||||
from homeassistant.helpers.service import (
|
from homeassistant.helpers.service import (
|
||||||
async_extract_config_entry_ids,
|
async_extract_config_entry_ids,
|
||||||
async_extract_referenced_entity_ids,
|
async_extract_referenced_entity_ids,
|
||||||
@ -49,7 +48,6 @@ SCHEMA_RELOAD_CONFIG_ENTRY = vol.All(
|
|||||||
|
|
||||||
|
|
||||||
SHUTDOWN_SERVICES = (SERVICE_HOMEASSISTANT_STOP, SERVICE_HOMEASSISTANT_RESTART)
|
SHUTDOWN_SERVICES = (SERVICE_HOMEASSISTANT_STOP, SERVICE_HOMEASSISTANT_RESTART)
|
||||||
WEBSOCKET_RECEIVE_DELAY = 1
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: ha.HomeAssistant, config: dict) -> bool:
|
async def async_setup(hass: ha.HomeAssistant, config: dict) -> bool:
|
||||||
@ -143,15 +141,7 @@ async def async_setup(hass: ha.HomeAssistant, config: dict) -> bool:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if call.service == SERVICE_HOMEASSISTANT_STOP:
|
if call.service == SERVICE_HOMEASSISTANT_STOP:
|
||||||
# We delay the stop by WEBSOCKET_RECEIVE_DELAY to ensure the frontend
|
asyncio.create_task(hass.async_stop())
|
||||||
# can receive the response before the webserver shuts down
|
|
||||||
@ha.callback
|
|
||||||
def _async_stop(_):
|
|
||||||
# This must not be a tracked task otherwise
|
|
||||||
# the task itself will block stop
|
|
||||||
asyncio.create_task(hass.async_stop())
|
|
||||||
|
|
||||||
async_call_later(hass, WEBSOCKET_RECEIVE_DELAY, _async_stop)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
errors = await conf_util.async_check_ha_config_file(hass)
|
errors = await conf_util.async_check_ha_config_file(hass)
|
||||||
@ -172,19 +162,7 @@ async def async_setup(hass: ha.HomeAssistant, config: dict) -> bool:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if call.service == SERVICE_HOMEASSISTANT_RESTART:
|
if call.service == SERVICE_HOMEASSISTANT_RESTART:
|
||||||
# We delay the restart by WEBSOCKET_RECEIVE_DELAY to ensure the frontend
|
asyncio.create_task(hass.async_stop(RESTART_EXIT_CODE))
|
||||||
# can receive the response before the webserver shuts down
|
|
||||||
@ha.callback
|
|
||||||
def _async_stop_with_code(_):
|
|
||||||
# This must not be a tracked task otherwise
|
|
||||||
# the task itself will block restart
|
|
||||||
asyncio.create_task(hass.async_stop(RESTART_EXIT_CODE))
|
|
||||||
|
|
||||||
async_call_later(
|
|
||||||
hass,
|
|
||||||
WEBSOCKET_RECEIVE_DELAY,
|
|
||||||
_async_stop_with_code,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_handle_update_service(call):
|
async def async_handle_update_service(call):
|
||||||
"""Service handler for updating an entity."""
|
"""Service handler for updating an entity."""
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""The tests for Core components."""
|
"""The tests for Core components."""
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
@ -34,12 +33,10 @@ import homeassistant.core as ha
|
|||||||
from homeassistant.exceptions import HomeAssistantError, Unauthorized
|
from homeassistant.exceptions import HomeAssistantError, Unauthorized
|
||||||
from homeassistant.helpers import entity
|
from homeassistant.helpers import entity
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
|
||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
MockConfigEntry,
|
MockConfigEntry,
|
||||||
async_capture_events,
|
async_capture_events,
|
||||||
async_fire_time_changed,
|
|
||||||
async_mock_service,
|
async_mock_service,
|
||||||
get_test_home_assistant,
|
get_test_home_assistant,
|
||||||
mock_registry,
|
mock_registry,
|
||||||
@ -526,7 +523,6 @@ async def test_restart_homeassistant(hass):
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
assert mock_check.called
|
assert mock_check.called
|
||||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=2))
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert mock_restart.called
|
assert mock_restart.called
|
||||||
|
|
||||||
@ -545,6 +541,5 @@ async def test_stop_homeassistant(hass):
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
assert not mock_check.called
|
assert not mock_check.called
|
||||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=2))
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert mock_restart.called
|
assert mock_restart.called
|
||||||
|
Loading…
x
Reference in New Issue
Block a user