Added increment + decrement to input_number (#9870)
* Added increment + decrement to input_number * Lint * Fix tests * Another lint * Additional testing * Added service descriptions * Consolidated service registration * Shortened service registration * Fixed service descriptions * Fix Lint
This commit is contained in:
parent
632466bb56
commit
6c39e1ef19
3 changed files with 165 additions and 11 deletions
|
@ -4,11 +4,13 @@ Component to offer a way to set a numeric value from a slider or text box.
|
||||||
For more details about this component, please refer to the documentation
|
For more details about this component, please refer to the documentation
|
||||||
at https://home-assistant.io/components/input_number/
|
at https://home-assistant.io/components/input_number/
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.config import load_yaml_config_file
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID, ATTR_UNIT_OF_MEASUREMENT, CONF_ICON, CONF_NAME)
|
ATTR_ENTITY_ID, ATTR_UNIT_OF_MEASUREMENT, CONF_ICON, CONF_NAME)
|
||||||
|
@ -38,6 +40,12 @@ ATTR_STEP = 'step'
|
||||||
ATTR_MODE = 'mode'
|
ATTR_MODE = 'mode'
|
||||||
|
|
||||||
SERVICE_SET_VALUE = 'set_value'
|
SERVICE_SET_VALUE = 'set_value'
|
||||||
|
SERVICE_INCREMENT = 'increment'
|
||||||
|
SERVICE_DECREMENT = 'decrement'
|
||||||
|
|
||||||
|
SERVICE_DEFAULT_SCHEMA = vol.Schema({
|
||||||
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids
|
||||||
|
})
|
||||||
|
|
||||||
SERVICE_SET_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,
|
||||||
|
@ -77,6 +85,19 @@ CONFIG_SCHEMA = vol.Schema({
|
||||||
}, required=True, extra=vol.ALLOW_EXTRA)
|
}, required=True, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
|
||||||
|
SERVICE_TO_METHOD = {
|
||||||
|
SERVICE_SET_VALUE: {
|
||||||
|
'method': 'async_set_value',
|
||||||
|
'schema': SERVICE_SET_VALUE_SCHEMA},
|
||||||
|
SERVICE_INCREMENT: {
|
||||||
|
'method': 'async_increment',
|
||||||
|
'schema': SERVICE_DEFAULT_SCHEMA},
|
||||||
|
SERVICE_DECREMENT: {
|
||||||
|
'method': 'async_decrement',
|
||||||
|
'schema': SERVICE_DEFAULT_SCHEMA},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
def set_value(hass, entity_id, value):
|
def set_value(hass, entity_id, value):
|
||||||
"""Set input_number to value."""
|
"""Set input_number to value."""
|
||||||
|
@ -86,6 +107,22 @@ def set_value(hass, entity_id, value):
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@bind_hass
|
||||||
|
def increment(hass, entity_id):
|
||||||
|
"""Increment value of entity."""
|
||||||
|
hass.services.call(DOMAIN, SERVICE_INCREMENT, {
|
||||||
|
ATTR_ENTITY_ID: entity_id
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@bind_hass
|
||||||
|
def decrement(hass, entity_id):
|
||||||
|
"""Decrement value of entity."""
|
||||||
|
hass.services.call(DOMAIN, SERVICE_DECREMENT, {
|
||||||
|
ATTR_ENTITY_ID: entity_id
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_setup(hass, config):
|
def async_setup(hass, config):
|
||||||
"""Set up an input slider."""
|
"""Set up an input slider."""
|
||||||
|
@ -111,18 +148,32 @@ def async_setup(hass, config):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_set_value_service(call):
|
def async_handle_service(service):
|
||||||
"""Handle a calls to the input slider services."""
|
"""Handle calls to input_number services."""
|
||||||
target_inputs = component.async_extract_from_service(call)
|
target_inputs = component.async_extract_from_service(service)
|
||||||
|
method = SERVICE_TO_METHOD.get(service.service)
|
||||||
|
params = service.data.copy()
|
||||||
|
params.pop(ATTR_ENTITY_ID, None)
|
||||||
|
|
||||||
tasks = [input_number.async_set_value(call.data[ATTR_VALUE])
|
# call method
|
||||||
for input_number in target_inputs]
|
update_tasks = []
|
||||||
if tasks:
|
for target_input in target_inputs:
|
||||||
yield from asyncio.wait(tasks, loop=hass.loop)
|
yield from getattr(target_input, method['method'])(**params)
|
||||||
|
if not target_input.should_poll:
|
||||||
|
continue
|
||||||
|
update_tasks.append(target_input.async_update_ha_state(True))
|
||||||
|
|
||||||
hass.services.async_register(
|
if update_tasks:
|
||||||
DOMAIN, SERVICE_SET_VALUE, async_set_value_service,
|
yield from asyncio.wait(update_tasks, loop=hass.loop)
|
||||||
schema=SERVICE_SET_VALUE_SCHEMA)
|
|
||||||
|
descriptions = yield from hass.async_add_job(
|
||||||
|
load_yaml_config_file, os.path.join(
|
||||||
|
os.path.dirname(__file__), 'services.yaml'))
|
||||||
|
|
||||||
|
for service, data in SERVICE_TO_METHOD.items():
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN, service, async_handle_service,
|
||||||
|
description=descriptions[DOMAIN][service], schema=data['schema'])
|
||||||
|
|
||||||
yield from component.async_add_entities(entities)
|
yield from component.async_add_entities(entities)
|
||||||
return True
|
return True
|
||||||
|
@ -204,3 +255,25 @@ class InputNumber(Entity):
|
||||||
return
|
return
|
||||||
self._current_value = num_value
|
self._current_value = num_value
|
||||||
yield from self.async_update_ha_state()
|
yield from self.async_update_ha_state()
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_increment(self):
|
||||||
|
"""Increment value."""
|
||||||
|
new_value = self._current_value + self._step
|
||||||
|
if new_value > self._maximum:
|
||||||
|
_LOGGER.warning("Invalid value: %s (range %s - %s)",
|
||||||
|
new_value, self._minimum, self._maximum)
|
||||||
|
return
|
||||||
|
self._current_value = new_value
|
||||||
|
yield from self.async_update_ha_state()
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_decrement(self):
|
||||||
|
"""Decrement value."""
|
||||||
|
new_value = self._current_value - self._step
|
||||||
|
if new_value < self._minimum:
|
||||||
|
_LOGGER.warning("Invalid value: %s (range %s - %s)",
|
||||||
|
new_value, self._minimum, self._maximum)
|
||||||
|
return
|
||||||
|
self._current_value = new_value
|
||||||
|
yield from self.async_update_ha_state()
|
||||||
|
|
|
@ -543,6 +543,34 @@ input_boolean:
|
||||||
description: Entity id of the input boolean to turn on
|
description: Entity id of the input boolean to turn on
|
||||||
example: 'input_boolean.notify_alerts'
|
example: 'input_boolean.notify_alerts'
|
||||||
|
|
||||||
|
input_number:
|
||||||
|
set_value:
|
||||||
|
description: Set the value of an input_number entity.
|
||||||
|
|
||||||
|
fields:
|
||||||
|
entity_id:
|
||||||
|
description: Entity id of the input_number to set the new value.
|
||||||
|
example: 'input_number.threshold'
|
||||||
|
value:
|
||||||
|
description: The target value the entity should be set to.
|
||||||
|
example: 42
|
||||||
|
|
||||||
|
increment:
|
||||||
|
description: Increment the value of an input_number entity by its stepping.
|
||||||
|
|
||||||
|
fields:
|
||||||
|
entity_id:
|
||||||
|
description: Entity id of the input_number the should be incremented.
|
||||||
|
example: 'input_number.threshold'
|
||||||
|
|
||||||
|
decrement:
|
||||||
|
description: Decrement the value of an input_number entity by its stepping.
|
||||||
|
|
||||||
|
fields:
|
||||||
|
entity_id:
|
||||||
|
description: Entity id of the input_number the should be decremented.
|
||||||
|
example: 'input_number.threshold'
|
||||||
|
|
||||||
homeassistant:
|
homeassistant:
|
||||||
check_config:
|
check_config:
|
||||||
description: Check the Home Assistant configuration files for errors. Errors will be displayed in the Home Assistant log.
|
description: Check the Home Assistant configuration files for errors. Errors will be displayed in the Home Assistant log.
|
||||||
|
|
|
@ -5,7 +5,8 @@ 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_number import (DOMAIN, set_value)
|
from homeassistant.components.input_number import (
|
||||||
|
DOMAIN, set_value, increment, decrement)
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant, mock_restore_cache
|
from tests.common import get_test_home_assistant, mock_restore_cache
|
||||||
|
|
||||||
|
@ -70,6 +71,58 @@ class TestInputNumber(unittest.TestCase):
|
||||||
state = self.hass.states.get(entity_id)
|
state = self.hass.states.get(entity_id)
|
||||||
self.assertEqual(70, float(state.state))
|
self.assertEqual(70, float(state.state))
|
||||||
|
|
||||||
|
def test_increment(self):
|
||||||
|
"""Test increment method."""
|
||||||
|
self.assertTrue(setup_component(self.hass, DOMAIN, {DOMAIN: {
|
||||||
|
'test_2': {
|
||||||
|
'initial': 50,
|
||||||
|
'min': 0,
|
||||||
|
'max': 51,
|
||||||
|
},
|
||||||
|
}}))
|
||||||
|
entity_id = 'input_number.test_2'
|
||||||
|
|
||||||
|
state = self.hass.states.get(entity_id)
|
||||||
|
self.assertEqual(50, float(state.state))
|
||||||
|
|
||||||
|
increment(self.hass, entity_id)
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get(entity_id)
|
||||||
|
self.assertEqual(51, float(state.state))
|
||||||
|
|
||||||
|
increment(self.hass, entity_id)
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get(entity_id)
|
||||||
|
self.assertEqual(51, float(state.state))
|
||||||
|
|
||||||
|
def test_decrement(self):
|
||||||
|
"""Test decrement method."""
|
||||||
|
self.assertTrue(setup_component(self.hass, DOMAIN, {DOMAIN: {
|
||||||
|
'test_3': {
|
||||||
|
'initial': 50,
|
||||||
|
'min': 49,
|
||||||
|
'max': 100,
|
||||||
|
},
|
||||||
|
}}))
|
||||||
|
entity_id = 'input_number.test_3'
|
||||||
|
|
||||||
|
state = self.hass.states.get(entity_id)
|
||||||
|
self.assertEqual(50, float(state.state))
|
||||||
|
|
||||||
|
decrement(self.hass, entity_id)
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get(entity_id)
|
||||||
|
self.assertEqual(49, float(state.state))
|
||||||
|
|
||||||
|
decrement(self.hass, entity_id)
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get(entity_id)
|
||||||
|
self.assertEqual(49, float(state.state))
|
||||||
|
|
||||||
def test_mode(self):
|
def test_mode(self):
|
||||||
"""Test mode settings."""
|
"""Test mode settings."""
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue