2021-05-31 23:35:31 +00:00
|
|
|
"""Test starting HA helpers."""
|
2024-03-08 18:16:38 +00:00
|
|
|
|
2023-02-20 10:42:56 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-10-05 10:24:51 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STARTED
|
2023-02-08 07:51:43 +00:00
|
|
|
from homeassistant.core import CoreState, HomeAssistant, callback
|
2021-05-31 23:35:31 +00:00
|
|
|
from homeassistant.helpers import start
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_at_start_when_running_awaitable(hass: HomeAssistant) -> None:
|
2021-05-31 23:35:31 +00:00
|
|
|
"""Test at start when already running."""
|
2024-01-12 09:21:26 +00:00
|
|
|
assert hass.state is CoreState.running
|
2021-05-31 23:35:31 +00:00
|
|
|
assert hass.is_running
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
start.async_at_start(hass, cb_at_start)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
2024-01-18 18:41:32 +00:00
|
|
|
hass.set_state(CoreState.starting)
|
2022-01-05 17:04:09 +00:00
|
|
|
assert hass.is_running
|
|
|
|
|
|
|
|
start.async_at_start(hass, cb_at_start)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 2
|
|
|
|
|
|
|
|
|
2023-02-20 10:42:56 +00:00
|
|
|
async def test_at_start_when_running_callback(
|
|
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2022-01-05 17:04:09 +00:00
|
|
|
"""Test at start when already running."""
|
2024-01-12 09:21:26 +00:00
|
|
|
assert hass.state is CoreState.running
|
2022-01-05 17:04:09 +00:00
|
|
|
assert hass.is_running
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
@callback
|
2022-01-05 17:04:09 +00:00
|
|
|
def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
2022-05-02 14:41:14 +00:00
|
|
|
start.async_at_start(hass, cb_at_start)()
|
2022-01-05 17:04:09 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
|
2024-01-18 18:41:32 +00:00
|
|
|
hass.set_state(CoreState.starting)
|
2022-01-05 17:04:09 +00:00
|
|
|
assert hass.is_running
|
|
|
|
|
2022-05-02 14:41:14 +00:00
|
|
|
start.async_at_start(hass, cb_at_start)()
|
2022-01-05 17:04:09 +00:00
|
|
|
assert len(calls) == 2
|
2021-05-31 23:35:31 +00:00
|
|
|
|
2022-05-02 14:41:14 +00:00
|
|
|
# Check the unnecessary cancel did not generate warnings or errors
|
|
|
|
for record in caplog.records:
|
|
|
|
assert record.levelname in ("DEBUG", "INFO")
|
|
|
|
|
2022-01-05 17:04:09 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_at_start_when_starting_awaitable(hass: HomeAssistant) -> None:
|
2021-05-31 23:35:31 +00:00
|
|
|
"""Test at start when yet to start."""
|
2024-01-18 18:41:32 +00:00
|
|
|
hass.set_state(CoreState.not_running)
|
2021-05-31 23:35:31 +00:00
|
|
|
assert not hass.is_running
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
start.async_at_start(hass, cb_at_start)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 1
|
2022-01-05 17:04:09 +00:00
|
|
|
|
|
|
|
|
2023-02-20 10:42:56 +00:00
|
|
|
async def test_at_start_when_starting_callback(
|
|
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2022-01-05 17:04:09 +00:00
|
|
|
"""Test at start when yet to start."""
|
2024-01-18 18:41:32 +00:00
|
|
|
hass.set_state(CoreState.not_running)
|
2022-01-05 17:04:09 +00:00
|
|
|
assert not hass.is_running
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
@callback
|
2022-01-05 17:04:09 +00:00
|
|
|
def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
2022-05-02 14:41:14 +00:00
|
|
|
cancel = start.async_at_start(hass, cb_at_start)
|
2022-01-05 17:04:09 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 1
|
2022-05-02 14:41:14 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
# Check the unnecessary cancel did not generate warnings or errors
|
|
|
|
for record in caplog.records:
|
|
|
|
assert record.levelname in ("DEBUG", "INFO")
|
|
|
|
|
|
|
|
|
2023-02-20 10:42:56 +00:00
|
|
|
async def test_cancelling_at_start_when_running(
|
|
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2022-05-02 14:41:14 +00:00
|
|
|
"""Test cancelling at start when already running."""
|
2024-01-12 09:21:26 +00:00
|
|
|
assert hass.state is CoreState.running
|
2022-05-02 14:41:14 +00:00
|
|
|
assert hass.is_running
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
start.async_at_start(hass, cb_at_start)()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
# Check the unnecessary cancel did not generate warnings or errors
|
|
|
|
for record in caplog.records:
|
|
|
|
assert record.levelname in ("DEBUG", "INFO")
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_cancelling_at_start_when_starting(hass: HomeAssistant) -> None:
|
2022-05-02 14:41:14 +00:00
|
|
|
"""Test cancelling at start when yet to start."""
|
2024-01-18 18:41:32 +00:00
|
|
|
hass.set_state(CoreState.not_running)
|
2022-05-02 14:41:14 +00:00
|
|
|
assert not hass.is_running
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
@callback
|
2022-05-02 14:41:14 +00:00
|
|
|
def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
start.async_at_start(hass, cb_at_start)()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
2022-10-05 10:24:51 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_at_started_when_running_awaitable(hass: HomeAssistant) -> None:
|
2022-10-05 10:24:51 +00:00
|
|
|
"""Test at started when already started."""
|
2024-01-12 09:21:26 +00:00
|
|
|
assert hass.state is CoreState.running
|
2022-10-05 10:24:51 +00:00
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
start.async_at_started(hass, cb_at_start)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
# Test the job is not run if state is CoreState.starting
|
2024-01-18 18:41:32 +00:00
|
|
|
hass.set_state(CoreState.starting)
|
2022-10-05 10:24:51 +00:00
|
|
|
|
|
|
|
start.async_at_started(hass, cb_at_start)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-20 10:42:56 +00:00
|
|
|
async def test_at_started_when_running_callback(
|
|
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2022-10-05 10:24:51 +00:00
|
|
|
"""Test at started when already running."""
|
2024-01-12 09:21:26 +00:00
|
|
|
assert hass.state is CoreState.running
|
2022-10-05 10:24:51 +00:00
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
@callback
|
2022-10-05 10:24:51 +00:00
|
|
|
def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
start.async_at_started(hass, cb_at_start)()
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
# Test the job is not run if state is CoreState.starting
|
2024-01-18 18:41:32 +00:00
|
|
|
hass.set_state(CoreState.starting)
|
2022-10-05 10:24:51 +00:00
|
|
|
|
|
|
|
start.async_at_started(hass, cb_at_start)()
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
# Check the unnecessary cancel did not generate warnings or errors
|
|
|
|
for record in caplog.records:
|
|
|
|
assert record.levelname in ("DEBUG", "INFO")
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_at_started_when_starting_awaitable(hass: HomeAssistant) -> None:
|
2022-10-05 10:24:51 +00:00
|
|
|
"""Test at started when yet to start."""
|
2024-01-18 18:41:32 +00:00
|
|
|
hass.set_state(CoreState.not_running)
|
2022-10-05 10:24:51 +00:00
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
start.async_at_started(hass, cb_at_start)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-20 10:42:56 +00:00
|
|
|
async def test_at_started_when_starting_callback(
|
|
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2022-10-05 10:24:51 +00:00
|
|
|
"""Test at started when yet to start."""
|
2024-01-18 18:41:32 +00:00
|
|
|
hass.set_state(CoreState.not_running)
|
2022-10-05 10:24:51 +00:00
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
@callback
|
2022-10-05 10:24:51 +00:00
|
|
|
def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
cancel = start.async_at_started(hass, cb_at_start)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
# Check the unnecessary cancel did not generate warnings or errors
|
|
|
|
for record in caplog.records:
|
|
|
|
assert record.levelname in ("DEBUG", "INFO")
|
|
|
|
|
|
|
|
|
2023-02-20 10:42:56 +00:00
|
|
|
async def test_cancelling_at_started_when_running(
|
|
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2022-10-05 10:24:51 +00:00
|
|
|
"""Test cancelling at start when already running."""
|
2024-01-12 09:21:26 +00:00
|
|
|
assert hass.state is CoreState.running
|
2022-10-05 10:24:51 +00:00
|
|
|
assert hass.is_running
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
start.async_at_started(hass, cb_at_start)()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
# Check the unnecessary cancel did not generate warnings or errors
|
|
|
|
for record in caplog.records:
|
|
|
|
assert record.levelname in ("DEBUG", "INFO")
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_cancelling_at_started_when_starting(hass: HomeAssistant) -> None:
|
2022-10-05 10:24:51 +00:00
|
|
|
"""Test cancelling at start when yet to start."""
|
2024-01-18 18:41:32 +00:00
|
|
|
hass.set_state(CoreState.not_running)
|
2022-10-05 10:24:51 +00:00
|
|
|
assert not hass.is_running
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
@callback
|
2022-10-05 10:24:51 +00:00
|
|
|
def cb_at_start(hass):
|
|
|
|
"""Home Assistant is started."""
|
|
|
|
calls.append(1)
|
|
|
|
|
|
|
|
start.async_at_started(hass, cb_at_start)()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls) == 0
|