Make sure groups are initialized before template sensors (#37766)

* Make sure groups are initialized before template sensors

This way users may use the `expand` function in templates to expand
groups and have HA listen for changes to group members.

Fixes #35872

* Patch async_setup_platform instead of async_setup

* Cleanup

* Use an event to avoid sleep

* Update tests/components/template/test_sensor.py

Co-authored-by: J. Nick Koston <nick@koston.org>

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Thomas Hollstegge 2020-08-09 04:34:14 +02:00 committed by GitHub
parent 34e2a1825b
commit 5de21375c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 2 deletions

View File

@ -3,5 +3,6 @@
"name": "Template", "name": "Template",
"documentation": "https://www.home-assistant.io/integrations/template", "documentation": "https://www.home-assistant.io/integrations/template",
"codeowners": ["@PhracturedBlue", "@tetienne"], "codeowners": ["@PhracturedBlue", "@tetienne"],
"quality_scale": "internal" "quality_scale": "internal",
"after_dependencies": ["group"]
} }

View File

@ -1,11 +1,16 @@
"""The test for the Template sensor platform.""" """The test for the Template sensor platform."""
from asyncio import Event
from unittest.mock import patch
from homeassistant.bootstrap import async_from_config_dict
from homeassistant.const import ( from homeassistant.const import (
EVENT_COMPONENT_LOADED,
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_START,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
) )
from homeassistant.setup import async_setup_component, setup_component from homeassistant.setup import ATTR_COMPONENT, async_setup_component, setup_component
from tests.common import assert_setup_component, get_test_home_assistant from tests.common import assert_setup_component, get_test_home_assistant
@ -438,6 +443,45 @@ class TestTemplateSensor:
) )
async def test_creating_sensor_loads_group(hass):
"""Test setting up template sensor loads group component first."""
order = []
after_dep_event = Event()
async def async_setup_group(hass, config):
# Make sure group takes longer to load, so that it won't
# be loaded first by chance
await after_dep_event.wait()
order.append("group")
return True
async def async_setup_template(
hass, config, async_add_entities, discovery_info=None
):
order.append("sensor.template")
return True
async def set_after_dep_event(event):
if event.data[ATTR_COMPONENT] == "sensor":
after_dep_event.set()
hass.bus.async_listen(EVENT_COMPONENT_LOADED, set_after_dep_event)
with patch(
"homeassistant.components.group.async_setup", new=async_setup_group,
), patch(
"homeassistant.components.template.sensor.async_setup_platform",
new=async_setup_template,
):
await async_from_config_dict(
{"sensor": {"platform": "template", "sensors": {}}, "group": {}}, hass
)
await hass.async_block_till_done()
assert order == ["group", "sensor.template"]
async def test_available_template_with_entities(hass): async def test_available_template_with_entities(hass):
"""Test availability tempalates with values from other entities.""" """Test availability tempalates with values from other entities."""
hass.states.async_set("sensor.availability_sensor", STATE_OFF) hass.states.async_set("sensor.availability_sensor", STATE_OFF)