Cleanup input_text (#9326)
parent
c539b5c12b
commit
74bfcde814
|
@ -25,17 +25,15 @@ ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||||
CONF_INITIAL = 'initial'
|
CONF_INITIAL = 'initial'
|
||||||
CONF_MIN = 'min'
|
CONF_MIN = 'min'
|
||||||
CONF_MAX = 'max'
|
CONF_MAX = 'max'
|
||||||
CONF_DISABLED = 'disabled'
|
|
||||||
|
|
||||||
ATTR_VALUE = 'value'
|
ATTR_VALUE = 'value'
|
||||||
ATTR_MIN = 'min'
|
ATTR_MIN = 'min'
|
||||||
ATTR_MAX = 'max'
|
ATTR_MAX = 'max'
|
||||||
ATTR_PATTERN = 'pattern'
|
ATTR_PATTERN = 'pattern'
|
||||||
ATTR_DISABLED = 'disabled'
|
|
||||||
|
|
||||||
SERVICE_SELECT_VALUE = 'select_value'
|
SERVICE_SET_VALUE = 'set_value'
|
||||||
|
|
||||||
SERVICE_SELECT_VALUE_SCHEMA = vol.Schema({
|
SERVICE_SET_VALUE_SCHEMA = vol.Schema({
|
||||||
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
||||||
vol.Required(ATTR_VALUE): cv.string,
|
vol.Required(ATTR_VALUE): cv.string,
|
||||||
})
|
})
|
||||||
|
@ -65,16 +63,15 @@ CONFIG_SCHEMA = vol.Schema({
|
||||||
vol.Optional(CONF_ICON): cv.icon,
|
vol.Optional(CONF_ICON): cv.icon,
|
||||||
vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string,
|
vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string,
|
||||||
vol.Optional(ATTR_PATTERN): cv.string,
|
vol.Optional(ATTR_PATTERN): cv.string,
|
||||||
vol.Optional(CONF_DISABLED, default=False): cv.boolean,
|
|
||||||
}, _cv_input_text)
|
}, _cv_input_text)
|
||||||
})
|
})
|
||||||
}, required=True, extra=vol.ALLOW_EXTRA)
|
}, required=True, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
def select_value(hass, entity_id, value):
|
def set_value(hass, entity_id, value):
|
||||||
"""Set input_text to value."""
|
"""Set input_text to value."""
|
||||||
hass.services.call(DOMAIN, SERVICE_SELECT_VALUE, {
|
hass.services.call(DOMAIN, SERVICE_SET_VALUE, {
|
||||||
ATTR_ENTITY_ID: entity_id,
|
ATTR_ENTITY_ID: entity_id,
|
||||||
ATTR_VALUE: value,
|
ATTR_VALUE: value,
|
||||||
})
|
})
|
||||||
|
@ -95,28 +92,27 @@ def async_setup(hass, config):
|
||||||
icon = cfg.get(CONF_ICON)
|
icon = cfg.get(CONF_ICON)
|
||||||
unit = cfg.get(ATTR_UNIT_OF_MEASUREMENT)
|
unit = cfg.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||||
pattern = cfg.get(ATTR_PATTERN)
|
pattern = cfg.get(ATTR_PATTERN)
|
||||||
disabled = cfg.get(CONF_DISABLED)
|
|
||||||
|
|
||||||
entities.append(InputText(
|
entities.append(InputText(
|
||||||
object_id, name, initial, minimum, maximum, icon, unit,
|
object_id, name, initial, minimum, maximum, icon, unit,
|
||||||
pattern, disabled))
|
pattern))
|
||||||
|
|
||||||
if not entities:
|
if not entities:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_select_value_service(call):
|
def async_set_value_service(call):
|
||||||
"""Handle a calls to the input box services."""
|
"""Handle a calls to the input box services."""
|
||||||
target_inputs = component.async_extract_from_service(call)
|
target_inputs = component.async_extract_from_service(call)
|
||||||
|
|
||||||
tasks = [input_text.async_select_value(call.data[ATTR_VALUE])
|
tasks = [input_text.async_set_value(call.data[ATTR_VALUE])
|
||||||
for input_text in target_inputs]
|
for input_text in target_inputs]
|
||||||
if tasks:
|
if tasks:
|
||||||
yield from asyncio.wait(tasks, loop=hass.loop)
|
yield from asyncio.wait(tasks, loop=hass.loop)
|
||||||
|
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN, SERVICE_SELECT_VALUE, async_select_value_service,
|
DOMAIN, SERVICE_SET_VALUE, async_set_value_service,
|
||||||
schema=SERVICE_SELECT_VALUE_SCHEMA)
|
schema=SERVICE_SET_VALUE_SCHEMA)
|
||||||
|
|
||||||
yield from component.async_add_entities(entities)
|
yield from component.async_add_entities(entities)
|
||||||
return True
|
return True
|
||||||
|
@ -126,8 +122,8 @@ class InputText(Entity):
|
||||||
"""Represent a text box."""
|
"""Represent a text box."""
|
||||||
|
|
||||||
def __init__(self, object_id, name, initial, minimum, maximum, icon,
|
def __init__(self, object_id, name, initial, minimum, maximum, icon,
|
||||||
unit, pattern, disabled):
|
unit, pattern):
|
||||||
"""Initialize a select input."""
|
"""Initialize a text input."""
|
||||||
self.entity_id = ENTITY_ID_FORMAT.format(object_id)
|
self.entity_id = ENTITY_ID_FORMAT.format(object_id)
|
||||||
self._name = name
|
self._name = name
|
||||||
self._current_value = initial
|
self._current_value = initial
|
||||||
|
@ -136,7 +132,6 @@ class InputText(Entity):
|
||||||
self._icon = icon
|
self._icon = icon
|
||||||
self._unit = unit
|
self._unit = unit
|
||||||
self._pattern = pattern
|
self._pattern = pattern
|
||||||
self._disabled = disabled
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
|
@ -145,7 +140,7 @@ class InputText(Entity):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the select input box."""
|
"""Return the name of the text input entity."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -163,11 +158,6 @@ class InputText(Entity):
|
||||||
"""Return the unit the value is expressed in."""
|
"""Return the unit the value is expressed in."""
|
||||||
return self._unit
|
return self._unit
|
||||||
|
|
||||||
@property
|
|
||||||
def disabled(self):
|
|
||||||
"""Return the disabled flag."""
|
|
||||||
return self._disabled
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state_attributes(self):
|
def state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
|
@ -175,7 +165,6 @@ class InputText(Entity):
|
||||||
ATTR_MIN: self._minimum,
|
ATTR_MIN: self._minimum,
|
||||||
ATTR_MAX: self._maximum,
|
ATTR_MAX: self._maximum,
|
||||||
ATTR_PATTERN: self._pattern,
|
ATTR_PATTERN: self._pattern,
|
||||||
ATTR_DISABLED: self._disabled,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
|
@ -192,7 +181,7 @@ class InputText(Entity):
|
||||||
self._current_value = value
|
self._current_value = value
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_select_value(self, value):
|
def async_set_value(self, value):
|
||||||
"""Select new value."""
|
"""Select new value."""
|
||||||
if len(value) < self._minimum or len(value) > self._maximum:
|
if len(value) < self._minimum or len(value) > self._maximum:
|
||||||
_LOGGER.warning("Invalid value: %s (length range %s - %s)",
|
_LOGGER.warning("Invalid value: %s (length range %s - %s)",
|
||||||
|
|
|
@ -5,7 +5,7 @@ import unittest
|
||||||
|
|
||||||
from homeassistant.core import CoreState, State
|
from homeassistant.core import CoreState, State
|
||||||
from homeassistant.setup import setup_component, async_setup_component
|
from homeassistant.setup import setup_component, async_setup_component
|
||||||
from homeassistant.components.input_text import (DOMAIN, select_value)
|
from homeassistant.components.input_text import (DOMAIN, set_value)
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant, mock_restore_cache
|
from tests.common import get_test_home_assistant, mock_restore_cache
|
||||||
|
|
||||||
|
@ -38,8 +38,8 @@ class TestInputText(unittest.TestCase):
|
||||||
self.assertFalse(
|
self.assertFalse(
|
||||||
setup_component(self.hass, DOMAIN, {DOMAIN: cfg}))
|
setup_component(self.hass, DOMAIN, {DOMAIN: cfg}))
|
||||||
|
|
||||||
def test_select_value(self):
|
def test_set_value(self):
|
||||||
"""Test select_value method."""
|
"""Test set_value method."""
|
||||||
self.assertTrue(setup_component(self.hass, DOMAIN, {DOMAIN: {
|
self.assertTrue(setup_component(self.hass, DOMAIN, {DOMAIN: {
|
||||||
'test_1': {
|
'test_1': {
|
||||||
'initial': 'test',
|
'initial': 'test',
|
||||||
|
@ -52,13 +52,13 @@ class TestInputText(unittest.TestCase):
|
||||||
state = self.hass.states.get(entity_id)
|
state = self.hass.states.get(entity_id)
|
||||||
self.assertEqual('test', str(state.state))
|
self.assertEqual('test', str(state.state))
|
||||||
|
|
||||||
select_value(self.hass, entity_id, 'testing')
|
set_value(self.hass, entity_id, 'testing')
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get(entity_id)
|
state = self.hass.states.get(entity_id)
|
||||||
self.assertEqual('testing', str(state.state))
|
self.assertEqual('testing', str(state.state))
|
||||||
|
|
||||||
select_value(self.hass, entity_id, 'testing too long')
|
set_value(self.hass, entity_id, 'testing too long')
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get(entity_id)
|
state = self.hass.states.get(entity_id)
|
||||||
|
|
Loading…
Reference in New Issue