core/tests/components/demo/test_init.py

40 lines
1.3 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Demo component."""
import json
2023-01-10 16:31:47 +00:00
from unittest.mock import patch
2014-11-29 06:49:29 +00:00
import pytest
from homeassistant.components.demo import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers.json import JSONEncoder
from homeassistant.setup import async_setup_component
2014-11-29 06:49:29 +00:00
@pytest.fixture
def mock_history(hass):
"""Mock history component loaded."""
2019-07-31 19:25:30 +00:00
hass.config.components.add("history")
@pytest.fixture(autouse=True)
def mock_device_tracker_update_config():
"""Prevent device tracker from creating known devices file."""
with patch("homeassistant.components.device_tracker.legacy.update_config"):
yield
2014-11-29 06:49:29 +00:00
async def test_setting_up_demo(mock_history, hass: HomeAssistant) -> None:
"""Test if we can set up the demo and dump it to JSON."""
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()
await hass.async_start()
# This is done to make sure entity components don't accidentally store
# non-JSON-serializable data in the state machine.
try:
json.dumps(hass.states.async_all(), cls=JSONEncoder)
except Exception: # pylint: disable=broad-except
2019-07-31 19:25:30 +00:00
pytest.fail(
"Unable to convert all demo entities to JSON. Wrong data in state machine!"
2019-07-31 19:25:30 +00:00
)