-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Allow specifying template entities based on triggers #48169
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7cef1c8
Add trigger entity groundwork
balloob cede608
Add some todo
balloob a988727
Rename to trigger entity and finish
balloob c86e766
Update manifest name
balloob 00f3901
Test warning
balloob 064bc2d
Lint
balloob 0f6043d
Make sensor definition required
balloob 3e24566
Use 'state' to render the state
balloob a869ce3
Merge trigger_entity into template integration
balloob cb91cc2
Handle load after start and fix test
balloob 6cbdada
Remove assert
balloob 8d71e6f
Test invalid config
balloob 480d399
Align top-level config with platform config
balloob 8b7eb03
Cleanup + test more
balloob 73d91f0
Improve warning if trigger defined in platform config
balloob b25ceb9
Apply suggestions from code review
balloob ad3031e
Update homeassistant/components/template/trigger_entity.py
balloob 3b36a9c
Black
balloob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,80 @@ | ||
| """The template component.""" | ||
| import logging | ||
| from typing import Optional | ||
|
|
||
| from homeassistant.const import CONF_SENSORS, EVENT_HOMEASSISTANT_START | ||
| from homeassistant.core import CoreState, callback | ||
| from homeassistant.helpers import ( | ||
| discovery, | ||
| trigger as trigger_helper, | ||
| update_coordinator, | ||
| ) | ||
| from homeassistant.helpers.reload import async_setup_reload_service | ||
|
|
||
| from .const import DOMAIN, PLATFORMS | ||
| from .const import CONF_TRIGGER, DOMAIN, PLATFORMS | ||
|
|
||
|
|
||
| async def async_setup(hass, config): | ||
| """Set up the template integration.""" | ||
| if DOMAIN in config: | ||
| for conf in config[DOMAIN]: | ||
| coordinator = TriggerUpdateCoordinator(hass, conf) | ||
| await coordinator.async_setup(config) | ||
|
|
||
| await async_setup_reload_service(hass, DOMAIN, PLATFORMS) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| class TriggerUpdateCoordinator(update_coordinator.DataUpdateCoordinator): | ||
| """Class to handle incoming data.""" | ||
|
|
||
| def __init__(self, hass, config): | ||
| """Instantiate trigger data.""" | ||
| super().__init__( | ||
| hass, logging.getLogger(__name__), name="Trigger Update Coordinator" | ||
| ) | ||
| self.config = config | ||
| self._unsub_trigger = None | ||
|
|
||
| @property | ||
| def unique_id(self) -> Optional[str]: | ||
| """Return unique ID for the entity.""" | ||
| return self.config.get("unique_id") | ||
|
|
||
| async def async_setup(self, hass_config): | ||
| """Set up the trigger and create entities.""" | ||
| if self.hass.state == CoreState.running: | ||
| await self._attach_triggers() | ||
| else: | ||
| self.hass.bus.async_listen_once( | ||
| EVENT_HOMEASSISTANT_START, self._attach_triggers | ||
| ) | ||
|
|
||
| self.hass.async_create_task( | ||
| discovery.async_load_platform( | ||
| self.hass, | ||
| "sensor", | ||
| DOMAIN, | ||
| {"coordinator": self, "entities": self.config[CONF_SENSORS]}, | ||
| hass_config, | ||
| ) | ||
| ) | ||
|
|
||
| async def _attach_triggers(self, start_event=None) -> None: | ||
| """Attach the triggers.""" | ||
| self._unsub_trigger = await trigger_helper.async_initialize_triggers( | ||
| self.hass, | ||
| self.config[CONF_TRIGGER], | ||
| self._handle_triggered, | ||
| DOMAIN, | ||
| self.name, | ||
| self.logger.log, | ||
| start_event is not None, | ||
| ) | ||
|
|
||
| @callback | ||
| def _handle_triggered(self, run_variables, context=None): | ||
| self.async_set_updated_data( | ||
| {"run_variables": run_variables, "context": context} | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """Template config validator.""" | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.config import async_log_exception, config_without_domain | ||
| from homeassistant.const import CONF_SENSORS, CONF_UNIQUE_ID | ||
| from homeassistant.helpers import config_validation as cv | ||
| from homeassistant.helpers.trigger import async_validate_trigger_config | ||
|
|
||
| from .const import CONF_TRIGGER, DOMAIN | ||
| from .sensor import SENSOR_SCHEMA | ||
|
|
||
| CONF_STATE = "state" | ||
|
|
||
|
|
||
| TRIGGER_ENTITY_SCHEMA = vol.Schema( | ||
| { | ||
| vol.Optional(CONF_UNIQUE_ID): cv.string, | ||
| vol.Required(CONF_TRIGGER): cv.TRIGGER_SCHEMA, | ||
| vol.Required(CONF_SENSORS): cv.schema_with_slug_keys(SENSOR_SCHEMA), | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| async def async_validate_config(hass, config): | ||
| """Validate config.""" | ||
| if DOMAIN not in config: | ||
| return config | ||
|
|
||
| trigger_entity_configs = [] | ||
|
|
||
| for cfg in cv.ensure_list(config[DOMAIN]): | ||
| try: | ||
| cfg = TRIGGER_ENTITY_SCHEMA(cfg) | ||
| cfg[CONF_TRIGGER] = await async_validate_trigger_config( | ||
| hass, cfg[CONF_TRIGGER] | ||
| ) | ||
| trigger_entity_configs.append(cfg) | ||
| except vol.Invalid as err: | ||
| async_log_exception(err, DOMAIN, cfg, hass) | ||
|
|
||
| # Create a copy of the configuration with all config for current | ||
| # component removed and add validated config back in. | ||
| config = config_without_domain(config, DOMAIN) | ||
| config[DOMAIN] = trigger_entity_configs | ||
|
|
||
| return config | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.