2016-01-15 06:51:28 +00:00
|
|
|
"""
|
|
|
|
Component to keep track of user controlled booleans for within automation.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation
|
|
|
|
at https://home-assistant.io/components/input_boolean/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-04-13 03:04:06 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-01-15 06:51:28 +00:00
|
|
|
from homeassistant.const import (
|
2016-09-23 07:12:11 +00:00
|
|
|
ATTR_ENTITY_ID, CONF_ICON, CONF_NAME, SERVICE_TURN_OFF, SERVICE_TURN_ON,
|
|
|
|
SERVICE_TOGGLE, STATE_ON)
|
2016-04-13 03:04:06 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-10-03 20:05:06 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2016-01-15 06:51:28 +00:00
|
|
|
|
|
|
|
DOMAIN = 'input_boolean'
|
|
|
|
|
|
|
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-09-23 07:12:11 +00:00
|
|
|
CONF_INITIAL = 'initial'
|
2016-01-15 06:51:28 +00:00
|
|
|
|
2016-09-18 17:18:44 +00:00
|
|
|
SERVICE_SCHEMA = vol.Schema({
|
2016-04-13 03:04:06 +00:00
|
|
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
|
|
|
})
|
|
|
|
|
2016-09-23 21:10:12 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({DOMAIN: {
|
|
|
|
cv.slug: vol.Any({
|
2016-09-23 07:12:11 +00:00
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
2016-09-23 21:10:12 +00:00
|
|
|
vol.Optional(CONF_INITIAL, default=False): cv.boolean,
|
2016-09-23 07:12:11 +00:00
|
|
|
vol.Optional(CONF_ICON): cv.icon,
|
2016-09-23 21:10:12 +00:00
|
|
|
}, None)}}, extra=vol.ALLOW_EXTRA)
|
2016-09-23 07:12:11 +00:00
|
|
|
|
2016-01-15 06:51:28 +00:00
|
|
|
|
|
|
|
def is_on(hass, entity_id):
|
|
|
|
"""Test if input_boolean is True."""
|
|
|
|
return hass.states.is_state(entity_id, STATE_ON)
|
|
|
|
|
|
|
|
|
|
|
|
def turn_on(hass, entity_id):
|
|
|
|
"""Set input_boolean to True."""
|
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id})
|
|
|
|
|
|
|
|
|
|
|
|
def turn_off(hass, entity_id):
|
|
|
|
"""Set input_boolean to False."""
|
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
|
|
|
|
|
|
|
|
|
2016-09-18 17:18:44 +00:00
|
|
|
def toggle(hass, entity_id):
|
|
|
|
"""Set input_boolean to False."""
|
|
|
|
hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
|
|
|
|
|
|
|
|
|
2016-01-15 06:51:28 +00:00
|
|
|
def setup(hass, config):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Set up input boolean."""
|
2016-01-15 06:51:28 +00:00
|
|
|
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
|
|
|
|
|
|
|
entities = []
|
|
|
|
|
|
|
|
for object_id, cfg in config[DOMAIN].items():
|
|
|
|
if not cfg:
|
|
|
|
cfg = {}
|
|
|
|
|
|
|
|
name = cfg.get(CONF_NAME)
|
|
|
|
state = cfg.get(CONF_INITIAL, False)
|
|
|
|
icon = cfg.get(CONF_ICON)
|
|
|
|
|
|
|
|
entities.append(InputBoolean(object_id, name, state, icon))
|
|
|
|
|
|
|
|
if not entities:
|
|
|
|
return False
|
|
|
|
|
2016-09-18 17:18:44 +00:00
|
|
|
def handler_service(service):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Handle a calls to the input boolean services."""
|
2016-01-15 06:51:28 +00:00
|
|
|
target_inputs = component.extract_from_service(service)
|
|
|
|
|
|
|
|
for input_b in target_inputs:
|
|
|
|
if service.service == SERVICE_TURN_ON:
|
|
|
|
input_b.turn_on()
|
2016-09-18 17:18:44 +00:00
|
|
|
elif service.service == SERVICE_TURN_OFF:
|
2016-01-15 06:51:28 +00:00
|
|
|
input_b.turn_off()
|
2016-09-18 17:18:44 +00:00
|
|
|
else:
|
|
|
|
input_b.toggle()
|
|
|
|
|
|
|
|
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handler_service,
|
|
|
|
schema=SERVICE_SCHEMA)
|
|
|
|
hass.services.register(DOMAIN, SERVICE_TURN_ON, handler_service,
|
|
|
|
schema=SERVICE_SCHEMA)
|
|
|
|
hass.services.register(DOMAIN, SERVICE_TOGGLE, handler_service,
|
|
|
|
schema=SERVICE_SCHEMA)
|
2016-01-15 06:51:28 +00:00
|
|
|
|
2016-01-15 07:25:25 +00:00
|
|
|
component.add_entities(entities)
|
|
|
|
|
2016-01-15 06:51:28 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class InputBoolean(ToggleEntity):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Representation of a boolean input."""
|
|
|
|
|
2016-01-15 06:51:28 +00:00
|
|
|
def __init__(self, object_id, name, state, icon):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize a boolean input."""
|
2016-10-03 20:05:06 +00:00
|
|
|
self.entity_id = ENTITY_ID_FORMAT.format(object_id)
|
2016-01-15 06:51:28 +00:00
|
|
|
self._name = name
|
|
|
|
self._state = state
|
|
|
|
self._icon = icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""If entity should be polled."""
|
2016-01-15 06:51:28 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Return name of the boolean input."""
|
2016-01-15 06:51:28 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Returh the icon to be used for this entity."""
|
2016-01-15 06:51:28 +00:00
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Return true if entity is on."""
|
2016-01-15 06:51:28 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the entity on."""
|
|
|
|
self._state = True
|
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the entity off."""
|
|
|
|
self._state = False
|
|
|
|
self.update_ha_state()
|