Migrate template to register reload service on async_setup (#48273)

This commit is contained in:
Paulus Schoutsen 2021-03-23 20:35:15 -07:00 committed by GitHub
parent b58dd7d047
commit 3dec394cad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 132 additions and 174 deletions

View file

@ -1,20 +1,11 @@
"""The template component.""" """The template component."""
from homeassistant.const import SERVICE_RELOAD from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.reload import async_reload_integration_platforms
from .const import DOMAIN, EVENT_TEMPLATE_RELOADED, PLATFORMS from .const import DOMAIN, PLATFORMS
async def async_setup_reload_service(hass): async def async_setup(hass, config):
"""Create the reload service for the template domain.""" """Set up the template integration."""
if hass.services.has_service(DOMAIN, SERVICE_RELOAD): await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
return
async def _reload_config(call): return True
"""Reload the template platform config."""
await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS)
hass.bus.async_fire(EVENT_TEMPLATE_RELOADED, context=call.context)
hass.helpers.service.async_register_admin_service(
DOMAIN, SERVICE_RELOAD, _reload_config
)

View file

@ -32,10 +32,8 @@ from homeassistant.core import callback
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import DOMAIN, PLATFORMS
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -113,7 +111,6 @@ async def _async_create_entities(hass, config):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Template Alarm Control Panels.""" """Set up the Template Alarm Control Panels."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config)) async_add_entities(await _async_create_entities(hass, config))

View file

@ -22,10 +22,9 @@ from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.event import async_call_later from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.template import result_as_boolean from homeassistant.helpers.template import result_as_boolean
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
CONF_DELAY_ON = "delay_on" CONF_DELAY_ON = "delay_on"
@ -97,7 +96,6 @@ async def _async_create_entities(hass, config):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template binary sensors.""" """Set up the template binary sensors."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config)) async_add_entities(await _async_create_entities(hass, config))

View file

