core/homeassistant/components/input_select.py

148 lines
4.1 KiB
Python
Raw Normal View History

2016-01-31 20:40:34 +00:00
"""
Component to offer a way to select an option from a list.
For more details about this component, please refer to the documentation
at https://home-assistant.io/components/input_select/
"""
import logging
import voluptuous as vol
2016-01-31 20:40:34 +00:00
from homeassistant.const import ATTR_ENTITY_ID
import homeassistant.helpers.config_validation as cv
2016-01-31 20:40:34 +00:00
from homeassistant.helpers.entity import Entity
2016-02-19 05:27:50 +00:00
from homeassistant.helpers.entity_component import EntityComponent
2016-01-31 20:40:34 +00:00
from homeassistant.util import slugify
DOMAIN = 'input_select'
ENTITY_ID_FORMAT = DOMAIN + '.{}'
_LOGGER = logging.getLogger(__name__)
CONF_NAME = 'name'
CONF_INITIAL = 'initial'
CONF_ICON = 'icon'
CONF_OPTIONS = 'options'
ATTR_OPTION = 'option'
ATTR_OPTIONS = 'options'
SERVICE_SELECT_OPTION = 'select_option'
SERVICE_SELECT_OPTION_SCHEMA = vol.Schema({
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
vol.Required(ATTR_OPTION): cv.string,
})
2016-01-31 20:40:34 +00:00
def select_option(hass, entity_id, option):
2016-03-07 17:49:31 +00:00
"""Set input_select to False."""
2016-01-31 20:40:34 +00:00
hass.services.call(DOMAIN, SERVICE_SELECT_OPTION, {
ATTR_ENTITY_ID: entity_id,
ATTR_OPTION: option,
})
def setup(hass, config):
2016-03-07 17:49:31 +00:00
"""Setup input select."""
2016-01-31 20:40:34 +00:00
if not isinstance(config.get(DOMAIN), dict):
_LOGGER.error('Expected %s config to be a dictionary', DOMAIN)
return False
component = EntityComponent(_LOGGER, DOMAIN, hass)
entities = []
for object_id, cfg in config[DOMAIN].items():
if object_id != slugify(object_id):
_LOGGER.warning("Found invalid key for boolean input: %s. "
"Use %s instead", object_id, slugify(object_id))
continue
if not cfg:
_LOGGER.warning("No configuration specified for %s", object_id)
continue
name = cfg.get(CONF_NAME)
options = cfg.get(CONF_OPTIONS)
if not isinstance(options, list) or len(options) == 0:
_LOGGER.warning('Key %s should be a list of options', CONF_OPTIONS)
continue
options = [str(val) for val in options]
state = cfg.get(CONF_INITIAL)
if state not in options:
state = options[0]
icon = cfg.get(CONF_ICON)
entities.append(InputSelect(object_id, name, state, options, icon))
if not entities:
return False
def select_option_service(call):
2016-03-07 17:49:31 +00:00
"""Handle a calls to the input select services."""
2016-01-31 20:40:34 +00:00
target_inputs = component.extract_from_service(call)
for input_select in target_inputs:
input_select.select_option(call.data[ATTR_OPTION])
2016-01-31 20:40:34 +00:00
hass.services.register(DOMAIN, SERVICE_SELECT_OPTION,
select_option_service,
schema=SERVICE_SELECT_OPTION_SCHEMA)
2016-01-31 20:40:34 +00:00
component.add_entities(entities)
return True
class InputSelect(Entity):
2016-03-08 16:55:57 +00:00
"""Representation of a select input."""
2016-01-31 20:40:34 +00:00
# pylint: disable=too-many-arguments
def __init__(self, object_id, name, state, options, icon):
2016-03-08 16:55:57 +00:00
"""Initialize a select input."""
2016-01-31 20:40:34 +00:00
self.entity_id = ENTITY_ID_FORMAT.format(object_id)
self._name = name
self._current_option = state
self._options = options
self._icon = icon
@property
def should_poll(self):
2016-03-07 17:49:31 +00:00
"""If entity should be polled."""
2016-01-31 20:40:34 +00:00
return False
@property
def name(self):
2016-03-07 17:49:31 +00:00
"""Return the name of the select input."""
2016-01-31 20:40:34 +00:00
return self._name
@property
def icon(self):
2016-03-07 17:49:31 +00:00
"""Return the icon to be used for this entity."""
2016-01-31 20:40:34 +00:00
return self._icon
@property
def state(self):
2016-03-07 17:49:31 +00:00
"""Return the state of the component."""
2016-01-31 20:40:34 +00:00
return self._current_option
@property
def state_attributes(self):
2016-03-07 17:49:31 +00:00
"""Return the state attributes."""
2016-01-31 20:40:34 +00:00
return {
ATTR_OPTIONS: self._options,
}
def select_option(self, option):
2016-03-07 17:49:31 +00:00
"""Select new option."""
2016-01-31 20:40:34 +00:00
if option not in self._options:
_LOGGER.warning('Invalid option: %s (possible options: %s)',
option, ', '.join(self._options))
return
self._current_option = option
self.update_ha_state()