Add input_text reload service. (#29644)
* Add input_text reload service. * Add test.
This commit is contained in:
1 parent
1222aa8c56
commit
8c1cdc0cf7
3 files changed
+102
-10
No files matched your search
@@ -9,10 +9,12 @@ from homeassistant.const import (
|
||||
CONF_ICON,
|
||||
CONF_MODE,
|
||||
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__)
|
||||
|
||||
@@ -76,12 +78,43 @@ 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 text box."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
|
||||
entities = await _async_process_config(config)
|
||||
|
||||
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)
|
||||
|
||||
homeassistant.helpers.service.async_register_admin_service(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
reload_service_handler,
|
||||
schema=RELOAD_SERVICE_SCHEMA,
|
||||
)
|
||||
|
||||
component.async_register_entity_service(
|
||||
SERVICE_SET_VALUE, {vol.Required(ATTR_VALUE): cv.string}, "async_set_value"
|
||||
)
|
||||
|
||||
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():
|
||||
@@ -102,15 +135,7 @@ async def async_setup(hass, config):
|
||||
)
|
||||
)
|
||||
|
||||
if not entities:
|
||||
return False
|
||||
|
||||
component.async_register_entity_service(
|
||||
SERVICE_SET_VALUE, {vol.Required(ATTR_VALUE): cv.string}, "async_set_value"
|
||||
)
|
||||
|
||||
await component.async_add_entities(entities)
|
||||
return True
|
||||
return entities
|
||||
|
||||
|
||||
class InputText(RestoreEntity):
|
||||
|
||||
@@ -4,3 +4,5 @@ set_value:
|
||||
entity_id: {description: Entity id of the input text to set the new value., example: input_text.text1}
|
||||
value: {description: The target value the entity should be set to., example: This
|
||||
is an example text}
|
||||
reload:
|
||||
description: Reload the input_text configuration.
|
||||
Reference in new issue
Block a user