2016-03-09 09:25:50 +00:00
|
|
|
"""Test to verify that Home Assistant core works."""
|
2022-05-14 19:12:08 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import array
|
2016-09-30 19:57:24 +00:00
|
|
|
import asyncio
|
2019-12-09 15:52:24 +00:00
|
|
|
from datetime import datetime, timedelta
|
2019-01-14 23:08:44 +00:00
|
|
|
import functools
|
2022-05-27 03:54:26 +00:00
|
|
|
import gc
|
2017-06-25 22:10:30 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from tempfile import TemporaryDirectory
|
2023-02-27 03:36:18 +00:00
|
|
|
import time
|
2022-05-27 03:54:26 +00:00
|
|
|
from typing import Any
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import MagicMock, Mock, PropertyMock, patch
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2016-12-16 05:30:09 +00:00
|
|
|
import pytest
|
2019-12-09 15:52:24 +00:00
|
|
|
import voluptuous as vol
|
2015-08-04 16:16:10 +00:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FRIENDLY_NAME,
|
2019-12-09 15:52:24 +00:00
|
|
|
CONF_UNIT_SYSTEM,
|
|
|
|
EVENT_CALL_SERVICE,
|
|
|
|
EVENT_CORE_CONFIG_UPDATE,
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_HOMEASSISTANT_CLOSE,
|
2020-03-30 17:18:39 +00:00
|
|
|
EVENT_HOMEASSISTANT_FINAL_WRITE,
|
2020-05-08 00:29:47 +00:00
|
|
|
EVENT_HOMEASSISTANT_START,
|
2020-06-15 22:22:53 +00:00
|
|
|
EVENT_HOMEASSISTANT_STARTED,
|
2019-12-09 15:52:24 +00:00
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_SERVICE_REGISTERED,
|
|
|
|
EVENT_SERVICE_REMOVED,
|
2019-12-09 15:52:24 +00:00
|
|
|
EVENT_STATE_CHANGED,
|
2020-06-15 22:22:53 +00:00
|
|
|
MATCH_ALL,
|
2019-12-09 15:52:24 +00:00
|
|
|
__version__,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-12-09 15:52:24 +00:00
|
|
|
import homeassistant.core as ha
|
2023-06-20 13:24:31 +00:00
|
|
|
from homeassistant.core import (
|
|
|
|
HassJob,
|
|
|
|
HomeAssistant,
|
|
|
|
ServiceCall,
|
|
|
|
ServiceResponse,
|
|
|
|
State,
|
|
|
|
SupportsResponse,
|
|
|
|
)
|
2020-11-16 17:25:55 +00:00
|
|
|
from homeassistant.exceptions import (
|
2023-06-16 16:43:35 +00:00
|
|
|
HomeAssistantError,
|
2020-11-16 17:25:55 +00:00
|
|
|
InvalidEntityFormatError,
|
|
|
|
InvalidStateError,
|
2021-04-08 18:46:28 +00:00
|
|
|
MaxLengthExceeded,
|
2020-11-16 17:25:55 +00:00
|
|
|
ServiceNotFound,
|
|
|
|
)
|
2019-12-09 15:52:24 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2022-02-04 22:45:25 +00:00
|
|
|
from homeassistant.util.read_only_dict import ReadOnlyDict
|
2019-12-09 15:52:24 +00:00
|
|
|
from homeassistant.util.unit_system import METRIC_SYSTEM
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2023-01-27 11:51:58 +00:00
|
|
|
from .common import async_capture_events, async_mock_service
|
2016-02-14 23:08:23 +00:00
|
|
|
|
2021-05-08 05:46:26 +00:00
|
|
|
PST = dt_util.get_time_zone("America/Los_Angeles")
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_split_entity_id() -> None:
|
2016-10-05 03:44:32 +00:00
|
|
|
"""Test split_entity_id."""
|
2022-02-19 00:11:17 +00:00
|
|
|
assert ha.split_entity_id("domain.object_id") == ("domain", "object_id")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ha.split_entity_id("")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ha.split_entity_id(".")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ha.split_entity_id("just_domain")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ha.split_entity_id("empty_object_id.")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ha.split_entity_id(".empty_domain")
|
2016-08-09 03:21:40 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_async_add_hass_job_schedule_callback() -> None:
|
2023-03-05 11:46:02 +00:00
|
|
|
"""Test that we schedule callbacks and add jobs to the job pool."""
|
2016-10-05 03:44:32 +00:00
|
|
|
hass = MagicMock()
|
|
|
|
job = MagicMock()
|
|
|
|
|
2020-10-07 14:51:50 +00:00
|
|
|
ha.HomeAssistant.async_add_hass_job(hass, ha.HassJob(ha.callback(job)))
|
2016-10-05 03:44:32 +00:00
|
|
|
assert len(hass.loop.call_soon.mock_calls) == 1
|
|
|
|
assert len(hass.loop.create_task.mock_calls) == 0
|
|
|
|
assert len(hass.add_job.mock_calls) == 0
|
|
|
|
|
|
|
|
|
2023-03-10 11:06:50 +00:00
|
|
|
def test_async_add_hass_job_coro_named(hass: HomeAssistant) -> None:
|
2023-03-05 11:46:02 +00:00
|
|
|
"""Test that we schedule coroutines and add jobs to the job pool with a name."""
|
|
|
|
|
|
|
|
async def mycoro():
|
|
|
|
pass
|
|
|
|
|
|
|
|
job = ha.HassJob(mycoro, "named coro")
|
|
|
|
assert "named coro" in str(job)
|
|
|
|
assert job.name == "named coro"
|
|
|
|
task = ha.HomeAssistant.async_add_hass_job(hass, job)
|
|
|
|
assert "named coro" in str(task)
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_async_add_hass_job_schedule_partial_callback() -> None:
|
2019-01-14 23:08:44 +00:00
|
|
|
"""Test that we schedule partial coros and add jobs to the job pool."""
|
2016-10-05 03:44:32 +00:00
|
|
|
hass = MagicMock()
|
|
|
|
job = MagicMock()
|
2019-01-14 23:08:44 +00:00
|
|
|
partial = functools.partial(ha.callback(job))
|
|
|
|
|
2020-10-07 14:51:50 +00:00
|
|
|
ha.HomeAssistant.async_add_hass_job(hass, ha.HassJob(partial))
|
2019-01-14 23:08:44 +00:00
|
|
|
assert len(hass.loop.call_soon.mock_calls) == 1
|
|
|
|
assert len(hass.loop.create_task.mock_calls) == 0
|
|
|
|
assert len(hass.add_job.mock_calls) == 0
|
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
def test_async_add_hass_job_schedule_coroutinefunction(event_loop) -> None:
|
2019-01-14 23:08:44 +00:00
|
|
|
"""Test that we schedule coroutines and add jobs to the job pool."""
|
2022-11-29 21:36:36 +00:00
|
|
|
hass = MagicMock(loop=MagicMock(wraps=event_loop))
|
2019-01-14 23:08:44 +00:00
|
|
|
|
|
|
|
async def job():
|
|
|
|
pass
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2020-10-07 14:51:50 +00:00
|
|
|
ha.HomeAssistant.async_add_hass_job(hass, ha.HassJob(job))
|
2016-10-05 03:44:32 +00:00
|
|
|
assert len(hass.loop.call_soon.mock_calls) == 0
|
|
|
|
assert len(hass.loop.create_task.mock_calls) == 1
|
|
|
|
assert len(hass.add_job.mock_calls) == 0
|
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
def test_async_add_hass_job_schedule_partial_coroutinefunction(event_loop) -> None:
|
2019-01-14 23:08:44 +00:00
|
|
|
"""Test that we schedule partial coros and add jobs to the job pool."""
|
2022-11-29 21:36:36 +00:00
|
|
|
hass = MagicMock(loop=MagicMock(wraps=event_loop))
|
2019-01-14 23:08:44 +00:00
|
|
|
|
|
|
|
async def job():
|
|
|
|
pass
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-01-14 23:08:44 +00:00
|
|
|
partial = functools.partial(job)
|
|
|
|
|
2020-10-07 14:51:50 +00:00
|
|
|
ha.HomeAssistant.async_add_hass_job(hass, ha.HassJob(partial))
|
2019-01-14 23:08:44 +00:00
|
|
|
assert len(hass.loop.call_soon.mock_calls) == 0
|
|
|
|
assert len(hass.loop.create_task.mock_calls) == 1
|
|
|
|
assert len(hass.add_job.mock_calls) == 0
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_async_add_job_add_hass_threaded_job_to_pool() -> None:
|
2016-10-05 03:44:32 +00:00
|
|
|
"""Test that we schedule coroutines and add jobs to the job pool."""
|
|
|
|
hass = MagicMock()
|
2019-01-14 23:08:44 +00:00
|
|
|
|
|
|
|
def job():
|
|
|
|
pass
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2020-10-07 14:51:50 +00:00
|
|
|
ha.HomeAssistant.async_add_hass_job(hass, ha.HassJob(job))
|
2016-10-05 03:44:32 +00:00
|
|
|
assert len(hass.loop.call_soon.mock_calls) == 0
|
|
|
|
assert len(hass.loop.create_task.mock_calls) == 0
|
2023-02-14 04:16:59 +00:00
|
|
|
assert len(hass.loop.run_in_executor.mock_calls) == 2
|
2016-10-05 03:44:32 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
def test_async_create_task_schedule_coroutine(event_loop) -> None:
|
2018-07-13 10:24:51 +00:00
|
|
|
"""Test that we schedule coroutines and add jobs to the job pool."""
|
2022-11-29 21:36:36 +00:00
|
|
|
hass = MagicMock(loop=MagicMock(wraps=event_loop))
|
2018-07-13 10:24:51 +00:00
|
|
|
|
2019-01-14 23:08:44 +00:00
|
|
|
async def job():
|
|
|
|
pass
|
|
|
|
|
|
|
|
ha.HomeAssistant.async_create_task(hass, job())
|
2018-07-13 10:24:51 +00:00
|
|
|
assert len(hass.loop.call_soon.mock_calls) == 0
|
|
|
|
assert len(hass.loop.create_task.mock_calls) == 1
|
|
|
|
assert len(hass.add_job.mock_calls) == 0
|
|
|
|
|
|
|
|
|
2023-03-05 11:46:02 +00:00
|
|
|
def test_async_create_task_schedule_coroutine_with_name(event_loop) -> None:
|
|
|
|
"""Test that we schedule coroutines and add jobs to the job pool with a name."""
|
|
|
|
hass = MagicMock(loop=MagicMock(wraps=event_loop))
|
|
|
|
|
|
|
|
async def job():
|
|
|
|
pass
|
|
|
|
|
|
|
|
task = ha.HomeAssistant.async_create_task(hass, job(), "named task")
|
|
|
|
assert len(hass.loop.call_soon.mock_calls) == 0
|
|
|
|
assert len(hass.loop.create_task.mock_calls) == 1
|
|
|
|
assert len(hass.add_job.mock_calls) == 0
|
|
|
|
assert "named task" in str(task)
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_async_run_hass_job_calls_callback() -> None:
|
2016-10-05 03:44:32 +00:00
|
|
|
"""Test that the callback annotation is respected."""
|
|
|
|
hass = MagicMock()
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def job():
|
|
|
|
calls.append(1)
|
|
|
|
|
2020-10-07 14:51:50 +00:00
|
|
|
ha.HomeAssistant.async_run_hass_job(hass, ha.HassJob(ha.callback(job)))
|
2016-10-05 03:44:32 +00:00
|
|
|
assert len(calls) == 1
|
|
|
|
assert len(hass.async_add_job.mock_calls) == 0
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_async_run_hass_job_delegates_non_async() -> None:
|
2016-10-05 03:44:32 +00:00
|
|
|
"""Test that the callback annotation is respected."""
|
|
|
|
hass = MagicMock()
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
def job():
|
|
|
|
calls.append(1)
|
|
|
|
|
2020-10-07 14:51:50 +00:00
|
|
|
ha.HomeAssistant.async_run_hass_job(hass, ha.HassJob(job))
|
2016-10-05 03:44:32 +00:00
|
|
|
assert len(calls) == 0
|
2020-10-07 14:51:50 +00:00
|
|
|
assert len(hass.async_add_hass_job.mock_calls) == 1
|
2016-08-09 03:21:40 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_stage_shutdown(hass: HomeAssistant) -> None:
|
2017-02-13 05:24:07 +00:00
|
|
|
"""Simulate a shutdown, test calling stuff."""
|
2020-11-16 14:43:48 +00:00
|
|
|
test_stop = async_capture_events(hass, EVENT_HOMEASSISTANT_STOP)
|
|
|
|
test_final_write = async_capture_events(hass, EVENT_HOMEASSISTANT_FINAL_WRITE)
|
|
|
|
test_close = async_capture_events(hass, EVENT_HOMEASSISTANT_CLOSE)
|
|
|
|
test_all = async_capture_events(hass, MATCH_ALL)
|
2017-02-13 05:24:07 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
await hass.async_stop()
|
2017-02-13 05:24:07 +00:00
|
|
|
|
|
|
|
assert len(test_stop) == 1
|
|
|
|
assert len(test_close) == 1
|
2020-03-30 17:18:39 +00:00
|
|
|
assert len(test_final_write) == 1
|
|
|
|
assert len(test_all) == 2
|
2017-02-13 05:24:07 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_stage_shutdown_with_exit_code(hass: HomeAssistant) -> None:
|
2023-02-18 01:56:02 +00:00
|
|
|
"""Simulate a shutdown, test calling stuff with exit code checks."""
|
|
|
|
test_stop = async_capture_events(hass, EVENT_HOMEASSISTANT_STOP)
|
|
|
|
test_final_write = async_capture_events(hass, EVENT_HOMEASSISTANT_FINAL_WRITE)
|
|
|
|
test_close = async_capture_events(hass, EVENT_HOMEASSISTANT_CLOSE)
|
|
|
|
test_all = async_capture_events(hass, MATCH_ALL)
|
|
|
|
|
|
|
|
event_call_counters = [0, 0, 0]
|
|
|
|
expected_exit_code = 101
|
|
|
|
|
|
|
|
async def async_on_stop(event) -> None:
|
|
|
|
if hass.exit_code == expected_exit_code:
|
|
|
|
event_call_counters[0] += 1
|
|
|
|
|
|
|
|
async def async_on_final_write(event) -> None:
|
|
|
|
if hass.exit_code == expected_exit_code:
|
|
|
|
event_call_counters[1] += 1
|
|
|
|
|
|
|
|
async def async_on_close(event) -> None:
|
|
|
|
if hass.exit_code == expected_exit_code:
|
|
|
|
event_call_counters[2] += 1
|
|
|
|
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_on_stop)
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_FINAL_WRITE, async_on_final_write)
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, async_on_close)
|
|
|
|
|
|
|
|
await hass.async_stop(expected_exit_code)
|
|
|
|
|
|
|
|
assert len(test_stop) == 1
|
|
|
|
assert len(test_close) == 1
|
|
|
|
assert len(test_final_write) == 1
|
|
|
|
assert len(test_all) == 2
|
|
|
|
|
|
|
|
assert (
|
|
|
|
event_call_counters[0] == 1
|
|
|
|
and event_call_counters[1] == 1
|
|
|
|
and event_call_counters[2] == 1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-02-01 09:54:39 +00:00
|
|
|
async def test_shutdown_calls_block_till_done_after_shutdown_run_callback_threadsafe(
|
2023-02-21 08:27:13 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2021-02-01 09:54:39 +00:00
|
|
|
"""Ensure shutdown_run_callback_threadsafe is called before the final async_block_till_done."""
|
|
|
|
stop_calls = []
|
|
|
|
|
|
|
|
async def _record_block_till_done():
|
|
|
|
nonlocal stop_calls
|
|
|
|
stop_calls.append("async_block_till_done")
|
|
|
|
|
|
|
|
def _record_shutdown_run_callback_threadsafe(loop):
|
|
|
|
nonlocal stop_calls
|
|
|
|
stop_calls.append(("shutdown_run_callback_threadsafe", loop))
|
|
|
|
|
|
|
|
with patch.object(hass, "async_block_till_done", _record_block_till_done), patch(
|
|
|
|
"homeassistant.core.shutdown_run_callback_threadsafe",
|
|
|
|
_record_shutdown_run_callback_threadsafe,
|
|
|
|
):
|
|
|
|
await hass.async_stop()
|
|
|
|
|
|
|
|
assert stop_calls[-2] == ("shutdown_run_callback_threadsafe", hass.loop)
|
|
|
|
assert stop_calls[-1] == "async_block_till_done"
|
|
|
|
|
|
|
|
|
2023-02-14 04:16:59 +00:00
|
|
|
async def test_pending_scheduler(hass: HomeAssistant) -> None:
|
2020-08-26 14:57:52 +00:00
|
|
|
"""Add a coro to pending tasks."""
|
|
|
|
call_count = []
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
async def test_coro():
|
|
|
|
"""Test Coro."""
|
|
|
|
call_count.append("call")
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
for _ in range(3):
|
|
|
|
hass.async_add_job(test_coro())
|
2014-11-23 20:57:29 +00:00
|
|
|
|
2023-02-14 04:16:59 +00:00
|
|
|
await asyncio.wait(hass._tasks)
|
2016-11-08 09:24:50 +00:00
|
|
|
|
2023-02-14 04:16:59 +00:00
|
|
|
assert len(hass._tasks) == 0
|
2020-08-26 14:57:52 +00:00
|
|
|
assert len(call_count) == 3
|
2016-11-08 09:24:50 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_async_add_job_pending_tasks_coro(hass: HomeAssistant) -> None:
|
2020-08-26 14:57:52 +00:00
|
|
|
"""Add a coro to pending tasks."""
|
|
|
|
call_count = []
|
2016-11-09 16:41:17 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
async def test_coro():
|
|
|
|
"""Test Coro."""
|
|
|
|
call_count.append("call")
|
2016-11-08 09:24:50 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
for _ in range(2):
|
|
|
|
hass.add_job(test_coro())
|
2016-11-08 09:24:50 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
async def wait_finish_callback():
|
|
|
|
"""Wait until all stuff is scheduled."""
|
|
|
|
await asyncio.sleep(0)
|
2021-05-17 19:54:06 +00:00
|
|
|
await asyncio.sleep(0)
|
|
|
|
|
|
|
|
await wait_finish_callback()
|
|
|
|
|
2023-02-14 04:16:59 +00:00
|
|
|
assert len(hass._tasks) == 2
|
2021-05-17 19:54:06 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(call_count) == 2
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_async_create_task_pending_tasks_coro(hass: HomeAssistant) -> None:
|
2021-05-17 19:54:06 +00:00
|
|
|
"""Add a coro to pending tasks."""
|
|
|
|
call_count = []
|
|
|
|
|
|
|
|
async def test_coro():
|
|
|
|
"""Test Coro."""
|
|
|
|
call_count.append("call")
|
|
|
|
|
|
|
|
for _ in range(2):
|
|
|
|
hass.create_task(test_coro())
|
|
|
|
|
|
|
|
async def wait_finish_callback():
|
|
|
|
"""Wait until all stuff is scheduled."""
|
|
|
|
await asyncio.sleep(0)
|
2020-08-26 14:57:52 +00:00
|
|
|
await asyncio.sleep(0)
|
2016-11-08 09:24:50 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
await wait_finish_callback()
|
2016-11-08 09:24:50 +00:00
|
|
|
|
2023-02-14 04:16:59 +00:00
|
|
|
assert len(hass._tasks) == 2
|
2020-08-26 14:57:52 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(call_count) == 2
|
2016-11-17 04:00:08 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_async_add_job_pending_tasks_executor(hass: HomeAssistant) -> None:
|
2020-08-26 14:57:52 +00:00
|
|
|
"""Run an executor in pending tasks."""
|
|
|
|
call_count = []
|
2016-11-08 09:24:50 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
def test_executor():
|
|
|
|
"""Test executor."""
|
|
|
|
call_count.append("call")
|
2016-11-09 04:01:05 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
async def wait_finish_callback():
|
|
|
|
"""Wait until all stuff is scheduled."""
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
await asyncio.sleep(0)
|
2016-11-09 04:01:05 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
for _ in range(2):
|
|
|
|
hass.async_add_job(test_executor)
|
2016-11-17 04:00:08 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
await wait_finish_callback()
|
2016-11-09 04:01:05 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(call_count) == 2
|
2016-11-17 04:00:08 +00:00
|
|
|
|
2016-11-09 04:01:05 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_async_add_job_pending_tasks_callback(hass: HomeAssistant) -> None:
|
2020-08-26 14:57:52 +00:00
|
|
|
"""Run a callback in pending tasks."""
|
|
|
|
call_count = []
|
2016-11-09 04:01:05 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
@ha.callback
|
|
|
|
def test_callback():
|
|
|
|
"""Test callback."""
|
|
|
|
call_count.append("call")
|
2016-11-09 04:01:05 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
async def wait_finish_callback():
|
|
|
|
"""Wait until all stuff is scheduled."""
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
await asyncio.sleep(0)
|
2016-11-17 04:00:08 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
for _ in range(2):
|
|
|
|
hass.async_add_job(test_callback)
|
2016-11-09 04:01:05 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
await wait_finish_callback()
|
2016-11-17 04:00:08 +00:00
|
|
|
|
2020-08-26 14:57:52 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2023-02-14 04:16:59 +00:00
|
|
|
assert len(hass._tasks) == 0
|
2020-08-26 14:57:52 +00:00
|
|
|
assert len(call_count) == 2
|
2016-11-17 04:00:08 +00:00
|
|
|
|
2016-11-09 04:01:05 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_add_job_with_none(hass: HomeAssistant) -> None:
|
2020-08-26 14:57:52 +00:00
|
|
|
"""Try to add a job with None as function."""
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
hass.async_add_job(None, "test_arg")
|
2016-12-16 05:30:09 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_event_eq() -> None:
|
2020-09-27 13:39:45 +00:00
|
|
|
"""Test events."""
|
|
|
|
now = dt_util.utcnow()
|
|
|
|
data = {"some": "attr"}
|
|
|
|
context = ha.Context()
|
2021-07-19 08:46:09 +00:00
|
|
|
event1, event2 = (
|
2020-09-27 13:39:45 +00:00
|
|
|
ha.Event("some_type", data, time_fired=now, context=context) for _ in range(2)
|
2021-07-19 08:46:09 +00:00
|
|
|
)
|
2020-09-27 13:39:45 +00:00
|
|
|
|
2023-01-29 18:31:43 +00:00
|
|
|
assert event1.as_dict() == event2.as_dict()
|
2020-09-27 13:39:45 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_event_repr() -> None:
|
2020-09-27 13:39:45 +00:00
|
|
|
"""Test that Event repr method works."""
|
|
|
|
assert str(ha.Event("TestEvent")) == "<Event TestEvent[L]>"
|
|
|
|
|
|
|
|
assert (
|
|
|
|
str(ha.Event("TestEvent", {"beer": "nice"}, ha.EventOrigin.remote))
|
|
|
|
== "<Event TestEvent[R]: beer=nice>"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_event_as_dict() -> None:
|
2020-10-05 14:18:57 +00:00
|
|
|
"""Test an Event as dictionary."""
|
2020-09-27 13:39:45 +00:00
|
|
|
event_type = "some_type"
|
|
|
|
now = dt_util.utcnow()
|
|
|
|
data = {"some": "attr"}
|
|
|
|
|
|
|
|
event = ha.Event(event_type, data, ha.EventOrigin.local, now)
|
|
|
|
expected = {
|
|
|
|
"event_type": event_type,
|
|
|
|
"data": data,
|
|
|
|
"origin": "LOCAL",
|
2020-10-05 14:18:57 +00:00
|
|
|
"time_fired": now.isoformat(),
|
2020-09-27 13:39:45 +00:00
|
|
|
"context": {
|
|
|
|
"id": event.context.id,
|
|
|
|
"parent_id": None,
|
|
|
|
"user_id": event.context.user_id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert event.as_dict() == expected
|
2020-10-05 14:18:57 +00:00
|
|
|
# 2nd time to verify cache
|
|
|
|
assert event.as_dict() == expected
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_as_dict() -> None:
|
2020-10-05 14:18:57 +00:00
|
|
|
"""Test a State as dictionary."""
|
|
|
|
last_time = datetime(1984, 12, 8, 12, 0, 0)
|
|
|
|
state = ha.State(
|
|
|
|
"happy.happy",
|
|
|
|
"on",
|
|
|
|
{"pig": "dog"},
|
|
|
|
last_updated=last_time,
|
|
|
|
last_changed=last_time,
|
|
|
|
)
|
|
|
|
expected = {
|
|
|
|
"context": {
|
|
|
|
"id": state.context.id,
|
|
|
|
"parent_id": None,
|
|
|
|
"user_id": state.context.user_id,
|
|
|
|
},
|
|
|
|
"entity_id": "happy.happy",
|
|
|
|
"attributes": {"pig": "dog"},
|
|
|
|
"last_changed": last_time.isoformat(),
|
|
|
|
"last_updated": last_time.isoformat(),
|
|
|
|
"state": "on",
|
|
|
|
}
|
2022-02-04 22:45:25 +00:00
|
|
|
as_dict_1 = state.as_dict()
|
|
|
|
assert isinstance(as_dict_1, ReadOnlyDict)
|
|
|
|
assert isinstance(as_dict_1["attributes"], ReadOnlyDict)
|
|
|
|
assert isinstance(as_dict_1["context"], ReadOnlyDict)
|
|
|
|
assert as_dict_1 == expected
|
2020-10-05 14:18:57 +00:00
|
|
|
# 2nd time to verify cache
|
|
|
|
assert state.as_dict() == expected
|
2022-02-04 22:45:25 +00:00
|
|
|
assert state.as_dict() is as_dict_1
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2023-05-16 07:33:12 +00:00
|
|
|
def test_state_as_dict_json() -> None:
|
|
|
|
"""Test a State as JSON."""
|
|
|
|
last_time = datetime(1984, 12, 8, 12, 0, 0)
|
|
|
|
state = ha.State(
|
|
|
|
"happy.happy",
|
|
|
|
"on",
|
|
|
|
{"pig": "dog"},
|
|
|
|
last_updated=last_time,
|
|
|
|
last_changed=last_time,
|
|
|
|
context=ha.Context(id="01H0D6K3RFJAYAV2093ZW30PCW"),
|
|
|
|
)
|
|
|
|
expected = (
|
|
|
|
'{"entity_id":"happy.happy","state":"on","attributes":{"pig":"dog"},'
|
|
|
|
'"last_changed":"1984-12-08T12:00:00","last_updated":"1984-12-08T12:00:00",'
|
|
|
|
'"context":{"id":"01H0D6K3RFJAYAV2093ZW30PCW","parent_id":null,"user_id":null}}'
|
|
|
|
)
|
|
|
|
as_dict_json_1 = state.as_dict_json()
|
|
|
|
assert as_dict_json_1 == expected
|
|
|
|
# 2nd time to verify cache
|
|
|
|
assert state.as_dict_json() == expected
|
|
|
|
assert state.as_dict_json() is as_dict_json_1
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_as_compressed_state() -> None:
|
2023-01-09 22:07:32 +00:00
|
|
|
"""Test a State as compressed state."""
|
|
|
|
last_time = datetime(1984, 12, 8, 12, 0, 0, tzinfo=dt_util.UTC)
|
|
|
|
state = ha.State(
|
|
|
|
"happy.happy",
|
|
|
|
"on",
|
|
|
|
{"pig": "dog"},
|
|
|
|
last_updated=last_time,
|
|
|
|
last_changed=last_time,
|
|
|
|
)
|
|
|
|
expected = {
|
|
|
|
"a": {"pig": "dog"},
|
|
|
|
"c": state.context.id,
|
|
|
|
"lc": last_time.timestamp(),
|
|
|
|
"s": "on",
|
|
|
|
}
|
|
|
|
as_compressed_state = state.as_compressed_state()
|
|
|
|
# We are not too concerned about these being ReadOnlyDict
|
|
|
|
# since we don't expect them to be called by external callers
|
|
|
|
assert as_compressed_state == expected
|
|
|
|
# 2nd time to verify cache
|
|
|
|
assert state.as_compressed_state() == expected
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_as_compressed_state_unique_last_updated() -> None:
|
2023-01-09 22:07:32 +00:00
|
|
|
"""Test a State as compressed state where last_changed is not last_updated."""
|
|
|
|
last_changed = datetime(1984, 12, 8, 11, 0, 0, tzinfo=dt_util.UTC)
|
|
|
|
last_updated = datetime(1984, 12, 8, 12, 0, 0, tzinfo=dt_util.UTC)
|
|
|
|
state = ha.State(
|
|
|
|
"happy.happy",
|
|
|
|
"on",
|
|
|
|
{"pig": "dog"},
|
|
|
|
last_updated=last_updated,
|
|
|
|
last_changed=last_changed,
|
|
|
|
)
|
|
|
|
expected = {
|
|
|
|
"a": {"pig": "dog"},
|
|
|
|
"c": state.context.id,
|
|
|
|
"lc": last_changed.timestamp(),
|
|
|
|
"lu": last_updated.timestamp(),
|
|
|
|
"s": "on",
|
|
|
|
}
|
|
|
|
as_compressed_state = state.as_compressed_state()
|
|
|
|
# We are not too concerned about these being ReadOnlyDict
|
|
|
|
# since we don't expect them to be called by external callers
|
|
|
|
assert as_compressed_state == expected
|
|
|
|
# 2nd time to verify cache
|
|
|
|
assert state.as_compressed_state() == expected
|
|
|
|
|
|
|
|
|
2023-05-16 07:33:12 +00:00
|
|
|
def test_state_as_compressed_state_json() -> None:
|
|
|
|
"""Test a State as a JSON compressed state."""
|
|
|
|
last_time = datetime(1984, 12, 8, 12, 0, 0, tzinfo=dt_util.UTC)
|
|
|
|
state = ha.State(
|
|
|
|
"happy.happy",
|
|
|
|
"on",
|
|
|
|
{"pig": "dog"},
|
|
|
|
last_updated=last_time,
|
|
|
|
last_changed=last_time,
|
|
|
|
context=ha.Context(id="01H0D6H5K3SZJ3XGDHED1TJ79N"),
|
|
|
|
)
|
|
|
|
expected = '"happy.happy":{"s":"on","a":{"pig":"dog"},"c":"01H0D6H5K3SZJ3XGDHED1TJ79N","lc":471355200.0}'
|
|
|
|
as_compressed_state = state.as_compressed_state_json()
|
|
|
|
# We are not too concerned about these being ReadOnlyDict
|
|
|
|
# since we don't expect them to be called by external callers
|
|
|
|
assert as_compressed_state == expected
|
|
|
|
# 2nd time to verify cache
|
|
|
|
assert state.as_compressed_state_json() == expected
|
|
|
|
assert state.as_compressed_state_json() is as_compressed_state
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_add_remove_listener(hass: HomeAssistant) -> None:
|
2020-11-16 14:43:48 +00:00
|
|
|
"""Test remove_listener method."""
|
|
|
|
old_count = len(hass.bus.async_listeners())
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
def listener(_):
|
|
|
|
pass
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
unsub = hass.bus.async_listen("test", listener)
|
2014-11-23 20:57:29 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
assert old_count + 1 == len(hass.bus.async_listeners())
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
# Remove listener
|
|
|
|
unsub()
|
|
|
|
assert old_count == len(hass.bus.async_listeners())
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
# Should do nothing now
|
|
|
|
unsub()
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_filtered_listener(hass: HomeAssistant) -> None:
|
2021-02-14 19:42:55 +00:00
|
|
|
"""Test we can prefilter events."""
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def listener(event):
|
|
|
|
"""Mock listener."""
|
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def filter(event):
|
|
|
|
"""Mock filter."""
|
|
|
|
return not event.data["filtered"]
|
|
|
|
|
|
|
|
unsub = hass.bus.async_listen("test", listener, event_filter=filter)
|
|
|
|
|
|
|
|
hass.bus.async_fire("test", {"filtered": True})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire("test", {"filtered": False})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
unsub()
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_run_immediately(hass: HomeAssistant) -> None:
|
2022-05-06 03:09:10 +00:00
|
|
|
"""Test we can call events immediately."""
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def listener(event):
|
|
|
|
"""Mock listener."""
|
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
unsub = hass.bus.async_listen("test", listener, run_immediately=True)
|
|
|
|
|
|
|
|
hass.bus.async_fire("test", {"event": True})
|
|
|
|
# No async_block_till_done here
|
|
|
|
assert len(calls) == 1
|
|
|
|
|
|
|
|
unsub()
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_unsubscribe_listener(hass: HomeAssistant) -> None:
|
2020-11-16 14:43:48 +00:00
|
|
|
"""Test unsubscribe listener from returned function."""
|
|
|
|
calls = []
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
@ha.callback
|
|
|
|
def listener(event):
|
|
|
|
"""Mock listener."""
|
|
|
|
calls.append(event)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
unsub = hass.bus.async_listen("test", listener)
|
2016-08-26 06:25:35 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
hass.bus.async_fire("test")
|
|
|
|
await hass.async_block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
assert len(calls) == 1
|
2016-08-26 06:25:35 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
unsub()
|
2016-08-26 06:25:35 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
hass.bus.async_fire("event")
|
|
|
|
await hass.async_block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
assert len(calls) == 1
|
2016-08-26 06:25:35 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_listen_once_event_with_callback(hass: HomeAssistant) -> None:
|
2020-11-16 14:43:48 +00:00
|
|
|
"""Test listen_once_event method."""
|
|
|
|
runs = []
|
2016-08-26 06:25:35 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
@ha.callback
|
|
|
|
def event_handler(event):
|
|
|
|
runs.append(event)
|
2014-11-29 07:19:59 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
hass.bus.async_listen_once("test_event", event_handler)
|
2016-10-18 02:38:41 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
# Second time it should not increase runs
|
|
|
|
hass.bus.async_fire("test_event")
|
2016-10-18 02:38:41 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(runs) == 1
|
2016-10-18 02:38:41 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_listen_once_event_with_coroutine(hass: HomeAssistant) -> None:
|
2020-11-16 14:43:48 +00:00
|
|
|
"""Test listen_once_event method."""
|
|
|
|
runs = []
|
2016-10-18 02:38:41 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
async def event_handler(event):
|
|
|
|
runs.append(event)
|
2016-10-18 02:38:41 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
hass.bus.async_listen_once("test_event", event_handler)
|
2016-10-18 02:38:41 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
# Second time it should not increase runs
|
|
|
|
hass.bus.async_fire("test_event")
|
2016-10-18 02:38:41 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(runs) == 1
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_listen_once_event_with_thread(hass: HomeAssistant) -> None:
|
2020-11-16 14:43:48 +00:00
|
|
|
"""Test listen_once_event method."""
|
|
|
|
runs = []
|
|
|
|
|
|
|
|
def event_handler(event):
|
|
|
|
runs.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen_once("test_event", event_handler)
|
2016-10-18 02:38:41 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
# Second time it should not increase runs
|
|
|
|
hass.bus.async_fire("test_event")
|
2016-10-18 02:38:41 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(runs) == 1
|
2016-10-18 02:38:41 +00:00
|
|
|
|
2014-11-29 07:19:59 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_thread_event_listener(hass: HomeAssistant) -> None:
|
2020-11-16 14:43:48 +00:00
|
|
|
"""Test thread event listener."""
|
|
|
|
thread_calls = []
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
def thread_listener(event):
|
|
|
|
thread_calls.append(event)
|
2014-11-29 07:19:59 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
hass.bus.async_listen("test_thread", thread_listener)
|
|
|
|
hass.bus.async_fire("test_thread")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(thread_calls) == 1
|
2016-10-05 03:44:32 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_callback_event_listener(hass: HomeAssistant) -> None:
|
2020-11-16 14:43:48 +00:00
|
|
|
"""Test callback event listener."""
|
|
|
|
callback_calls = []
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
@ha.callback
|
|
|
|
def callback_listener(event):
|
|
|
|
callback_calls.append(event)
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
hass.bus.async_listen("test_callback", callback_listener)
|
|
|
|
hass.bus.async_fire("test_callback")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(callback_calls) == 1
|
2016-10-05 03:44:32 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_coroutine_event_listener(hass: HomeAssistant) -> None:
|
2020-11-16 14:43:48 +00:00
|
|
|
"""Test coroutine event listener."""
|
|
|
|
coroutine_calls = []
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
async def coroutine_listener(event):
|
|
|
|
coroutine_calls.append(event)
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2020-11-16 14:43:48 +00:00
|
|
|
hass.bus.async_listen("test_coroutine", coroutine_listener)
|
|
|
|
hass.bus.async_fire("test_coroutine")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(coroutine_calls) == 1
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_eventbus_max_length_exceeded(hass: HomeAssistant) -> None:
|
2021-04-08 18:46:28 +00:00
|
|
|
"""Test that an exception is raised when the max character length is exceeded."""
|
|
|
|
|
|
|
|
long_evt_name = (
|
|
|
|
"this_event_exceeds_the_max_character_length_even_with_the_new_limit"
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(MaxLengthExceeded) as exc_info:
|
|
|
|
hass.bus.async_fire(long_evt_name)
|
|
|
|
|
|
|
|
assert exc_info.value.property_name == "event_type"
|
|
|
|
assert exc_info.value.max_length == 64
|
|
|
|
assert exc_info.value.value == long_evt_name
|
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_init() -> None:
|
2019-02-20 07:02:56 +00:00
|
|
|
"""Test state.init."""
|
|
|
|
with pytest.raises(InvalidEntityFormatError):
|
2019-07-31 19:25:30 +00:00
|
|
|
ha.State("invalid_entity_format", "test_state")
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2019-02-20 07:02:56 +00:00
|
|
|
with pytest.raises(InvalidStateError):
|
2019-07-31 19:25:30 +00:00
|
|
|
ha.State("domain.long_state", "t" * 256)
|
2019-02-20 07:02:56 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_domain() -> None:
|
2019-02-20 07:02:56 +00:00
|
|
|
"""Test domain."""
|
2019-07-31 19:25:30 +00:00
|
|
|
state = ha.State("some_domain.hello", "world")
|
2020-04-06 10:51:48 +00:00
|
|
|
assert state.domain == "some_domain"
|
2019-02-20 07:02:56 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_object_id() -> None:
|
2019-02-20 07:02:56 +00:00
|
|
|
"""Test object ID."""
|
2019-07-31 19:25:30 +00:00
|
|
|
state = ha.State("domain.hello", "world")
|
2020-04-06 10:51:48 +00:00
|
|
|
assert state.object_id == "hello"
|
2019-02-20 07:02:56 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_name_if_no_friendly_name_attr() -> None:
|
2019-02-20 07:02:56 +00:00
|
|
|
"""Test if there is no friendly name."""
|
2019-07-31 19:25:30 +00:00
|
|
|
state = ha.State("domain.hello_world", "world")
|
2020-04-06 10:51:48 +00:00
|
|
|
assert state.name == "hello world"
|
2019-02-20 07:02:56 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_name_if_friendly_name_attr() -> None:
|
2019-02-20 07:02:56 +00:00
|
|
|
"""Test if there is a friendly name."""
|
2019-07-31 19:25:30 +00:00
|
|
|
name = "Some Unique Name"
|
|
|
|
state = ha.State("domain.hello_world", "world", {ATTR_FRIENDLY_NAME: name})
|
2020-04-06 10:51:48 +00:00
|
|
|
assert state.name == name
|
2019-02-20 07:02:56 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_dict_conversion() -> None:
|
2019-02-20 07:02:56 +00:00
|
|
|
"""Test conversion of dict."""
|
2019-07-31 19:25:30 +00:00
|
|
|
state = ha.State("domain.hello", "world", {"some": "attr"})
|
2023-01-29 18:31:43 +00:00
|
|
|
assert state.as_dict() == ha.State.from_dict(state.as_dict()).as_dict()
|
2019-02-20 07:02:56 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_dict_conversion_with_wrong_data() -> None:
|
2019-02-20 07:02:56 +00:00
|
|
|
"""Test conversion with wrong data."""
|
|
|
|
assert ha.State.from_dict(None) is None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert ha.State.from_dict({"state": "yes"}) is None
|
|
|
|
assert ha.State.from_dict({"entity_id": "yes"}) is None
|
2019-02-20 07:02:56 +00:00
|
|
|
# Make sure invalid context data doesn't crash
|
2019-07-31 19:25:30 +00:00
|
|
|
wrong_context = ha.State.from_dict(
|
|
|
|
{
|
|
|
|
"entity_id": "light.kitchen",
|
|
|
|
"state": "on",
|
|
|
|
"context": {"id": "123", "non-existing": "crash"},
|
2019-02-20 07:02:56 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-02-20 07:02:56 +00:00
|
|
|
assert wrong_context is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert wrong_context.context.id == "123"
|
2019-02-20 07:02:56 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_state_repr() -> None:
|
2019-02-20 07:02:56 +00:00
|
|
|
"""Test state.repr."""
|
2020-04-06 10:51:48 +00:00
|
|
|
assert (
|
|
|
|
str(ha.State("happy.happy", "on", last_changed=datetime(1984, 12, 8, 12, 0, 0)))
|
|
|
|
== "<state happy.happy=on @ 1984-12-08T12:00:00+00:00>"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert (
|
2020-04-06 10:51:48 +00:00
|
|
|
str(
|
2019-07-31 19:25:30 +00:00
|
|
|
ha.State(
|
|
|
|
"happy.happy",
|
|
|
|
"on",
|
|
|
|
{"brightness": 144},
|
|
|
|
datetime(1984, 12, 8, 12, 0, 0),
|
|
|
|
)
|
|
|
|
)
|
2023-01-20 12:52:46 +00:00
|
|
|
== "<state happy.happy=on; brightness=144 @ 1984-12-08T12:00:00+00:00>"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_statemachine_is_state(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test is_state method."""
|
|
|
|
hass.states.async_set("light.bowl", "on", {})
|
|
|
|
assert hass.states.is_state("light.Bowl", "on")
|
|
|
|
assert not hass.states.is_state("light.Bowl", "off")
|
|
|
|
assert not hass.states.is_state("light.Non_existing", "on")
|
2014-11-29 07:19:59 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_statemachine_entity_ids(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test get_entity_ids method."""
|
|
|
|
hass.states.async_set("light.bowl", "on", {})
|
|
|
|
hass.states.async_set("SWITCH.AC", "off", {})
|
|
|
|
ent_ids = hass.states.async_entity_ids()
|
|
|
|
assert len(ent_ids) == 2
|
|
|
|
assert "light.bowl" in ent_ids
|
|
|
|
assert "switch.ac" in ent_ids
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
ent_ids = hass.states.async_entity_ids("light")
|
|
|
|
assert len(ent_ids) == 1
|
|
|
|
assert "light.bowl" in ent_ids
|
2016-10-31 15:47:29 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
states = sorted(state.entity_id for state in hass.states.async_all())
|
|
|
|
assert states == ["light.bowl", "switch.ac"]
|
2016-10-31 15:47:29 +00:00
|
|
|
|
2016-02-14 06:57:40 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_statemachine_remove(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test remove method."""
|
|
|
|
hass.states.async_set("light.bowl", "on", {})
|
|
|
|
events = async_capture_events(hass, EVENT_STATE_CHANGED)
|
2016-02-14 06:57:40 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
assert "light.bowl" in hass.states.async_entity_ids()
|
|
|
|
assert hass.states.async_remove("light.bowl")
|
|
|
|
await hass.async_block_till_done()
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
assert "light.bowl" not in hass.states.async_entity_ids()
|
|
|
|
assert len(events) == 1
|
|
|
|
assert events[0].data.get("entity_id") == "light.bowl"
|
|
|
|
assert events[0].data.get("old_state") is not None
|
|
|
|
assert events[0].data["old_state"].entity_id == "light.bowl"
|
|
|
|
assert events[0].data.get("new_state") is None
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
# If it does not exist, we should get False
|
|
|
|
assert not hass.states.async_remove("light.Bowl")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(events) == 1
|
2014-12-27 07:26:39 +00:00
|
|
|
|
2016-10-31 15:47:29 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_statemachine_case_insensitivty(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test insensitivty."""
|
|
|
|
events = async_capture_events(hass, EVENT_STATE_CHANGED)
|
2014-12-27 07:26:39 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
hass.states.async_set("light.BOWL", "off")
|
|
|
|
await hass.async_block_till_done()
|
2014-12-27 07:26:39 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
assert hass.states.is_state("light.bowl", "off")
|
|
|
|
assert len(events) == 1
|
2014-12-27 07:26:39 +00:00
|
|
|
|
2015-01-02 16:48:20 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_statemachine_last_changed_not_updated_on_same_state(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test to not update the existing, same state."""
|
|
|
|
hass.states.async_set("light.bowl", "on", {})
|
|
|
|
state = hass.states.get("light.Bowl")
|
2015-01-02 16:48:20 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
future = dt_util.utcnow() + timedelta(hours=10)
|
2015-01-02 16:48:20 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
with patch("homeassistant.util.dt.utcnow", return_value=future):
|
|
|
|
hass.states.async_set("light.Bowl", "on", {"attr": "triggers_change"})
|
|
|
|
await hass.async_block_till_done()
|
2015-01-02 16:48:20 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
state2 = hass.states.get("light.Bowl")
|
|
|
|
assert state2 is not None
|
|
|
|
assert state.last_changed == state2.last_changed
|
2016-10-31 15:47:29 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_statemachine_force_update(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test force update option."""
|
|
|
|
hass.states.async_set("light.bowl", "on", {})
|
|
|
|
events = async_capture_events(hass, EVENT_STATE_CHANGED)
|
2016-06-26 07:33:23 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
hass.states.async_set("light.bowl", "on")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(events) == 0
|
2016-06-26 07:33:23 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
hass.states.async_set("light.bowl", "on", None, True)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(events) == 1
|
2016-06-26 07:33:23 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_service_call_repr() -> None:
|
2018-07-29 00:53:37 +00:00
|
|
|
"""Test ServiceCall repr."""
|
2019-07-31 19:25:30 +00:00
|
|
|
call = ha.ServiceCall("homeassistant", "start")
|
2020-01-03 13:47:06 +00:00
|
|
|
assert str(call) == f"<ServiceCall homeassistant.start (c:{call.context.id})>"
|
2016-03-09 09:25:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
call2 = ha.ServiceCall("homeassistant", "start", {"fast": "yes"})
|
2020-01-03 13:47:06 +00:00
|
|
|
assert (
|
|
|
|
str(call2)
|
|
|
|
== f"<ServiceCall homeassistant.start (c:{call2.context.id}): fast=yes>"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_serviceregistry_has_service(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test has_service method."""
|
|
|
|
hass.services.async_register("test_domain", "test_service", lambda call: None)
|
|
|
|
assert len(hass.services.async_services()) == 1
|
|
|
|
assert hass.services.has_service("tesT_domaiN", "tesT_servicE")
|
|
|
|
assert not hass.services.has_service("test_domain", "non_existing")
|
|
|
|
assert not hass.services.has_service("non_existing", "test_service")
|
2016-11-05 23:36:20 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_serviceregistry_call_with_blocking_done_in_time(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test call with blocking."""
|
|
|
|
registered_events = async_capture_events(hass, EVENT_SERVICE_REGISTERED)
|
|
|
|
calls = async_mock_service(hass, "test_domain", "register_calls")
|
|
|
|
await hass.async_block_till_done()
|
2016-09-30 19:57:24 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
assert len(registered_events) == 1
|
|
|
|
assert registered_events[0].data["domain"] == "test_domain"
|
|
|
|
assert registered_events[0].data["service"] == "register_calls"
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2023-06-16 14:01:40 +00:00
|
|
|
await hass.services.async_call("test_domain", "REGISTER_CALLS", blocking=True)
|
2020-11-16 17:25:55 +00:00
|
|
|
assert len(calls) == 1
|
2015-08-04 16:16:10 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_serviceregistry_call_non_existing_with_blocking(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test non-existing with blocking."""
|
|
|
|
with pytest.raises(ha.ServiceNotFound):
|
|
|
|
await hass.services.async_call("test_domain", "i_do_not_exist", blocking=True)
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2016-09-30 19:57:24 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_serviceregistry_async_service(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test registering and calling an async service."""
|
|
|
|
calls = []
|
2016-09-30 19:57:24 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
async def service_handler(call):
|
|
|
|
"""Service handler coroutine."""
|
|
|
|
calls.append(call)
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
hass.services.async_register("test_domain", "register_calls", service_handler)
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2023-06-16 14:01:40 +00:00
|
|
|
await hass.services.async_call("test_domain", "REGISTER_CALLS", blocking=True)
|
2020-11-16 17:25:55 +00:00
|
|
|
assert len(calls) == 1
|
2016-09-30 19:57:24 +00:00
|
|
|
|
2019-05-07 16:39:42 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_serviceregistry_async_service_partial(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test registering and calling an wrapped async service."""
|
|
|
|
calls = []
|
2019-05-07 16:39:42 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
async def service_handler(call):
|
|
|
|
"""Service handler coroutine."""
|
|
|
|
calls.append(call)
|
2019-05-07 16:39:42 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
hass.services.async_register(
|
|
|
|
"test_domain", "register_calls", functools.partial(service_handler)
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2019-05-07 16:39:42 +00:00
|
|
|
|
2023-06-16 14:01:40 +00:00
|
|
|
await hass.services.async_call("test_domain", "REGISTER_CALLS", blocking=True)
|
2020-11-16 17:25:55 +00:00
|
|
|
assert len(calls) == 1
|
2019-05-07 16:39:42 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_serviceregistry_callback_service(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test registering and calling an async service."""
|
|
|
|
calls = []
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
@ha.callback
|
|
|
|
def service_handler(call):
|
|
|
|
"""Service handler coroutine."""
|
|
|
|
calls.append(call)
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
hass.services.async_register("test_domain", "register_calls", service_handler)
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2023-06-16 14:01:40 +00:00
|
|
|
await hass.services.async_call("test_domain", "REGISTER_CALLS", blocking=True)
|
2020-11-16 17:25:55 +00:00
|
|
|
assert len(calls) == 1
|
2016-10-05 03:44:32 +00:00
|
|
|
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_serviceregistry_remove_service(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test remove service."""
|
|
|
|
calls_remove = async_capture_events(hass, EVENT_SERVICE_REMOVED)
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
hass.services.async_register("test_domain", "test_service", lambda call: None)
|
|
|
|
assert hass.services.has_service("test_Domain", "test_Service")
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
hass.services.async_remove("test_Domain", "test_Service")
|
|
|
|
await hass.async_block_till_done()
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
assert not hass.services.has_service("test_Domain", "test_Service")
|
|
|
|
assert len(calls_remove) == 1
|
|
|
|
assert calls_remove[-1].data["domain"] == "test_domain"
|
|
|
|
assert calls_remove[-1].data["service"] == "test_service"
|
2017-03-08 06:51:34 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_serviceregistry_service_that_not_exists(hass: HomeAssistant) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test remove service that not exists."""
|
|
|
|
calls_remove = async_capture_events(hass, EVENT_SERVICE_REMOVED)
|
|
|
|
assert not hass.services.has_service("test_xxx", "test_yyy")
|
|
|
|
hass.services.async_remove("test_xxx", "test_yyy")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(calls_remove) == 0
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
with pytest.raises(ServiceNotFound):
|
|
|
|
await hass.services.async_call("test_do_not", "exist", {})
|
2017-03-08 06:51:34 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_serviceregistry_async_service_raise_exception(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test registering and calling an async service raise exception."""
|
2017-03-08 06:51:34 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
async def service_handler(_):
|
|
|
|
"""Service handler coroutine."""
|
|
|
|
raise ValueError
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
hass.services.async_register("test_domain", "register_calls", service_handler)
|
2019-03-02 07:09:31 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
with pytest.raises(ValueError):
|
2023-06-16 14:01:40 +00:00
|
|
|
await hass.services.async_call("test_domain", "REGISTER_CALLS", blocking=True)
|
2019-03-02 07:09:31 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
# Non-blocking service call never throw exception
|
2023-06-16 14:01:40 +00:00
|
|
|
hass.services.async_call("test_domain", "REGISTER_CALLS", blocking=False)
|
2020-11-16 17:25:55 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-03-02 07:09:31 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_serviceregistry_callback_service_raise_exception(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2020-11-16 17:25:55 +00:00
|
|
|
"""Test registering and calling an callback service raise exception."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
@ha.callback
|
|
|
|
def service_handler(_):
|
|
|
|
"""Service handler coroutine."""
|
|
|
|
raise ValueError
|
2019-03-02 07:09:31 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
hass.services.async_register("test_domain", "register_calls", service_handler)
|
2019-03-02 07:09:31 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
with pytest.raises(ValueError):
|
2023-06-16 14:01:40 +00:00
|
|
|
await hass.services.async_call("test_domain", "REGISTER_CALLS", blocking=True)
|
2019-03-02 07:09:31 +00:00
|
|
|
|
2020-11-16 17:25:55 +00:00
|
|
|
# Non-blocking service call never throw exception
|
2023-06-16 14:01:40 +00:00
|
|
|
hass.services.async_call("test_domain", "REGISTER_CALLS", blocking=False)
|
2020-11-16 17:25:55 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-03-02 07:09:31 +00:00
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2023-06-20 13:24:31 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"supports_response",
|
|
|
|
[
|
|
|
|
SupportsResponse.ONLY,
|
|
|
|
SupportsResponse.OPTIONAL,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_serviceregistry_async_return_response(
|
|
|
|
hass: HomeAssistant, supports_response: SupportsResponse
|
|
|
|
) -> None:
|
|
|
|
"""Test service call for a service that returns response data."""
|
2023-06-16 16:43:35 +00:00
|
|
|
|
2023-06-20 13:24:31 +00:00
|
|
|
async def service_handler(call: ServiceCall) -> ServiceResponse:
|
2023-06-16 16:43:35 +00:00
|
|
|
"""Service handler coroutine."""
|
2023-06-20 13:24:31 +00:00
|
|
|
assert call.return_response
|
2023-06-16 16:43:35 +00:00
|
|
|
return {"test-reply": "test-value1"}
|
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
service_handler,
|
2023-06-20 13:24:31 +00:00
|
|
|
supports_response=supports_response,
|
2023-06-16 16:43:35 +00:00
|
|
|
)
|
|
|
|
result = await hass.services.async_call(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
service_data={},
|
|
|
|
blocking=True,
|
2023-06-20 13:24:31 +00:00
|
|
|
return_response=True,
|
2023-06-16 16:43:35 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert result == {"test-reply": "test-value1"}
|
|
|
|
|
|
|
|
|
2023-06-20 13:24:31 +00:00
|
|
|
async def test_services_call_return_response_requires_blocking(
|
2023-06-16 16:43:35 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2023-06-20 13:24:31 +00:00
|
|
|
"""Test that non-blocking service calls cannot ask for response data."""
|
2023-06-16 16:43:35 +00:00
|
|
|
async_mock_service(hass, "test_domain", "test_service")
|
|
|
|
with pytest.raises(ValueError, match="when blocking=False"):
|
|
|
|
await hass.services.async_call(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
service_data={},
|
|
|
|
blocking=False,
|
2023-06-20 13:24:31 +00:00
|
|
|
return_response=True,
|
2023-06-16 16:43:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-06-20 13:24:31 +00:00
|
|
|
("response_data", "expected_error"),
|
2023-06-16 16:43:35 +00:00
|
|
|
[
|
|
|
|
(True, "expected a dictionary"),
|
|
|
|
(False, "expected a dictionary"),
|
|
|
|
(None, "expected a dictionary"),
|
|
|
|
("some-value", "expected a dictionary"),
|
|
|
|
(["some-list"], "expected a dictionary"),
|
|
|
|
],
|
|
|
|
)
|
2023-06-20 13:24:31 +00:00
|
|
|
async def test_serviceregistry_return_response_invalid(
|
|
|
|
hass: HomeAssistant, response_data: Any, expected_error: str
|
2023-06-16 16:43:35 +00:00
|
|
|
) -> None:
|
2023-06-20 13:24:31 +00:00
|
|
|
"""Test service call response data must be json serializable objects."""
|
2023-06-16 16:43:35 +00:00
|
|
|
|
2023-06-20 13:24:31 +00:00
|
|
|
def service_handler(call: ServiceCall) -> ServiceResponse:
|
2023-06-16 16:43:35 +00:00
|
|
|
"""Service handler coroutine."""
|
2023-06-20 13:24:31 +00:00
|
|
|
assert call.return_response
|
|
|
|
return response_data
|
2023-06-16 16:43:35 +00:00
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
service_handler,
|
2023-06-20 13:24:31 +00:00
|
|
|
supports_response=SupportsResponse.ONLY,
|
2023-06-16 16:43:35 +00:00
|
|
|
)
|
|
|
|
with pytest.raises(HomeAssistantError, match=expected_error):
|
|
|
|
await hass.services.async_call(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
service_data={},
|
|
|
|
blocking=True,
|
2023-06-20 13:24:31 +00:00
|
|
|
return_response=True,
|
2023-06-16 16:43:35 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
|
2023-06-20 13:24:31 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("supports_response", "return_response", "expected_error"),
|
|
|
|
[
|
|
|
|
(SupportsResponse.NONE, True, "not support responses"),
|
|
|
|
(SupportsResponse.ONLY, False, "caller did not ask for responses"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_serviceregistry_return_response_arguments(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
supports_response: SupportsResponse,
|
|
|
|
return_response: bool,
|
|
|
|
expected_error: str,
|
|
|
|
) -> None:
|
|
|
|
"""Test service call response data invalid arguments."""
|
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
"service_handler",
|
|
|
|
supports_response=supports_response,
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match=expected_error):
|
|
|
|
await hass.services.async_call(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
service_data={},
|
|
|
|
blocking=True,
|
|
|
|
return_response=return_response,
|
|
|
|
)
|
|
|
|
|
2023-06-16 16:43:35 +00:00
|
|
|
|
2023-06-20 13:24:31 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("return_response", "expected_response_data"),
|
|
|
|
[
|
|
|
|
(True, {"key": "value"}),
|
|
|
|
(False, None),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_serviceregistry_return_response_optional(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
return_response: bool,
|
|
|
|
expected_response_data: Any,
|
|
|
|
) -> None:
|
|
|
|
"""Test optional service call response data."""
|
|
|
|
|
|
|
|
def service_handler(call: ServiceCall) -> ServiceResponse:
|
2023-06-16 16:43:35 +00:00
|
|
|
"""Service handler coroutine."""
|
2023-06-20 13:24:31 +00:00
|
|
|
if call.return_response:
|
|
|
|
return {"key": "value"}
|
|
|
|
return None
|
2023-06-16 16:43:35 +00:00
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
service_handler,
|
2023-06-20 13:24:31 +00:00
|
|
|
supports_response=SupportsResponse.OPTIONAL,
|
2023-06-16 16:43:35 +00:00
|
|
|
)
|
2023-06-20 13:24:31 +00:00
|
|
|
response_data = await hass.services.async_call(
|
2023-06-16 16:43:35 +00:00
|
|
|
"test_domain",
|
|
|
|
"test_service",
|
|
|
|
service_data={},
|
|
|
|
blocking=True,
|
2023-06-20 13:24:31 +00:00
|
|
|
return_response=return_response,
|
2023-06-16 16:43:35 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2023-06-20 13:24:31 +00:00
|
|
|
assert response_data == expected_response_data
|
2023-06-16 16:43:35 +00:00
|
|
|
|
|
|
|
|
2023-02-07 09:26:56 +00:00
|
|
|
async def test_config_defaults() -> None:
|
2020-10-28 15:58:16 +00:00
|
|
|
"""Test config defaults."""
|
|
|
|
hass = Mock()
|
|
|
|
config = ha.Config(hass)
|
|
|
|
assert config.hass is hass
|
|
|
|
assert config.latitude == 0
|
|
|
|
assert config.longitude == 0
|
|
|
|
assert config.elevation == 0
|
|
|
|
assert config.location_name == "Home"
|
2021-05-08 05:46:26 +00:00
|
|
|
assert config.time_zone == "UTC"
|
2020-10-28 15:58:16 +00:00
|
|
|
assert config.internal_url is None
|
|
|
|
assert config.external_url is None
|
2021-12-19 17:02:52 +00:00
|
|
|
assert config.config_source is ha.ConfigSource.DEFAULT
|
2020-10-28 15:58:16 +00:00
|
|
|
assert config.skip_pip is False
|
2022-11-30 07:38:52 +00:00
|
|
|
assert config.skip_pip_packages == []
|
2020-10-28 15:58:16 +00:00
|
|
|
assert config.components == set()
|
|
|
|
assert config.api is None
|
|
|
|
assert config.config_dir is None
|
|
|
|
assert config.allowlist_external_dirs == set()
|
|
|
|
assert config.allowlist_external_urls == set()
|
|
|
|
assert config.media_dirs == {}
|
|
|
|
assert config.safe_mode is False
|
2020-11-08 15:11:38 +00:00
|
|
|
assert config.legacy_templates is False
|
2021-07-28 06:55:58 +00:00
|
|
|
assert config.currency == "EUR"
|
2022-11-24 22:25:50 +00:00
|
|
|
assert config.country is None
|
|
|
|
assert config.language == "en"
|
2020-10-28 15:58:16 +00:00
|
|
|
|
|
|
|
|
2023-02-07 09:26:56 +00:00
|
|
|
async def test_config_path_with_file() -> None:
|
2020-10-28 15:58:16 +00:00
|
|
|
"""Test get_config_path method."""
|
|
|
|
config = ha.Config(None)
|
|
|
|
config.config_dir = "/test/ha-config"
|
|
|
|
assert config.path("test.conf") == "/test/ha-config/test.conf"
|
|
|
|
|
|
|
|
|
2023-02-07 09:26:56 +00:00
|
|
|
async def test_config_path_with_dir_and_file() -> None:
|
2020-10-28 15:58:16 +00:00
|
|
|
"""Test get_config_path method."""
|
|
|
|
config = ha.Config(None)
|
|
|
|
config.config_dir = "/test/ha-config"
|
|
|
|
assert config.path("dir", "test.conf") == "/test/ha-config/dir/test.conf"
|
|
|
|
|
|
|
|
|
2023-02-07 09:26:56 +00:00
|
|
|
async def test_config_as_dict() -> None:
|
2020-10-28 15:58:16 +00:00
|
|
|
"""Test as dict."""
|
|
|
|
config = ha.Config(None)
|
|
|
|
config.config_dir = "/test/ha-config"
|
|
|
|
config.hass = MagicMock()
|
|
|
|
type(config.hass.state).value = PropertyMock(return_value="RUNNING")
|
|
|
|
expected = {
|
|
|
|
"latitude": 0,
|
|
|
|
"longitude": 0,
|
|
|
|
"elevation": 0,
|
|
|
|
CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(),
|
|
|
|
"location_name": "Home",
|
|
|
|
"time_zone": "UTC",
|
|
|
|
"components": set(),
|
|
|
|
"config_dir": "/test/ha-config",
|
|
|
|
"whitelist_external_dirs": set(),
|
|
|
|
"allowlist_external_dirs": set(),
|
|
|
|
"allowlist_external_urls": set(),
|
|
|
|
"version": __version__,
|
2021-12-19 17:02:52 +00:00
|
|
|
"config_source": ha.ConfigSource.DEFAULT,
|
2020-10-28 15:58:16 +00:00
|
|
|
"safe_mode": False,
|
|
|
|
"state": "RUNNING",
|
|
|
|
"external_url": None,
|
|
|
|
"internal_url": None,
|
2021-07-28 06:55:58 +00:00
|
|
|
"currency": "EUR",
|
2022-11-24 22:25:50 +00:00
|
|
|
"country": None,
|
|
|
|
"language": "en",
|
2020-10-28 15:58:16 +00:00
|
|
|
}
|
2016-03-09 09:25:50 +00:00
|
|
|
|
2020-10-28 15:58:16 +00:00
|
|
|
assert expected == config.as_dict()
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2020-06-25 00:37:01 +00:00
|
|
|
|
2023-02-07 09:26:56 +00:00
|
|
|
async def test_config_is_allowed_path() -> None:
|
2020-10-28 15:58:16 +00:00
|
|
|
"""Test is_allowed_path method."""
|
|
|
|
config = ha.Config(None)
|
|
|
|
with TemporaryDirectory() as tmp_dir:
|
|
|
|
# The created dir is in /tmp. This is a symlink on OS X
|
|
|
|
# causing this test to fail unless we resolve path first.
|
|
|
|
config.allowlist_external_dirs = {os.path.realpath(tmp_dir)}
|
|
|
|
|
|
|
|
test_file = os.path.join(tmp_dir, "test.jpg")
|
|
|
|
with open(test_file, "w") as tmp_file:
|
|
|
|
tmp_file.write("test")
|
|
|
|
|
|
|
|
valid = [test_file, tmp_dir, os.path.join(tmp_dir, "notfound321")]
|
|
|
|
for path in valid:
|
|
|
|
assert config.is_allowed_path(path)
|
|
|
|
|
|
|
|
config.allowlist_external_dirs = {"/home", "/var"}
|
2020-06-25 00:37:01 +00:00
|
|
|
|
|
|
|
invalid = [
|
2020-10-28 15:58:16 +00:00
|
|
|
"/hass/config/secure",
|
|
|
|
"/etc/passwd",
|
|
|
|
"/root/secure_file",
|
|
|
|
"/var/../etc/passwd",
|
|
|
|
test_file,
|
2020-06-25 00:37:01 +00:00
|
|
|
]
|
2020-10-28 15:58:16 +00:00
|
|
|
for path in invalid:
|
|
|
|
assert not config.is_allowed_path(path)
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
config.is_allowed_path(None)
|
|
|
|
|
|
|
|
|
2023-02-07 09:26:56 +00:00
|
|
|
async def test_config_is_allowed_external_url() -> None:
|
2020-10-28 15:58:16 +00:00
|
|
|
"""Test is_allowed_external_url method."""
|
|
|
|
config = ha.Config(None)
|
|
|
|
config.allowlist_external_urls = [
|
|
|
|
"http://x.com/",
|
|
|
|
"https://y.com/bla/",
|
|
|
|
"https://z.com/images/1.jpg/",
|
|
|
|
]
|
|
|
|
|
|
|
|
valid = [
|
|
|
|
"http://x.com/1.jpg",
|
|
|
|
"http://x.com",
|
|
|
|
"https://y.com/bla/",
|
|
|
|
"https://y.com/bla/2.png",
|
|
|
|
"https://z.com/images/1.jpg",
|
|
|
|
]
|
|
|
|
for url in valid:
|
|
|
|
assert config.is_allowed_external_url(url)
|
|
|
|
|
|
|
|
invalid = [
|
|
|
|
"https://a.co",
|
|
|
|
"https://y.com/bla_wrong",
|
|
|
|
"https://y.com/bla/../image.jpg",
|
|
|
|
"https://z.com/images",
|
|
|
|
]
|
|
|
|
for url in invalid:
|
|
|
|
assert not config.is_allowed_external_url(url)
|
2020-06-25 00:37:01 +00:00
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_event_on_update(hass: HomeAssistant) -> None:
|
2019-05-20 18:02:36 +00:00
|
|
|
"""Test that event is fired on update."""
|
2022-05-16 23:04:05 +00:00
|
|
|
events = async_capture_events(hass, EVENT_CORE_CONFIG_UPDATE)
|
2019-05-20 18:02:36 +00:00
|
|
|
|
|
|
|
assert hass.config.latitude != 12
|
|
|
|
|
2019-06-01 06:03:45 +00:00
|
|
|
await hass.config.async_update(latitude=12)
|
2019-05-20 18:02:36 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.config.latitude == 12
|
|
|
|
assert len(events) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert events[0].data == {"latitude": 12}
|
2019-05-20 18:02:36 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_bad_timezone_raises_value_error(hass: HomeAssistant) -> None:
|
2019-05-20 18:02:36 +00:00
|
|
|
"""Test bad timezone raises ValueError."""
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.config.async_update(time_zone="not_a_timezone")
|
2019-05-20 18:02:36 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_start_taking_too_long(
|
|
|
|
event_loop, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2017-04-08 21:53:32 +00:00
|
|
|
"""Test when async_start takes too long."""
|
2020-07-06 22:58:53 +00:00
|
|
|
hass = ha.HomeAssistant()
|
2017-04-08 21:53:32 +00:00
|
|
|
caplog.set_level(logging.WARNING)
|
2023-02-14 04:16:59 +00:00
|
|
|
hass.async_create_task(asyncio.sleep(0))
|
2017-04-08 21:53:32 +00:00
|
|
|
|
|
|
|
try:
|
2023-02-14 04:16:59 +00:00
|
|
|
with patch("asyncio.wait", return_value=(set(), {asyncio.Future()})):
|
2020-07-06 22:58:53 +00:00
|
|
|
await hass.async_start()
|
2017-04-08 21:53:32 +00:00
|
|
|
|
|
|
|
assert hass.state == ha.CoreState.running
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "Something is blocking Home Assistant" in caplog.text
|
2017-04-08 21:53:32 +00:00
|
|
|
|
|
|
|
finally:
|
2020-07-06 22:58:53 +00:00
|
|
|
await hass.async_stop()
|
2020-07-09 14:15:14 +00:00
|
|
|
assert hass.state == ha.CoreState.stopped
|
2017-04-11 16:09:31 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_service_executed_with_subservices(hass: HomeAssistant) -> None:
|
2018-09-11 19:40:35 +00:00
|
|
|
"""Test we block correctly till all services done."""
|
2019-07-31 19:25:30 +00:00
|
|
|
calls = async_mock_service(hass, "test", "inner")
|
2019-02-18 21:07:44 +00:00
|
|
|
context = ha.Context()
|
2018-09-11 19:40:35 +00:00
|
|
|
|
|
|
|
async def handle_outer(call):
|
|
|
|
"""Handle outer service call."""
|
|
|
|
calls.append(call)
|
2019-07-31 19:25:30 +00:00
|
|
|
call1 = hass.services.async_call(
|
|
|
|
"test", "inner", blocking=True, context=call.context
|
|
|
|
)
|
|
|
|
call2 = hass.services.async_call(
|
|
|
|
"test", "inner", blocking=True, context=call.context
|
|
|
|
)
|
2023-01-08 22:52:05 +00:00
|
|
|
await asyncio.wait(
|
|
|
|
[
|
|
|
|
hass.async_create_task(call1),
|
|
|
|
hass.async_create_task(call2),
|
|
|
|
]
|
|
|
|
)
|
2018-09-11 19:40:35 +00:00
|
|
|
calls.append(call)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.services.async_register("test", "outer", handle_outer)
|
2018-09-11 19:40:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call("test", "outer", blocking=True, context=context)
|
2018-09-11 19:40:35 +00:00
|
|
|
|
|
|
|
assert len(calls) == 4
|
2019-07-31 19:25:30 +00:00
|
|
|
assert [call.service for call in calls] == ["outer", "inner", "inner", "outer"]
|
2019-02-18 21:07:44 +00:00
|
|
|
assert all(call.context is context for call in calls)
|
2018-12-10 11:58:51 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_service_call_event_contains_original_data(hass: HomeAssistant) -> None:
|
2018-12-10 11:58:51 +00:00
|
|
|
"""Test that service call event contains original data."""
|
2022-05-16 23:04:05 +00:00
|
|
|
events = async_capture_events(hass, EVENT_CALL_SERVICE)
|
2018-12-10 11:58:51 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
calls = async_mock_service(
|
|
|
|
hass, "test", "service", vol.Schema({"number": vol.Coerce(int)})
|
|
|
|
)
|
2018-12-10 11:58:51 +00:00
|
|
|
|
2019-02-18 21:07:44 +00:00
|
|
|
context = ha.Context()
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"test", "service", {"number": "23"}, blocking=True, context=context
|
|
|
|
)
|
2018-12-10 11:58:51 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(events) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert events[0].data["service_data"]["number"] == "23"
|
2019-02-18 21:07:44 +00:00
|
|
|
assert events[0].context is context
|
2018-12-10 11:58:51 +00:00
|
|
|
assert len(calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert calls[0].data["number"] == 23
|
2019-02-18 21:07:44 +00:00
|
|
|
assert calls[0].context is context
|
2019-03-01 18:08:38 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_context() -> None:
|
2019-03-01 18:08:38 +00:00
|
|
|
"""Test context init."""
|
|
|
|
c = ha.Context()
|
|
|
|
assert c.user_id is None
|
|
|
|
assert c.parent_id is None
|
|
|
|
assert c.id is not None
|
|
|
|
|
|
|
|
c = ha.Context(23, 100)
|
|
|
|
assert c.user_id == 23
|
|
|
|
assert c.parent_id == 100
|
|
|
|
assert c.id is not None
|
2020-02-10 03:47:16 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_async_functions_with_callback(hass: HomeAssistant) -> None:
|
2020-02-10 03:47:16 +00:00
|
|
|
"""Test we deal with async functions accidentally marked as callback."""
|
|
|
|
runs = []
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
async def test():
|
|
|
|
runs.append(True)
|
|
|
|
|
|
|
|
await hass.async_add_job(test)
|
|
|
|
assert len(runs) == 1
|
|
|
|
|
|
|
|
hass.async_run_job(test)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(runs) == 2
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
async def service_handler(call):
|
|
|
|
runs.append(True)
|
|
|
|
|
|
|
|
hass.services.async_register("test_domain", "test_service", service_handler)
|
|
|
|
|
|
|
|
await hass.services.async_call("test_domain", "test_service", blocking=True)
|
|
|
|
assert len(runs) == 3
|
2020-02-24 16:35:02 +00:00
|
|
|
|
|
|
|
|
2023-02-07 13:20:06 +00:00
|
|
|
def test_valid_entity_id() -> None:
|
2020-02-24 16:35:02 +00:00
|
|
|
"""Test valid entity ID."""
|
|
|
|
for invalid in [
|
|
|
|
"_light.kitchen",
|
|
|
|
".kitchen",
|
|
|
|
".light.kitchen",
|
|
|
|
"light_.kitchen",
|
|
|
|
"light._kitchen",
|
|
|
|
"light.",
|
|
|
|
"light.kitchen__ceiling",
|
|
|
|
"light.kitchen_yo_",
|
|
|
|
"light.kitchen.",
|
|
|
|
"Light.kitchen",
|
|
|
|
"light.Kitchen",
|
|
|
|
"lightkitchen",
|
|
|
|
]:
|
|
|
|
assert not ha.valid_entity_id(invalid), invalid
|
|
|
|
|
|
|
|
for valid in [
|
|
|
|
"1.a",
|
|
|
|
"1light.kitchen",
|
|
|
|
"a.1",
|
|
|
|
"a.a",
|
|
|
|
"input_boolean.hello_world_0123",
|
|
|
|
"light.1kitchen",
|
|
|
|
"light.kitchen",
|
|
|
|
"light.something_yoo",
|
|
|
|
]:
|
|
|
|
assert ha.valid_entity_id(valid), valid
|
2020-05-08 00:29:47 +00:00
|
|
|
|
|
|
|
|
2023-05-12 12:57:51 +00:00
|
|
|
def test_valid_domain() -> None:
|
|
|
|
"""Test valid domain."""
|
|
|
|
for invalid in [
|
|
|
|
"_light",
|
|
|
|
".kitchen",
|
|
|
|
".light.kitchen",
|
|
|
|
"light_.kitchen",
|
|
|
|
"._kitchen",
|
|
|
|
"light.",
|
|
|
|
"light.kitchen__ceiling",
|
|
|
|
"light.kitchen_yo_",
|
|
|
|
"light.kitchen.",
|
|
|
|
"Light",
|
|
|
|
]:
|
|
|
|
assert not ha.valid_domain(invalid), invalid
|
|
|
|
|
|
|
|
for valid in [
|
|
|
|
"1",
|
|
|
|
"1light",
|
|
|
|
"a",
|
|
|
|
"input_boolean",
|
|
|
|
"light",
|
|
|
|
]:
|
|
|
|
assert ha.valid_domain(valid), valid
|
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_additional_data_in_core_config(
|
|
|
|
hass: HomeAssistant, hass_storage: dict[str, Any]
|
|
|
|
) -> None:
|
2020-05-16 11:31:15 +00:00
|
|
|
"""Test that we can handle additional data in core configuration."""
|
|
|
|
config = ha.Config(hass)
|
|
|
|
hass_storage[ha.CORE_STORAGE_KEY] = {
|
|
|
|
"version": 1,
|
|
|
|
"data": {"location_name": "Test Name", "additional_valid_key": "value"},
|
|
|
|
}
|
|
|
|
await config.async_load()
|
|
|
|
assert config.location_name == "Test Name"
|
2020-06-15 22:22:53 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_incorrect_internal_external_url(
|
|
|
|
hass: HomeAssistant, hass_storage: dict[str, Any], caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2022-10-17 11:59:04 +00:00
|
|
|
"""Test that we warn when detecting invalid internal/external url."""
|
2021-08-09 07:38:09 +00:00
|
|
|
config = ha.Config(hass)
|
2021-08-09 08:52:14 +00:00
|
|
|
|
|
|
|
hass_storage[ha.CORE_STORAGE_KEY] = {
|
|
|
|
"version": 1,
|
|
|
|
"data": {
|
|
|
|
"internal_url": None,
|
|
|
|
"external_url": None,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
await config.async_load()
|
|
|
|
assert "Invalid external_url set" not in caplog.text
|
|
|
|
assert "Invalid internal_url set" not in caplog.text
|
|
|
|
|
2022-10-17 11:59:04 +00:00
|
|
|
config = ha.Config(hass)
|
|
|
|
|
2021-08-09 07:38:09 +00:00
|
|
|
hass_storage[ha.CORE_STORAGE_KEY] = {
|
|
|
|
"version": 1,
|
|
|
|
"data": {
|
|
|
|
"internal_url": "https://community.home-assistant.io/profile",
|
|
|
|
"external_url": "https://www.home-assistant.io/blue",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
await config.async_load()
|
|
|
|
assert "Invalid external_url set" in caplog.text
|
|
|
|
assert "Invalid internal_url set" in caplog.text
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_start_events(hass: HomeAssistant) -> None:
|
2020-06-15 22:22:53 +00:00
|
|
|
"""Test events fired when starting Home Assistant."""
|
|
|
|
hass.state = ha.CoreState.not_running
|
|
|
|
|
|
|
|
all_events = []
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def capture_events(ev):
|
|
|
|
all_events.append(ev.event_type)
|
|
|
|
|
|
|
|
hass.bus.async_listen(MATCH_ALL, capture_events)
|
|
|
|
|
|
|
|
core_states = []
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def capture_core_state(_):
|
|
|
|
core_states.append(hass.state)
|
|
|
|
|
|
|
|
hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, capture_core_state)
|
|
|
|
|
|
|
|
await hass.async_start()
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert all_events == [
|
|
|
|
EVENT_CORE_CONFIG_UPDATE,
|
|
|
|
EVENT_HOMEASSISTANT_START,
|
|
|
|
EVENT_CORE_CONFIG_UPDATE,
|
|
|
|
EVENT_HOMEASSISTANT_STARTED,
|
|
|
|
]
|
|
|
|
assert core_states == [ha.CoreState.starting, ha.CoreState.running]
|
2020-07-24 02:03:42 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_log_blocking_events(
|
|
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2020-07-24 02:03:42 +00:00
|
|
|
"""Ensure we log which task is blocking startup when debug logging is on."""
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
|
|
|
async def _wait_a_bit_1():
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
|
|
|
async def _wait_a_bit_2():
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
|
|
|
hass.async_create_task(_wait_a_bit_1())
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-07-28 16:24:29 +00:00
|
|
|
with patch.object(ha, "BLOCK_LOG_TIMEOUT", 0.0001):
|
2020-07-24 02:03:42 +00:00
|
|
|
hass.async_create_task(_wait_a_bit_2())
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert "_wait_a_bit_2" in caplog.text
|
|
|
|
assert "_wait_a_bit_1" not in caplog.text
|
2020-07-28 16:24:29 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_chained_logging_hits_log_timeout(
|
|
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2020-07-28 16:24:29 +00:00
|
|
|
"""Ensure we log which task is blocking startup when there is a task chain and debug logging is on."""
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
|
|
|
created = 0
|
|
|
|
|
|
|
|
async def _task_chain_1():
|
|
|
|
nonlocal created
|
|
|
|
created += 1
|
2020-08-03 13:01:15 +00:00
|
|
|
if created > 1000:
|
2020-07-28 16:24:29 +00:00
|
|
|
return
|
|
|
|
hass.async_create_task(_task_chain_2())
|
|
|
|
|
|
|
|
async def _task_chain_2():
|
|
|
|
nonlocal created
|
|
|
|
created += 1
|
2020-08-03 13:01:15 +00:00
|
|
|
if created > 1000:
|
2020-07-28 16:24:29 +00:00
|
|
|
return
|
|
|
|
hass.async_create_task(_task_chain_1())
|
|
|
|
|
|
|
|
with patch.object(ha, "BLOCK_LOG_TIMEOUT", 0.0001):
|
|
|
|
hass.async_create_task(_task_chain_1())
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert "_task_chain_" in caplog.text
|
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_chained_logging_misses_log_timeout(
|
|
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2020-07-28 16:24:29 +00:00
|
|
|
"""Ensure we do not log which task is blocking startup if we do not hit the timeout."""
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
|
|
|
created = 0
|
|
|
|
|
|
|
|
async def _task_chain_1():
|
|
|
|
nonlocal created
|
|
|
|
created += 1
|
|
|
|
if created > 10:
|
|
|
|
return
|
|
|
|
hass.async_create_task(_task_chain_2())
|
|
|
|
|
|
|
|
async def _task_chain_2():
|
|
|
|
nonlocal created
|
|
|
|
created += 1
|
|
|
|
if created > 10:
|
|
|
|
return
|
|
|
|
hass.async_create_task(_task_chain_1())
|
|
|
|
|
|
|
|
hass.async_create_task(_task_chain_1())
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert "_task_chain_" not in caplog.text
|
2020-09-06 21:20:32 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_async_all(hass: HomeAssistant) -> None:
|
2020-09-06 21:20:32 +00:00
|
|
|
"""Test async_all."""
|
|
|
|
|
|
|
|
hass.states.async_set("switch.link", "on")
|
|
|
|
hass.states.async_set("light.bowl", "on")
|
|
|
|
hass.states.async_set("light.frog", "on")
|
|
|
|
hass.states.async_set("vacuum.floor", "on")
|
|
|
|
|
|
|
|
assert {state.entity_id for state in hass.states.async_all()} == {
|
|
|
|
"switch.link",
|
|
|
|
"light.bowl",
|
|
|
|
"light.frog",
|
|
|
|
"vacuum.floor",
|
|
|
|
}
|
|
|
|
assert {state.entity_id for state in hass.states.async_all("light")} == {
|
|
|
|
"light.bowl",
|
|
|
|
"light.frog",
|
|
|
|
}
|
|
|
|
assert {
|
|
|
|
state.entity_id for state in hass.states.async_all(["light", "switch"])
|
|
|
|
} == {"light.bowl", "light.frog", "switch.link"}
|
2020-09-26 16:36:47 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_async_entity_ids_count(hass: HomeAssistant) -> None:
|
2020-09-26 16:36:47 +00:00
|
|
|
"""Test async_entity_ids_count."""
|
|
|
|
|
|
|
|
hass.states.async_set("switch.link", "on")
|
|
|
|
hass.states.async_set("light.bowl", "on")
|
|
|
|
hass.states.async_set("light.frog", "on")
|
|
|
|
hass.states.async_set("vacuum.floor", "on")
|
|
|
|
|
|
|
|
assert hass.states.async_entity_ids_count() == 4
|
|
|
|
assert hass.states.async_entity_ids_count("light") == 2
|
|
|
|
|
|
|
|
hass.states.async_set("light.cow", "on")
|
|
|
|
|
|
|
|
assert hass.states.async_entity_ids_count() == 5
|
|
|
|
assert hass.states.async_entity_ids_count("light") == 3
|
2020-10-19 21:25:33 +00:00
|
|
|
|
|
|
|
|
2023-02-07 09:26:56 +00:00
|
|
|
async def test_hassjob_forbid_coroutine() -> None:
|
2020-10-19 21:25:33 +00:00
|
|
|
"""Test hassjob forbids coroutines."""
|
|
|
|
|
|
|
|
async def bla():
|
|
|
|
pass
|
|
|
|
|
|
|
|
coro = bla()
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ha.HassJob(coro)
|
|
|
|
|
|
|
|
# To avoid warning about unawaited coro
|
|
|
|
await coro
|
2020-10-21 15:01:51 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_reserving_states(hass: HomeAssistant) -> None:
|
2020-10-21 15:01:51 +00:00
|
|
|
"""Test we can reserve a state in the state machine."""
|
|
|
|
|
|
|
|
hass.states.async_reserve("light.bedroom")
|
|
|
|
assert hass.states.async_available("light.bedroom") is False
|
|
|
|
hass.states.async_set("light.bedroom", "on")
|
|
|
|
assert hass.states.async_available("light.bedroom") is False
|
|
|
|
|
|
|
|
with pytest.raises(ha.HomeAssistantError):
|
|
|
|
hass.states.async_reserve("light.bedroom")
|
|
|
|
|
|
|
|
hass.states.async_remove("light.bedroom")
|
|
|
|
assert hass.states.async_available("light.bedroom") is True
|
|
|
|
hass.states.async_set("light.bedroom", "on")
|
|
|
|
|
|
|
|
with pytest.raises(ha.HomeAssistantError):
|
|
|
|
hass.states.async_reserve("light.bedroom")
|
|
|
|
|
|
|
|
assert hass.states.async_available("light.bedroom") is False
|
|
|
|
hass.states.async_remove("light.bedroom")
|
|
|
|
assert hass.states.async_available("light.bedroom") is True
|
2020-11-08 01:51:06 +00:00
|
|
|
|
|
|
|
|
2022-05-14 19:12:08 +00:00
|
|
|
def _ulid_timestamp(ulid: str) -> int:
|
|
|
|
encoded = ulid[:10].encode("ascii")
|
|
|
|
# This unpacks the time from the ulid
|
|
|
|
|
|
|
|
# Copied from
|
|
|
|
# https://github.com/ahawker/ulid/blob/06289583e9de4286b4d80b4ad000d137816502ca/ulid/base32.py#L296
|
|
|
|
decoding = array.array(
|
|
|
|
"B",
|
|
|
|
(
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0x00,
|
|
|
|
0x01,
|
|
|
|
0x02,
|
|
|
|
0x03,
|
|
|
|
0x04,
|
|
|
|
0x05,
|
|
|
|
0x06,
|
|
|
|
0x07,
|
|
|
|
0x08,
|
|
|
|
0x09,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0x0A,
|
|
|
|
0x0B,
|
|
|
|
0x0C,
|
|
|
|
0x0D,
|
|
|
|
0x0E,
|
|
|
|
0x0F,
|
|
|
|
0x10,
|
|
|
|
0x11,
|
|
|
|
0x01,
|
|
|
|
0x12,
|
|
|
|
0x13,
|
|
|
|
0x01,
|
|
|
|
0x14,
|
|
|
|
0x15,
|
|
|
|
0x00,
|
|
|
|
0x16,
|
|
|
|
0x17,
|
|
|
|
0x18,
|
|
|
|
0x19,
|
|
|
|
0x1A,
|
|
|
|
0xFF,
|
|
|
|
0x1B,
|
|
|
|
0x1C,
|
|
|
|
0x1D,
|
|
|
|
0x1E,
|
|
|
|
0x1F,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0x0A,
|
|
|
|
0x0B,
|
|
|
|
0x0C,
|
|
|
|
0x0D,
|
|
|
|
0x0E,
|
|
|
|
0x0F,
|
|
|
|
0x10,
|
|
|
|
0x11,
|
|
|
|
0x01,
|
|
|
|
0x12,
|
|
|
|
0x13,
|
|
|
|
0x01,
|
|
|
|
0x14,
|
|
|
|
0x15,
|
|
|
|
0x00,
|
|
|
|
0x16,
|
|
|
|
0x17,
|
|
|
|
0x18,
|
|
|
|
0x19,
|
|
|
|
0x1A,
|
|
|
|
0xFF,
|
|
|
|
0x1B,
|
|
|
|
0x1C,
|
|
|
|
0x1D,
|
|
|
|
0x1E,
|
|
|
|
0x1F,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
0xFF,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return int.from_bytes(
|
|
|
|
bytes(
|
|
|
|
(
|
|
|
|
((decoding[encoded[0]] << 5) | decoding[encoded[1]]) & 0xFF,
|
|
|
|
((decoding[encoded[2]] << 3) | (decoding[encoded[3]] >> 2)) & 0xFF,
|
|
|
|
(
|
|
|
|
(decoding[encoded[3]] << 6)
|
|
|
|
| (decoding[encoded[4]] << 1)
|
|
|
|
| (decoding[encoded[5]] >> 4)
|
|
|
|
)
|
|
|
|
& 0xFF,
|
|
|
|
((decoding[encoded[5]] << 4) | (decoding[encoded[6]] >> 1)) & 0xFF,
|
|
|
|
(
|
|
|
|
(decoding[encoded[6]] << 7)
|
|
|
|
| (decoding[encoded[7]] << 2)
|
|
|
|
| (decoding[encoded[8]] >> 3)
|
|
|
|
)
|
|
|
|
& 0xFF,
|
|
|
|
((decoding[encoded[8]] << 5) | (decoding[encoded[9]])) & 0xFF,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
byteorder="big",
|
|
|
|
)
|
|
|
|
|
2020-11-08 01:51:06 +00:00
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_state_change_events_context_id_match_state_time(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2022-05-14 19:12:08 +00:00
|
|
|
"""Test last_updated, timed_fired, and the ulid all have the same time."""
|
2022-05-16 23:04:05 +00:00
|
|
|
events = async_capture_events(hass, ha.EVENT_STATE_CHANGED)
|
2020-11-08 01:51:06 +00:00
|
|
|
hass.states.async_set("light.bedroom", "on")
|
|
|
|
await hass.async_block_till_done()
|
2022-05-14 19:12:08 +00:00
|
|
|
state: State = hass.states.get("light.bedroom")
|
2020-11-08 01:51:06 +00:00
|
|
|
assert state.last_updated == events[0].time_fired
|
2022-05-14 19:12:08 +00:00
|
|
|
assert len(state.context.id) == 26
|
|
|
|
# ULIDs store time to 3 decimal places compared to python timestamps
|
|
|
|
assert _ulid_timestamp(state.context.id) == int(
|
|
|
|
state.last_updated.timestamp() * 1000
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_state_firing_event_matches_context_id_ulid_time(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2022-05-14 19:12:08 +00:00
|
|
|
"""Test timed_fired and the ulid have the same time."""
|
2022-05-16 23:04:05 +00:00
|
|
|
events = async_capture_events(hass, EVENT_HOMEASSISTANT_STARTED)
|
2022-05-14 19:12:08 +00:00
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
event = events[0]
|
|
|
|
assert len(event.context.id) == 26
|
|
|
|
# ULIDs store time to 3 decimal places compared to python timestamps
|
|
|
|
assert _ulid_timestamp(event.context.id) == int(
|
|
|
|
events[0].time_fired.timestamp() * 1000
|
|
|
|
)
|
2022-05-22 19:57:54 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_event_context(hass: HomeAssistant) -> None:
|
2022-05-22 19:57:54 +00:00
|
|
|
"""Test we can lookup the origin of a context from an event."""
|
|
|
|
events = []
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def capture_events(event):
|
|
|
|
nonlocal events
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
cancel = hass.bus.async_listen("dummy_event", capture_events)
|
|
|
|
cancel2 = hass.bus.async_listen("dummy_event_2", capture_events)
|
|
|
|
|
|
|
|
hass.bus.async_fire("dummy_event")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
dummy_event: ha.Event = events[0]
|
|
|
|
|
|
|
|
hass.bus.async_fire("dummy_event_2", context=dummy_event.context)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
context_id = dummy_event.context.id
|
|
|
|
|
|
|
|
dummy_event2: ha.Event = events[1]
|
|
|
|
assert dummy_event2.context == dummy_event.context
|
|
|
|
assert dummy_event2.context.id == context_id
|
|
|
|
cancel()
|
|
|
|
cancel2()
|
|
|
|
|
|
|
|
assert dummy_event2.context.origin_event == dummy_event
|
2022-05-27 03:54:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _get_full_name(obj) -> str:
|
|
|
|
"""Get the full name of an object in memory."""
|
|
|
|
objtype = type(obj)
|
|
|
|
name = objtype.__name__
|
|
|
|
if module := getattr(objtype, "__module__", None):
|
|
|
|
return f"{module}.{name}"
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
|
|
def _get_by_type(full_name: str) -> list[Any]:
|
|
|
|
"""Get all objects in memory with a specific type."""
|
|
|
|
return [obj for obj in gc.get_objects() if _get_full_name(obj) == full_name]
|
|
|
|
|
|
|
|
|
|
|
|
# The logger will hold a strong reference to the event for the life of the tests
|
|
|
|
# so we must patch it out
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
not os.environ.get("DEBUG_MEMORY"),
|
|
|
|
reason="Takes too long on the CI",
|
|
|
|
)
|
|
|
|
@patch.object(ha._LOGGER, "debug", lambda *args: None)
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_state_changed_events_to_not_leak_contexts(hass: HomeAssistant) -> None:
|
2022-05-27 03:54:26 +00:00
|
|
|
"""Test state changed events do not leak contexts."""
|
|
|
|
gc.collect()
|
|
|
|
# Other tests can log Contexts which keep them in memory
|
|
|
|
# so we need to look at how many exist at the start
|
|
|
|
init_count = len(_get_by_type("homeassistant.core.Context"))
|
|
|
|
|
|
|
|
assert len(_get_by_type("homeassistant.core.Context")) == init_count
|
|
|
|
for i in range(20):
|
|
|
|
hass.states.async_set("light.switch", str(i))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
assert len(_get_by_type("homeassistant.core.Context")) == init_count + 2
|
|
|
|
|
|
|
|
hass.states.async_remove("light.switch")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
assert len(_get_by_type("homeassistant.core.Context")) == init_count
|
2023-02-17 01:39:29 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_background_task(hass: HomeAssistant) -> None:
|
2023-02-17 01:39:29 +00:00
|
|
|
"""Test background tasks being quit."""
|
|
|
|
result = asyncio.Future()
|
|
|
|
|
|
|
|
async def test_task():
|
|
|
|
try:
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
result.set_result(hass.state)
|
|
|
|
raise
|
|
|
|
|
|
|
|
task = hass.async_create_background_task(test_task(), "happy task")
|
|
|
|
assert "happy task" in str(task)
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
await hass.async_stop()
|
|
|
|
assert result.result() == ha.CoreState.stopping
|
2023-02-27 03:36:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_shutdown_does_not_block_on_normal_tasks(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
|
|
|
"""Ensure shutdown does not block on normal tasks."""
|
|
|
|
result = asyncio.Future()
|
|
|
|
unshielded_task = asyncio.sleep(10)
|
|
|
|
|
|
|
|
async def test_task():
|
|
|
|
try:
|
|
|
|
await unshielded_task
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
result.set_result(hass.state)
|
|
|
|
|
|
|
|
start = time.monotonic()
|
|
|
|
task = hass.async_create_task(test_task())
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
await hass.async_stop()
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
assert result.done()
|
|
|
|
assert task.done()
|
|
|
|
assert time.monotonic() - start < 0.5
|
|
|
|
|
|
|
|
|
|
|
|
async def test_shutdown_does_not_block_on_shielded_tasks(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
|
|
|
"""Ensure shutdown does not block on shielded tasks."""
|
|
|
|
result = asyncio.Future()
|
2023-02-28 17:03:36 +00:00
|
|
|
sleep_task = asyncio.ensure_future(asyncio.sleep(10))
|
|
|
|
shielded_task = asyncio.shield(sleep_task)
|
2023-02-27 03:36:18 +00:00
|
|
|
|
|
|
|
async def test_task():
|
|
|
|
try:
|
|
|
|
await shielded_task
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
result.set_result(hass.state)
|
|
|
|
|
|
|
|
start = time.monotonic()
|
|
|
|
task = hass.async_create_task(test_task())
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
await hass.async_stop()
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
assert result.done()
|
|
|
|
assert task.done()
|
|
|
|
assert time.monotonic() - start < 0.5
|
2023-02-28 17:03:36 +00:00
|
|
|
|
|
|
|
# Cleanup lingering task after test is done
|
|
|
|
sleep_task.cancel()
|
2023-04-07 09:38:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_cancellable_hassjob(hass: HomeAssistant) -> None:
|
|
|
|
"""Simulate a shutdown, ensure cancellable jobs are cancelled."""
|
|
|
|
job = MagicMock()
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def run_job(job: HassJob) -> None:
|
|
|
|
"""Call the action."""
|
|
|
|
hass.async_run_hass_job(job)
|
|
|
|
|
|
|
|
timer1 = hass.loop.call_later(
|
|
|
|
60, run_job, HassJob(ha.callback(job), cancel_on_shutdown=True)
|
|
|
|
)
|
|
|
|
timer2 = hass.loop.call_later(60, run_job, HassJob(ha.callback(job)))
|
|
|
|
|
|
|
|
await hass.async_stop()
|
|
|
|
|
|
|
|
assert timer1.cancelled()
|
|
|
|
assert not timer2.cancelled()
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
timer2.cancel()
|