Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate template to register reload service on async_setup #48273

Merged
merged 1 commit into from Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 6 additions & 15 deletions homeassistant/components/template/__init__.py
@@ -1,20 +1,11 @@
"""The template component."""
from homeassistant.const import SERVICE_RELOAD
from homeassistant.helpers.reload import async_reload_integration_platforms
from homeassistant.helpers.reload import async_setup_reload_service

from .const import DOMAIN, EVENT_TEMPLATE_RELOADED, PLATFORMS
from .const import DOMAIN, PLATFORMS


async def async_setup_reload_service(hass):
"""Create the reload service for the template domain."""
if hass.services.has_service(DOMAIN, SERVICE_RELOAD):
return
async def async_setup(hass, config):
"""Set up the template integration."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)

async def _reload_config(call):
"""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
)
return True
3 changes: 0 additions & 3 deletions homeassistant/components/template/alarm_control_panel.py
Expand Up @@ -32,10 +32,8 @@
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
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 .const import DOMAIN, PLATFORMS
from .template_entity import TemplateEntity

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -113,7 +111,6 @@ async def _async_create_entities(hass, config):

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Template Alarm Control Panels."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config))


Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/template/binary_sensor.py
Expand Up @@ -22,10 +22,9 @@
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id
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 .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity

CONF_DELAY_ON = "delay_on"
Expand Down Expand Up @@ -97,7 +96,6 @@ async def _async_create_entities(hass, config):

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template binary sensors."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config))


Expand Down
2 changes: 0 additions & 2 deletions homeassistant/components/template/const.py
Expand Up @@ -6,8 +6,6 @@

PLATFORM_STORAGE_KEY = "template_platforms"

EVENT_TEMPLATE_RELOADED = "event_template_reloaded"

PLATFORMS = [
"alarm_control_panel",
"binary_sensor",
Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/template/cover.py
Expand Up @@ -38,10 +38,9 @@
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
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 .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -160,7 +159,6 @@ async def _async_create_entities(hass, config):

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Template cover."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config))


Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/template/fan.py
Expand Up @@ -36,10 +36,9 @@
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
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 .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -163,7 +162,6 @@ async def _async_create_entities(hass, config):

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template fans."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config))


Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/template/light.py
Expand Up @@ -31,10 +31,9 @@
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
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 .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -137,7 +136,6 @@ async def _async_create_entities(hass, config):

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template lights."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config))


Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/template/lock.py
Expand Up @@ -13,10 +13,9 @@
from homeassistant.core import callback
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.reload import async_setup_reload_service
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

CONF_LOCK = "lock"
Expand Down Expand Up @@ -60,7 +59,6 @@ async def _async_create_entities(hass, config):

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template lock."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config))


Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/template/sensor.py
Expand Up @@ -25,9 +25,8 @@
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
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

CONF_ATTRIBUTE_TEMPLATES = "attribute_templates"
Expand Down Expand Up @@ -96,7 +95,6 @@ async def _async_create_entities(hass, config):

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template sensors."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config))


Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/template/switch.py
Expand Up @@ -22,11 +22,10 @@
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
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.script import Script

from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity

_VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"]
Expand Down Expand Up @@ -90,7 +89,6 @@ async def _async_create_entities(hass, config):

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template switches."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config))


Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/template/vacuum.py
Expand Up @@ -41,10 +41,9 @@
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
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 .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
from .const import CONF_AVAILABILITY_TEMPLATE
from .template_entity import TemplateEntity

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -147,7 +146,6 @@ async def _async_create_entities(hass, config):

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template vacuums."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config))


Expand Down
3 changes: 0 additions & 3 deletions homeassistant/components/template/weather.py
Expand Up @@ -24,9 +24,7 @@
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
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

CONDITION_CLASSES = {
Expand Down Expand Up @@ -71,7 +69,6 @@

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Template weather."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)

name = config[CONF_NAME]
condition_template = config[CONF_CONDITION_TEMPLATE]
Expand Down
12 changes: 1 addition & 11 deletions tests/components/template/test_binary_sensor.py
Expand Up @@ -15,7 +15,7 @@
from homeassistant.core import CoreState
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
Copy link
Member Author

@balloob balloob Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is just whitespace changes: https://github.com/home-assistant/core/pull/48273/files?w=1

I removed assert_setup_component which is 馃し and it broke now template was another component set up.



async def test_setup(hass):
Expand All @@ -32,21 +32,18 @@ async def test_setup(hass):
},
}
}
with assert_setup_component(1):
assert await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)


async def test_setup_no_sensors(hass):
"""Test setup with no sensors."""
with assert_setup_component(0):
assert await setup.async_setup_component(
hass, binary_sensor.DOMAIN, {"binary_sensor": {"platform": "template"}}
)


async def test_setup_invalid_device(hass):
"""Test the setup with invalid devices."""
with assert_setup_component(0):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
Expand All @@ -56,7 +53,6 @@ async def test_setup_invalid_device(hass):

async def test_setup_invalid_device_class(hass):
"""Test setup with invalid sensor class."""
with assert_setup_component(0):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
Expand All @@ -76,7 +72,6 @@ async def test_setup_invalid_device_class(hass):

async def test_setup_invalid_missing_template(hass):
"""Test setup with invalid and missing template."""
with assert_setup_component(0):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
Expand All @@ -91,7 +86,6 @@ async def test_setup_invalid_missing_template(hass):

async def test_icon_template(hass):
"""Test icon template."""
with assert_setup_component(1):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
Expand Down Expand Up @@ -127,7 +121,6 @@ async def test_icon_template(hass):

async def test_entity_picture_template(hass):
"""Test entity_picture template."""
with assert_setup_component(1):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
Expand Down Expand Up @@ -163,7 +156,6 @@ async def test_entity_picture_template(hass):

async def test_attribute_templates(hass):
"""Test attribute_templates template."""
with assert_setup_component(1):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
Expand Down Expand Up @@ -202,7 +194,6 @@ async def test_match_all(hass):
"homeassistant.components.template.binary_sensor."
"BinarySensorTemplate._update_state"
) as _update_state:
with assert_setup_component(1):
assert await setup.async_setup_component(
hass,
binary_sensor.DOMAIN,
Expand Down Expand Up @@ -247,7 +238,6 @@ async def test_event(hass):
},
}
}
with assert_setup_component(1):
assert await setup.async_setup_component(hass, binary_sensor.DOMAIN, config)

await hass.async_block_till_done()
Expand Down
3 changes: 2 additions & 1 deletion tests/components/template/test_init.py
Expand Up @@ -4,7 +4,8 @@
from unittest.mock import patch

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.util import dt as dt_util

Expand Down