Add timer reload service. (#30015)

This commit is contained in:
Alexei Chetroi authored and Paulus Schoutsen committed 2019-12-18 21:15:11 +01:00
1 parent 0adb88156d
commit 41bef4b919
2 files changed
+140 -17

No files matched your search

+39 -15
View File
@@ -4,11 +4,12 @@ import logging
import voluptuous as vol
from homeassistant.const import CONF_ICON, CONF_NAME
from homeassistant.const import CONF_ICON, CONF_NAME, SERVICE_RELOAD
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.restore_state import RestoreEntity
import homeassistant.helpers.service
import homeassistant.util.dt as dt_util
_LOGGER = logging.getLogger(__name__)
@@ -54,26 +55,31 @@ CONFIG_SCHEMA = vol.Schema(
extra=vol.ALLOW_EXTRA,
)
RELOAD_SERVICE_SCHEMA = vol.Schema({})
async def async_setup(hass, config):
"""Set up a timer."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
entities = []
entities = await _async_process_config(hass, config)
for object_id, cfg in config[DOMAIN].items():
if not cfg:
cfg = {}
name = cfg.get(CONF_NAME)
icon = cfg.get(CONF_ICON)
duration = cfg.get(CONF_DURATION)
entities.append(Timer(hass, object_id, name, icon, duration))
if not entities:
return False
async def reload_service_handler(service_call):
"""Remove all input booleans and load new ones from config."""
conf = await component.async_prepare_reload()
if conf is None:
return
new_entities = await _async_process_config(hass, conf)
if new_entities:
await component.async_add_entities(new_entities)
homeassistant.helpers.service.async_register_admin_service(
hass,
DOMAIN,
SERVICE_RELOAD,
reload_service_handler,
schema=RELOAD_SERVICE_SCHEMA,
)
component.async_register_entity_service(
SERVICE_START,
{
@@ -87,10 +93,28 @@ async def async_setup(hass, config):
component.async_register_entity_service(SERVICE_CANCEL, {}, "async_cancel")
component.async_register_entity_service(SERVICE_FINISH, {}, "async_finish")
await component.async_add_entities(entities)
if entities:
await component.async_add_entities(entities)
return True
async def _async_process_config(hass, config):
"""Process config and create list of entities."""
entities = []
for object_id, cfg in config[DOMAIN].items():
if not cfg:
cfg = {}
name = cfg.get(CONF_NAME)
icon = cfg.get(CONF_ICON)
duration = cfg.get(CONF_DURATION)
entities.append(Timer(hass, object_id, name, icon, duration))
return entities
class Timer(RestoreEntity):
"""Representation of a timer."""