Files
home-assistant-core/homeassistant/helpers/signal.py
T
Matthias Urlichs 0121e3cb04 Remove usage of "run_until_complete" (#16617)
* De-run_forever()-ization

* Use asyncio.run (or our own implementation on Python <3.7)
* hass.start is only used by tests
* setup_and_run_hass() is now async
* Add "main" async hass.run method
* move SIGINT handling to helpers/signal.py
  * add flag to .run to disable hass's signal handlers
* Teach async_start and async_stop to not step on each other
  (more than necessary)

* shorten over-long lines

* restore missing "import asyncio"

* move run_asyncio to homeassistant.util.async_

* LOGGER: warn => warning

* Add "force" flag to async_stop

only useful for testing

* Add 'attrs==18.2.0' to requirements_all.txt

Required for keeping requirements_test_all.txt in sync, where it is in
turn required to prevent auto-downgrading "attrs" during "pip install"

* Fixes for mypy

* Fix "mock_signal" fixture

* Revert mistaken edit

* Flake8 fixes

* mypy fixes

* pylint fix

* Revert adding attrs== to requirements_test*.txt

solved by using "pip -c"

* Rename "run" to "async_run", as per calling conventions
2018-09-19 15:40:02 +02:00

46 lines
1.4 KiB
Python

"""Signal handling related helpers."""
import logging
import signal
import sys
from homeassistant.core import callback, HomeAssistant
from homeassistant.const import RESTART_EXIT_CODE
from homeassistant.loader import bind_hass
_LOGGER = logging.getLogger(__name__)
@callback
@bind_hass
def async_register_signal_handling(hass: HomeAssistant) -> None:
"""Register system signal handler for core."""
if sys.platform != 'win32':
@callback
def async_signal_handle(exit_code):
"""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")