Add explicit test of template config entry setup (#99345)

This commit is contained in:
Erik Montnemery 2023-08-30 19:56:34 +02:00 committed by GitHub
parent 59b1d4ba69
commit 8c04e4c7a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 158 additions and 1 deletions

View 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',
})
# ---

View 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',
})
# ---

View File

@ -5,6 +5,7 @@ from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant import setup
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
from tests.common import (
MockConfigEntry,
assert_setup_component,
async_fire_time_changed,
mock_restore_cache,
@ -123,6 +125,55 @@ async def test_setup(hass: HomeAssistant, start_ha, entity_id) -> None:
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(
("config", "domain"),

View File

@ -4,9 +4,10 @@ from datetime import timedelta
from unittest.mock import patch
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.bootstrap import async_from_config_dict
from homeassistant.components import sensor
from homeassistant.components import sensor, template
from homeassistant.const import (
ATTR_ENTITY_PICTURE,
ATTR_ICON,
@ -25,6 +26,7 @@ from homeassistant.setup import ATTR_COMPONENT, async_setup_component
import homeassistant.util.dt as dt_util
from tests.common import (
MockConfigEntry,
assert_setup_component,
async_fire_time_changed,
mock_restore_cache_with_extra_data,
@ -33,6 +35,56 @@ from tests.common import (
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(
"config",