Add input_select reload service. (#29647)

* Add input_select reload service.

* Add test.
This commit is contained in:
Alexei Chetroi authored and Paulus Schoutsen committed 2019-12-09 21:15:32 +01:00
1 parent 8c1cdc0cf7
commit 454cc684e4
3 files changed
+125 -12

No files matched your search

@@ -3,10 +3,11 @@ 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.restore_state import RestoreEntity
import homeassistant.helpers.service
_LOGGER = logging.getLogger(__name__)
@@ -61,23 +62,31 @@ CONFIG_SCHEMA = vol.Schema(
required=True,
extra=vol.ALLOW_EXTRA,
)
RELOAD_SERVICE_SCHEMA = vol.Schema({})
async def async_setup(hass, config):
"""Set up an input select."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
entities = []
entities = await _async_process_config(config)
for object_id, cfg in config[DOMAIN].items():
name = cfg.get(CONF_NAME)
options = cfg.get(CONF_OPTIONS)
initial = cfg.get(CONF_INITIAL)
icon = cfg.get(CONF_ICON)
entities.append(InputSelect(object_id, name, initial, options, icon))
async def reload_service_handler(service_call):
"""Remove all entities and load new ones from config."""
conf = await component.async_prepare_reload()
if conf is None:
return
new_entities = await _async_process_config(conf)
if new_entities:
await component.async_add_entities(new_entities)
if not entities:
return False
homeassistant.helpers.service.async_register_admin_service(
hass,
DOMAIN,
SERVICE_RELOAD,
reload_service_handler,
schema=RELOAD_SERVICE_SCHEMA,
)
component.async_register_entity_service(
SERVICE_SELECT_OPTION,
@@ -103,10 +112,25 @@ async def async_setup(hass, config):
"async_set_options",
)
await component.async_add_entities(entities)
if entities:
await component.async_add_entities(entities)
return True
async def _async_process_config(config):
"""Process config and create list of entities."""
entities = []
for object_id, cfg in config[DOMAIN].items():
name = cfg.get(CONF_NAME)
options = cfg.get(CONF_OPTIONS)
initial = cfg.get(CONF_INITIAL)
icon = cfg.get(CONF_ICON)
entities.append(InputSelect(object_id, name, initial, options, icon))
return entities
class InputSelect(RestoreEntity):
"""Representation of a select input."""
@@ -20,3 +20,5 @@ set_options:
for., example: input_select.my_select}
options: {description: Options for the input select entity., example: '["Item
A", "Item B", "Item C"]'}
reload:
description: Reload the input_select configuration.