hass-core/homeassistant/helpers/signal.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

49 lines
1.4 KiB
Python
Raw Normal View History

"""Signal handling related helpers."""
import asyncio
import logging
import signal
from homeassistant.const import RESTART_EXIT_CODE
from homeassistant.core import HomeAssistant, callback
from homeassistant.loader import bind_hass
2024-05-07 18:24:13 +02:00
from homeassistant.util.hass_dict import HassKey
_LOGGER = logging.getLogger(__name__)
2024-05-07 18:24:13 +02:00
KEY_HA_STOP: HassKey[asyncio.Task[None]] = HassKey("homeassistant_stop")
@callback
@bind_hass
def async_register_signal_handling(hass: HomeAssistant) -> None:
"""Register system signal handler for core."""
2019-07-31 12:25:30 -07:00
2022-01-13 20:41:11 +01:00
@callback
def async_signal_handle(exit_code: int) -> None:
"""Wrap signal handling.
* queue call to shutdown task
* re-instate default handler
"""
hass.loop.remove_signal_handler(signal.SIGTERM)
hass.loop.remove_signal_handler(signal.SIGINT)
2024-05-07 18:24:13 +02:00
hass.data[KEY_HA_STOP] = asyncio.create_task(hass.async_stop(exit_code))
2022-01-13 20:41:11 +01:00
try:
hass.loop.add_signal_handler(signal.SIGTERM, async_signal_handle, 0)
except ValueError:
_LOGGER.warning("Could not bind to SIGTERM")
try:
hass.loop.add_signal_handler(signal.SIGINT, async_signal_handle, 0)
except ValueError:
_LOGGER.warning("Could not bind to SIGINT")
try:
hass.loop.add_signal_handler(
signal.SIGHUP, async_signal_handle, RESTART_EXIT_CODE
)
except ValueError:
_LOGGER.warning("Could not bind to SIGHUP")