Add extra tests for input text (#70283)

This commit is contained in:
Paulus Schoutsen 2022-04-19 10:31:09 -07:00 committed by GitHub
parent 7bc9d01520
commit 78f5614cdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,7 +26,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 as er from homeassistant.helpers import entity_registry as er
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
@ -70,16 +69,13 @@ def storage_setup(hass, hass_storage):
return _storage return _storage
@bind_hass async def async_set_value(hass, entity_id, value):
def set_value(hass, entity_id, value): """Set input_text to value."""
"""Set input_text to value. await hass.services.async_call(
DOMAIN,
This is a legacy helper method. Do not use it for new tests. SERVICE_SET_VALUE,
""" {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: value},
hass.async_create_task( blocking=True,
hass.services.async_call(
DOMAIN, SERVICE_SET_VALUE, {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: value}
)
) )
@ -98,24 +94,31 @@ async def test_config(hass):
async def test_set_value(hass): async def test_set_value(hass):
"""Test set_value method.""" """Test set_value method."""
assert await async_setup_component( assert await async_setup_component(
hass, DOMAIN, {DOMAIN: {"test_1": {"initial": "test", "min": 3, "max": 10}}} hass,
DOMAIN,
{
DOMAIN: {
"test_1": {"initial": "test", "min": 3, "max": 10},
"test_2": {},
}
},
) )
entity_id = "input_text.test_1" entity_id = "input_text.test_1"
entity_id_2 = "input_text.test_2"
assert hass.states.get(entity_id).state == "test"
assert hass.states.get(entity_id_2).state == "unknown"
state = hass.states.get(entity_id) for entity in (entity_id, entity_id_2):
assert str(state.state) == "test" await async_set_value(hass, entity, "testing")
assert hass.states.get(entity).state == "testing"
set_value(hass, entity_id, "testing") # Too long for entity 1
await hass.async_block_till_done() await async_set_value(hass, entity, "testing too long")
assert hass.states.get(entity_id).state == "testing"
state = hass.states.get(entity_id) # Set to empty string
assert str(state.state) == "testing" await async_set_value(hass, entity_id_2, "")
assert hass.states.get(entity_id_2).state == ""
set_value(hass, entity_id, "testing too long")
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert str(state.state) == "testing"
async def test_mode(hass): async def test_mode(hass):