mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add explicit test of template config entry setup (#99345)
This commit is contained in:
parent
59b1d4ba69
commit
8c04e4c7a3
26
tests/components/template/snapshots/test_binary_sensor.ambr
Normal file
26
tests/components/template/snapshots/test_binary_sensor.ambr
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# serializer version: 1
|
||||||
|
# name: test_setup_config_entry[config_entry_extra_options0]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'My template',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'binary_sensor.my_template',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'on',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_setup_config_entry[config_entry_extra_options1]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'battery',
|
||||||
|
'friendly_name': 'My template',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'binary_sensor.my_template',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'on',
|
||||||
|
})
|
||||||
|
# ---
|
28
tests/components/template/snapshots/test_sensor.ambr
Normal file
28
tests/components/template/snapshots/test_sensor.ambr
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# serializer version: 1
|
||||||
|
# name: test_setup_config_entry[config_entry_extra_options0]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'My template',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.my_template',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '30.0',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_setup_config_entry[config_entry_extra_options1]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'battery',
|
||||||
|
'friendly_name': 'My template',
|
||||||
|
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||||
|
'unit_of_measurement': '%',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.my_template',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '30.0',
|
||||||
|
})
|
||||||
|
# ---
|
@ -5,6 +5,7 @@ from unittest.mock import patch
|
|||||||
|
|
||||||
from freezegun.api import FrozenDateTimeFactory
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant import setup
|
from homeassistant import setup
|
||||||
from homeassistant.components import binary_sensor, template
|
from homeassistant.components import binary_sensor, template
|
||||||
@ -23,6 +24,7 @@ from homeassistant.setup import async_setup_component
|
|||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
|
MockConfigEntry,
|
||||||
assert_setup_component,
|
assert_setup_component,
|
||||||
async_fire_time_changed,
|
async_fire_time_changed,
|
||||||
mock_restore_cache,
|
mock_restore_cache,
|
||||||
@ -123,6 +125,55 @@ async def test_setup(hass: HomeAssistant, start_ha, entity_id) -> None:
|
|||||||
assert state.attributes["device_class"] == "motion"
|
assert state.attributes["device_class"] == "motion"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"config_entry_extra_options",
|
||||||
|
[
|
||||||
|
{},
|
||||||
|
{"device_class": "battery"},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_setup_config_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
config_entry_extra_options: dict[str, str],
|
||||||
|
) -> None:
|
||||||
|
"""Test the config flow."""
|
||||||
|
state_template = (
|
||||||
|
"{{ states('binary_sensor.one') == 'on' or "
|
||||||
|
" states('binary_sensor.two') == 'on' }}"
|
||||||
|
)
|
||||||
|
input_entities = ["one", "two"]
|
||||||
|
input_states = {"one": "on", "two": "off"}
|
||||||
|
template_type = binary_sensor.DOMAIN
|
||||||
|
|
||||||
|
for input_entity in input_entities:
|
||||||
|
hass.states.async_set(
|
||||||
|
f"{template_type}.{input_entity}",
|
||||||
|
input_states[input_entity],
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
|
||||||
|
template_config_entry = MockConfigEntry(
|
||||||
|
data={},
|
||||||
|
domain=template.DOMAIN,
|
||||||
|
options={
|
||||||
|
"name": "My template",
|
||||||
|
"state": state_template,
|
||||||
|
"template_type": template_type,
|
||||||
|
}
|
||||||
|
| config_entry_extra_options,
|
||||||
|
title="My template",
|
||||||
|
)
|
||||||
|
template_config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
assert await hass.config_entries.async_setup(template_config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get(f"{template_type}.my_template")
|
||||||
|
assert state is not None
|
||||||
|
assert state == snapshot
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("count", [0])
|
@pytest.mark.parametrize("count", [0])
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("config", "domain"),
|
("config", "domain"),
|
||||||
|
@ -4,9 +4,10 @@ from datetime import timedelta
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant.bootstrap import async_from_config_dict
|
from homeassistant.bootstrap import async_from_config_dict
|
||||||
from homeassistant.components import sensor
|
from homeassistant.components import sensor, template
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_PICTURE,
|
ATTR_ENTITY_PICTURE,
|
||||||
ATTR_ICON,
|
ATTR_ICON,
|
||||||
@ -25,6 +26,7 @@ from homeassistant.setup import ATTR_COMPONENT, async_setup_component
|
|||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
|
MockConfigEntry,
|
||||||
assert_setup_component,
|
assert_setup_component,
|
||||||
async_fire_time_changed,
|
async_fire_time_changed,
|
||||||
mock_restore_cache_with_extra_data,
|
mock_restore_cache_with_extra_data,
|
||||||
@ -33,6 +35,56 @@ from tests.common import (
|
|||||||
TEST_NAME = "sensor.test_template_sensor"
|
TEST_NAME = "sensor.test_template_sensor"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"config_entry_extra_options",
|
||||||
|
[
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
"device_class": "battery",
|
||||||
|
"state_class": "measurement",
|
||||||
|
"unit_of_measurement": "%",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_setup_config_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
config_entry_extra_options: dict[str, str],
|
||||||
|
) -> None:
|
||||||
|
"""Test the config flow."""
|
||||||
|
state_template = "{{ float(states('sensor.one')) + float(states('sensor.two')) }}"
|
||||||
|
input_entities = ["one", "two"]
|
||||||
|
input_states = {"one": "10", "two": "20"}
|
||||||
|
template_type = sensor.DOMAIN
|
||||||
|
|
||||||
|
for input_entity in input_entities:
|
||||||
|
hass.states.async_set(
|
||||||
|
f"{template_type}.{input_entity}",
|
||||||
|
input_states[input_entity],
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
|
||||||
|
template_config_entry = MockConfigEntry(
|
||||||
|
data={},
|
||||||
|
domain=template.DOMAIN,
|
||||||
|
options={
|
||||||
|
"name": "My template",
|
||||||
|
"state": state_template,
|
||||||
|
"template_type": template_type,
|
||||||
|
}
|
||||||
|
| config_entry_extra_options,
|
||||||
|
title="My template",
|
||||||
|
)
|
||||||
|
template_config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
assert await hass.config_entries.async_setup(template_config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get(f"{template_type}.my_template")
|
||||||
|
assert state is not None
|
||||||
|
assert state == snapshot
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(("count", "domain"), [(1, sensor.DOMAIN)])
|
@pytest.mark.parametrize(("count", "domain"), [(1, sensor.DOMAIN)])
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"config",
|
"config",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user