2017-02-09 05:58:45 +00:00
|
|
|
"""Signal handling related helpers."""
|
|
|
|
import logging
|
|
|
|
import signal
|
|
|
|
|
|
|
|
from homeassistant.const import RESTART_EXIT_CODE
|
2019-12-09 15:42:10 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2017-10-08 15:17:54 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
2017-02-09 05:58:45 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2017-10-08 15:17:54 +00:00
|
|
|
@bind_hass
|
2018-07-31 14:00:17 +00:00
|
|
|
def async_register_signal_handling(hass: HomeAssistant) -> None:
|
2017-02-09 05:58:45 +00:00
|
|
|
"""Register system signal handler for core."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2022-01-13 19:41:11 +00: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)
|
|
|
|
hass.async_create_task(hass.async_stop(exit_code))
|
|
|
|
|
|
|
|
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")
|