@ -6,8 +6,6 @@ DOMAIN = "template"
PLATFORM_STORAGE_KEY = "template_platforms" PLATFORM_STORAGE_KEY = "template_platforms"
EVENT_TEMPLATE_RELOADED = "event_template_reloaded"
PLATFORMS = [ PLATFORMS = [
"alarm_control_panel", "alarm_control_panel",
"binary_sensor", "binary_sensor",

View file

@ -38,10 +38,9 @@ from homeassistant.core import callback
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -160,7 +159,6 @@ async def _async_create_entities(hass, config):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Template cover.""" """Set up the Template cover."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config)) async_add_entities(await _async_create_entities(hass, config))

View file

@ -36,10 +36,9 @@ from homeassistant.core import callback
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -163,7 +162,6 @@ async def _async_create_entities(hass, config):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template fans.""" """Set up the template fans."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config)) async_add_entities(await _async_create_entities(hass, config))

View file

@ -31,10 +31,9 @@ from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -137,7 +136,6 @@ async def _async_create_entities(hass, config):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template lights.""" """Set up the template lights."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config)) async_add_entities(await _async_create_entities(hass, config))

View file

@ -13,10 +13,9 @@ from homeassistant.const import (
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
CONF_LOCK = "lock" CONF_LOCK = "lock"
@ -60,7 +59,6 @@ async def _async_create_entities(hass, config):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template lock.""" """Set up the template lock."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config)) async_add_entities(await _async_create_entities(hass, config))

View file

@ -25,9 +25,8 @@ from homeassistant.core import callback
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.reload import async_setup_reload_service
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
CONF_ATTRIBUTE_TEMPLATES = "attribute_templates" CONF_ATTRIBUTE_TEMPLATES = "attribute_templates"
@ -96,7 +95,6 @@ async def _async_create_entities(hass, config):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template sensors.""" """Set up the template sensors."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config)) async_add_entities(await _async_create_entities(hass, config))

View file

@ -22,11 +22,10 @@ from homeassistant.core import callback
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"] _VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"]
@ -90,7 +89,6 @@ async def _async_create_entities(hass, config):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template switches.""" """Set up the template switches."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config)) async_add_entities(await _async_create_entities(hass, config))

View file

@ -41,10 +41,9 @@ from homeassistant.core import callback
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -147,7 +146,6 @@ async def _async_create_entities(hass, config):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template vacuums.""" """Set up the template vacuums."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config)) async_add_entities(await _async_create_entities(hass, config))

View file

@ -24,9 +24,7 @@ from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.reload import async_setup_reload_service
from .const import DOMAIN, PLATFORMS
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
CONDITION_CLASSES = { CONDITION_CLASSES = {
@ -71,7 +69,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Template weather.""" """Set up the Template weather."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
name = config[CONF_NAME] name = config[CONF_NAME]
condition_template = config[CONF_CONDITION_TEMPLATE] condition_template = config[CONF_CONDITION_TEMPLATE]

View file

@ -15,7 +15,7 @@ from homeassistant.const import (
from homeassistant.core import CoreState from homeassistant.core import CoreState
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from tests.common import assert_setup_component, async_fire_time_changed from tests.common import async_fire_time_changed
async def test_setup(hass): async def test_setup(hass):
@ -32,85 +32,79 @@ async def test_setup(hass):
}, },
} }
} }
with assert_setup_component(1): assert await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
assert await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
async def test_setup_no_sensors(hass): async def test_setup_no_sensors(hass):
"""Test setup with no sensors.""" """Test setup with no sensors."""
with assert_setup_component(0): assert await setup.async_setup_component(
assert await setup.async_setup_component( hass, binary_sensor.DOMAIN, {"binary_sensor": {"platform": "template"}}
hass, binary_sensor.DOMAIN, {"binary_sensor": {"platform": "template"}} )
)
async def test_setup_invalid_device(hass): async def test_setup_invalid_device(hass):
"""Test the setup with invalid devices.""" """Test the setup with invalid devices."""
with assert_setup_component(0): assert await setup.async_setup_component(
assert await setup.async_setup_component( hass,
hass, binary_sensor.DOMAIN,
binary_sensor.DOMAIN, {"binary_sensor": {"platform": "template", "sensors": {"foo bar": {}}}},
{"binary_sensor": {"platform": "template", "sensors": {"foo bar": {}}}}, )
)
async def test_setup_invalid_device_class(hass): async def test_setup_invalid_device_class(hass):
"""Test setup with invalid sensor class.""" """Test setup with invalid sensor class."""
with assert_setup_component(0): assert await setup.async_setup_component(
assert await setup.async_setup_component( hass,
hass, binary_sensor.DOMAIN,
binary_sensor.DOMAIN, {
{ "binary_sensor": {
"binary_sensor": { "platform": "template",
"platform": "template", "sensors": {
"sensors": { "test": {
"test": { "value_template": "{{ foo }}",
"value_template": "{{ foo }}", "device_class": "foobarnotreal",
"device_class": "foobarnotreal", }
} },
}, }
} },
}, )
)
async def test_setup_invalid_missing_template(hass): async def test_setup_invalid_missing_template(hass):
"""Test setup with invalid and missing template.""" """Test setup with invalid and missing template."""
with assert_setup_component(0): assert await setup.async_setup_component(
assert await setup.async_setup_component( hass,
hass, binary_sensor.DOMAIN,
binary_sensor.DOMAIN, {
{ "binary_sensor": {
"binary_sensor": { "platform": "template",
"platform": "template", "sensors": {"test": {"device_class": "motion"}},
"sensors": {"test": {"device_class": "motion"}}, }
} },
}, )
)
async def test_icon_template(hass): async def test_icon_template(hass):
"""Test icon template.""" """Test icon template."""
with assert_setup_component(1): assert await setup.async_setup_component(
assert await setup.async_setup_component( hass,
hass, binary_sensor.DOMAIN,
binary_sensor.DOMAIN, {
{ "binary_sensor": {
"binary_sensor": { "platform": "template",
"platform": "template", "sensors": {
"sensors": { "test_template_sensor": {
"test_template_sensor": { "value_template": "{{ states.sensor.xyz.state }}",
"value_template": "{{ states.sensor.xyz.state }}", "icon_template": "{% if "
"icon_template": "{% if " "states.binary_sensor.test_state.state == "
"states.binary_sensor.test_state.state == " "'Works' %}"
"'Works' %}" "mdi:check"
"mdi:check" "{% endif %}",
"{% endif %}", }
} },
}, }
} },
}, )
)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_start() await hass.async_start()
@ -127,26 +121,25 @@ async def test_icon_template(hass):
async def test_entity_picture_template(hass): async def test_entity_picture_template(hass):
"""Test entity_picture template.""" """Test entity_picture template."""
with assert_setup_component(1): assert await setup.async_setup_component(
assert await setup.async_setup_component( hass,
hass, binary_sensor.DOMAIN,
binary_sensor.DOMAIN, {
{ "binary_sensor": {
"binary_sensor": { "platform": "template",
"platform": "template", "sensors": {
"sensors": { "test_template_sensor": {
"test_template_sensor": { "value_template": "{{ states.sensor.xyz.state }}",
"value_template": "{{ states.sensor.xyz.state }}", "entity_picture_template": "{% if "
"entity_picture_template": "{% if " "states.binary_sensor.test_state.state == "
"states.binary_sensor.test_state.state == " "'Works' %}"
"'Works' %}" "/local/sensor.png"
"/local/sensor.png" "{% endif %}",
"{% endif %}", }
} },
}, }
} },
}, )
)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_start() await hass.async_start()
@ -163,24 +156,23 @@ async def test_entity_picture_template(hass):
async def test_attribute_templates(hass): async def test_attribute_templates(hass):
"""Test attribute_templates template.""" """Test attribute_templates template."""
with assert_setup_component(1): assert await setup.async_setup_component(
assert await setup.async_setup_component( hass,
hass, binary_sensor.DOMAIN,
binary_sensor.DOMAIN, {
{ "binary_sensor": {
"binary_sensor": { "platform": "template",
"platform": "template", "sensors": {
"sensors": { "test_template_sensor": {
"test_template_sensor": { "value_template": "{{ states.sensor.xyz.state }}",
"value_template": "{{ states.sensor.xyz.state }}", "attribute_templates": {
"attribute_templates": { "test_attribute": "It {{ states.sensor.test_state.state }}."
"test_attribute": "It {{ states.sensor.test_state.state }}." },
}, }
} },
}, }
} },
}, )
)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_start() await hass.async_start()
@ -202,35 +194,34 @@ async def test_match_all(hass):
"homeassistant.components.template.binary_sensor." "homeassistant.components.template.binary_sensor."
"BinarySensorTemplate._update_state" "BinarySensorTemplate._update_state"
) as _update_state: ) as _update_state:
with assert_setup_component(1): assert await setup.async_setup_component(
assert await setup.async_setup_component( hass,
hass, binary_sensor.DOMAIN,
binary_sensor.DOMAIN, {
{ "binary_sensor": {
"binary_sensor": { "platform": "template",
"platform": "template", "sensors": {
"sensors": { "match_all_template_sensor": {
"match_all_template_sensor": { "value_template": (
"value_template": ( "{% for state in states %}"
"{% for state in states %}" "{% if state.entity_id == 'sensor.humidity' %}"
"{% if state.entity_id == 'sensor.humidity' %}" "{{ state.entity_id }}={{ state.state }}"
"{{ state.entity_id }}={{ state.state }}" "{% endif %}"
"{% endif %}" "{% endfor %}"
"{% endfor %}" ),
),
},
}, },
} },
}, }
) },
)
await hass.async_start() await hass.async_start()
await hass.async_block_till_done() await hass.async_block_till_done()
init_calls = len(_update_state.mock_calls) init_calls = len(_update_state.mock_calls)
hass.states.async_set("sensor.any_state", "update") hass.states.async_set("sensor.any_state", "update")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(_update_state.mock_calls) == init_calls assert len(_update_state.mock_calls) == init_calls
async def test_event(hass): async def test_event(hass):
@ -247,8 +238,7 @@ async def test_event(hass):
}, },
} }
} }
with assert_setup_component(1): assert await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
assert await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_start() await hass.async_start()

View file

@ -4,7 +4,8 @@ from os import path
from unittest.mock import patch from unittest.mock import patch
from homeassistant import config from homeassistant import config
from homeassistant.components.template import DOMAIN, SERVICE_RELOAD from homeassistant.components.template import DOMAIN
from homeassistant.helpers.reload import SERVICE_RELOAD
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util