Validate Number value before calling entity method (#52343)
Co-authored-by: Franck Nijhof <frenck@frenck.nl> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
af38ff1ec1
commit
8f014361d4
5 changed files with 119 additions and 16 deletions
|
@ -1,7 +1,18 @@
|
|||
"""The tests for the Number component."""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from homeassistant.components.number import NumberEntity
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.number import (
|
||||
ATTR_STEP,
|
||||
ATTR_VALUE,
|
||||
DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
NumberEntity,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, CONF_PLATFORM
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
class MockDefaultNumberEntity(NumberEntity):
|
||||
|
@ -27,7 +38,7 @@ class MockNumberEntity(NumberEntity):
|
|||
return 0.5
|
||||
|
||||
|
||||
async def test_step(hass):
|
||||
async def test_step(hass: HomeAssistant) -> None:
|
||||
"""Test the step calculation."""
|
||||
number = MockDefaultNumberEntity()
|
||||
assert number.step == 1.0
|
||||
|
@ -36,7 +47,7 @@ async def test_step(hass):
|
|||
assert number_2.step == 0.1
|
||||
|
||||
|
||||
async def test_sync_set_value(hass):
|
||||
async def test_sync_set_value(hass: HomeAssistant) -> None:
|
||||
"""Test if async set_value calls sync set_value."""
|
||||
number = MockDefaultNumberEntity()
|
||||
number.hass = hass
|
||||
|
@ -46,3 +57,43 @@ async def test_sync_set_value(hass):
|
|||
|
||||
assert number.set_value.called
|
||||
assert number.set_value.call_args[0][0] == 42
|
||||
|
||||
|
||||
async def test_custom_integration_and_validation(
|
||||
hass: HomeAssistant, enable_custom_integrations: None
|
||||
) -> None:
|
||||
"""Test we can only set valid values."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("number.test")
|
||||
assert state.state == "50.0"
|
||||
assert state.attributes.get(ATTR_STEP) == 1.0
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{ATTR_VALUE: 60.0, ATTR_ENTITY_ID: "number.test"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
hass.states.async_set("number.test", 60.0)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("number.test")
|
||||
assert state.state == "60.0"
|
||||
|
||||
# test ValueError trigger
|
||||
with pytest.raises(ValueError):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{ATTR_VALUE: 110.0, ATTR_ENTITY_ID: "number.test"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("number.test")
|
||||
assert state.state == "60.0"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue