mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add entity ID to input_number warning (#32012)
* Add entity ID to warning * Input Number flexibility
This commit is contained in:
parent
2ad1f7fd02
commit
51b2d0b4f8
@ -288,44 +288,22 @@ class InputNumber(RestoreEntity):
|
|||||||
async def async_set_value(self, value):
|
async def async_set_value(self, value):
|
||||||
"""Set new value."""
|
"""Set new value."""
|
||||||
num_value = float(value)
|
num_value = float(value)
|
||||||
|
|
||||||
if num_value < self._minimum or num_value > self._maximum:
|
if num_value < self._minimum or num_value > self._maximum:
|
||||||
_LOGGER.warning(
|
raise vol.Invalid(
|
||||||
"Invalid value: %s (range %s - %s)",
|
f"Invalid value for {self.entity_id}: {value} (range {self._minimum} - {self._maximum})"
|
||||||
num_value,
|
|
||||||
self._minimum,
|
|
||||||
self._maximum,
|
|
||||||
)
|
)
|
||||||
return
|
|
||||||
self._current_value = num_value
|
self._current_value = num_value
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_increment(self):
|
async def async_increment(self):
|
||||||
"""Increment value."""
|
"""Increment value."""
|
||||||
new_value = self._current_value + self._step
|
await self.async_set_value(min(self._current_value + self._step, self._maximum))
|
||||||
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
|
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
async def async_decrement(self):
|
async def async_decrement(self):
|
||||||
"""Decrement value."""
|
"""Decrement value."""
|
||||||
new_value = self._current_value - self._step
|
await self.async_set_value(max(self._current_value - self._step, self._minimum))
|
||||||
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
|
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
async def async_update_config(self, config: typing.Dict) -> None:
|
async def async_update_config(self, config: typing.Dict) -> None:
|
||||||
"""Handle when the config is updated."""
|
"""Handle when the config is updated."""
|
||||||
|
@ -3,6 +3,8 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
from typing import Iterable, Optional
|
from typing import Iterable, Optional
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
from homeassistant.const import ATTR_ENTITY_ID
|
||||||
from homeassistant.core import Context, State
|
from homeassistant.core import Context, State
|
||||||
from homeassistant.helpers.typing import HomeAssistantType
|
from homeassistant.helpers.typing import HomeAssistantType
|
||||||
@ -37,9 +39,13 @@ async def _async_reproduce_state(
|
|||||||
service = SERVICE_SET_VALUE
|
service = SERVICE_SET_VALUE
|
||||||
service_data = {ATTR_ENTITY_ID: state.entity_id, ATTR_VALUE: state.state}
|
service_data = {ATTR_ENTITY_ID: state.entity_id, ATTR_VALUE: state.state}
|
||||||
|
|
||||||
await hass.services.async_call(
|
try:
|
||||||
DOMAIN, service, service_data, context=context, blocking=True
|
await hass.services.async_call(
|
||||||
)
|
DOMAIN, service, service_data, context=context, blocking=True
|
||||||
|
)
|
||||||
|
except vol.Invalid as err:
|
||||||
|
# If value out of range.
|
||||||
|
_LOGGER.warning("Unable to reproduce state for %s: %s", state.entity_id, err)
|
||||||
|
|
||||||
|
|
||||||
async def async_reproduce_states(
|
async def async_reproduce_states(
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.input_number import (
|
from homeassistant.components.input_number import (
|
||||||
ATTR_VALUE,
|
ATTR_VALUE,
|
||||||
@ -21,7 +22,6 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import Context, CoreState, State
|
from homeassistant.core import Context, CoreState, State
|
||||||
from homeassistant.exceptions import Unauthorized
|
from homeassistant.exceptions import Unauthorized
|
||||||
from homeassistant.helpers import entity_registry
|
from homeassistant.helpers import entity_registry
|
||||||
from homeassistant.loader import bind_hass
|
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import mock_restore_cache
|
from tests.common import mock_restore_cache
|
||||||
@ -63,38 +63,36 @@ def storage_setup(hass, hass_storage):
|
|||||||
return _storage
|
return _storage
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
async def set_value(hass, entity_id, value):
|
||||||
def set_value(hass, entity_id, value):
|
|
||||||
"""Set input_number to value.
|
"""Set input_number to value.
|
||||||
|
|
||||||
This is a legacy helper method. Do not use it for new tests.
|
This is a legacy helper method. Do not use it for new tests.
|
||||||
"""
|
"""
|
||||||
hass.async_create_task(
|
await hass.services.async_call(
|
||||||
hass.services.async_call(
|
DOMAIN,
|
||||||
DOMAIN, SERVICE_SET_VALUE, {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: value}
|
SERVICE_SET_VALUE,
|
||||||
)
|
{ATTR_ENTITY_ID: entity_id, ATTR_VALUE: value},
|
||||||
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
async def increment(hass, entity_id):
|
||||||
def increment(hass, entity_id):
|
|
||||||
"""Increment value of entity.
|
"""Increment value of entity.
|
||||||
|
|
||||||
This is a legacy helper method. Do not use it for new tests.
|
This is a legacy helper method. Do not use it for new tests.
|
||||||
"""
|
"""
|
||||||
hass.async_create_task(
|
await hass.services.async_call(
|
||||||
hass.services.async_call(DOMAIN, SERVICE_INCREMENT, {ATTR_ENTITY_ID: entity_id})
|
DOMAIN, SERVICE_INCREMENT, {ATTR_ENTITY_ID: entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
async def decrement(hass, entity_id):
|
||||||
def decrement(hass, entity_id):
|
|
||||||
"""Decrement value of entity.
|
"""Decrement value of entity.
|
||||||
|
|
||||||
This is a legacy helper method. Do not use it for new tests.
|
This is a legacy helper method. Do not use it for new tests.
|
||||||
"""
|
"""
|
||||||
hass.async_create_task(
|
await hass.services.async_call(
|
||||||
hass.services.async_call(DOMAIN, SERVICE_DECREMENT, {ATTR_ENTITY_ID: entity_id})
|
DOMAIN, SERVICE_DECREMENT, {ATTR_ENTITY_ID: entity_id}, blocking=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -110,7 +108,7 @@ async def test_config(hass):
|
|||||||
assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg})
|
assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg})
|
||||||
|
|
||||||
|
|
||||||
async def test_set_value(hass):
|
async def test_set_value(hass, caplog):
|
||||||
"""Test set_value method."""
|
"""Test set_value method."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass, DOMAIN, {DOMAIN: {"test_1": {"initial": 50, "min": 0, "max": 100}}}
|
hass, DOMAIN, {DOMAIN: {"test_1": {"initial": 50, "min": 0, "max": 100}}}
|
||||||
@ -120,20 +118,22 @@ async def test_set_value(hass):
|
|||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert 50 == float(state.state)
|
assert 50 == float(state.state)
|
||||||
|
|
||||||
set_value(hass, entity_id, "30.4")
|
await set_value(hass, entity_id, "30.4")
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert 30.4 == float(state.state)
|
assert 30.4 == float(state.state)
|
||||||
|
|
||||||
set_value(hass, entity_id, "70")
|
await set_value(hass, entity_id, "70")
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert 70 == float(state.state)
|
assert 70 == float(state.state)
|
||||||
|
|
||||||
set_value(hass, entity_id, "110")
|
with pytest.raises(vol.Invalid) as excinfo:
|
||||||
await hass.async_block_till_done()
|
await set_value(hass, entity_id, "110")
|
||||||
|
|
||||||
|
assert "Invalid value for input_number.test_1: 110.0 (range 0.0 - 100.0)" in str(
|
||||||
|
excinfo.value
|
||||||
|
)
|
||||||
|
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert 70 == float(state.state)
|
assert 70 == float(state.state)
|
||||||
@ -149,13 +149,13 @@ async def test_increment(hass):
|
|||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert 50 == float(state.state)
|
assert 50 == float(state.state)
|
||||||
|
|
||||||
increment(hass, entity_id)
|
await increment(hass, entity_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert 51 == float(state.state)
|
assert 51 == float(state.state)
|
||||||
|
|
||||||
increment(hass, entity_id)
|
await increment(hass, entity_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
@ -172,13 +172,13 @@ async def test_decrement(hass):
|
|||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert 50 == float(state.state)
|
assert 50 == float(state.state)
|
||||||
|
|
||||||
decrement(hass, entity_id)
|
await decrement(hass, entity_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert 49 == float(state.state)
|
assert 49 == float(state.state)
|
||||||
|
|
||||||
decrement(hass, entity_id)
|
await decrement(hass, entity_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user