mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Validate state in template helper preview (#99455)
* Validate state in template helper preview * Deduplicate state validation
This commit is contained in:
parent
709ce7e0af
commit
7c595ee2da
@ -25,6 +25,7 @@ from homeassistant.core import (
|
|||||||
HomeAssistant,
|
HomeAssistant,
|
||||||
State,
|
State,
|
||||||
callback,
|
callback,
|
||||||
|
validate_state,
|
||||||
)
|
)
|
||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -413,8 +414,8 @@ class TemplateEntity(Entity):
|
|||||||
return
|
return
|
||||||
|
|
||||||
for update in updates:
|
for update in updates:
|
||||||
for attr in self._template_attrs[update.template]:
|
for template_attr in self._template_attrs[update.template]:
|
||||||
attr.handle_result(
|
template_attr.handle_result(
|
||||||
event, update.template, update.last_result, update.result
|
event, update.template, update.last_result, update.result
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -422,7 +423,13 @@ class TemplateEntity(Entity):
|
|||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
self._preview_callback(*self._async_generate_attributes(), None)
|
try:
|
||||||
|
state, attrs = self._async_generate_attributes()
|
||||||
|
validate_state(state)
|
||||||
|
except Exception as err: # pylint: disable=broad-exception-caught
|
||||||
|
self._preview_callback(None, None, str(err))
|
||||||
|
else:
|
||||||
|
self._preview_callback(state, attrs, None)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_template_startup(self, *_: Any) -> None:
|
def _async_template_startup(self, *_: Any) -> None:
|
||||||
|
@ -174,6 +174,16 @@ def valid_entity_id(entity_id: str) -> bool:
|
|||||||
return VALID_ENTITY_ID.match(entity_id) is not None
|
return VALID_ENTITY_ID.match(entity_id) is not None
|
||||||
|
|
||||||
|
|
||||||
|
def validate_state(state: str) -> str:
|
||||||
|
"""Validate a state, raise if it not valid."""
|
||||||
|
if len(state) > MAX_LENGTH_STATE_STATE:
|
||||||
|
raise InvalidStateError(
|
||||||
|
f"Invalid state with length {len(state)}. "
|
||||||
|
"State max length is 255 characters."
|
||||||
|
)
|
||||||
|
return state
|
||||||
|
|
||||||
|
|
||||||
def callback(func: _CallableT) -> _CallableT:
|
def callback(func: _CallableT) -> _CallableT:
|
||||||
"""Annotation to mark method as safe to call from within the event loop."""
|
"""Annotation to mark method as safe to call from within the event loop."""
|
||||||
setattr(func, "_hass_callback", True)
|
setattr(func, "_hass_callback", True)
|
||||||
@ -1255,11 +1265,7 @@ class State:
|
|||||||
"Format should be <domain>.<object_id>"
|
"Format should be <domain>.<object_id>"
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(state) > MAX_LENGTH_STATE_STATE:
|
validate_state(state)
|
||||||
raise InvalidStateError(
|
|
||||||
f"Invalid state encountered for entity ID: {entity_id}. "
|
|
||||||
"State max length is 255 characters."
|
|
||||||
)
|
|
||||||
|
|
||||||
self.entity_id = entity_id
|
self.entity_id = entity_id
|
||||||
self.state = state
|
self.state = state
|
||||||
|
@ -2465,3 +2465,10 @@ async def test_cancellable_hassjob(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
# Cleanup
|
# Cleanup
|
||||||
timer2.cancel()
|
timer2.cancel()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_validate_state(hass: HomeAssistant) -> None:
|
||||||
|
"""Test validate_state."""
|
||||||
|
assert ha.validate_state("test") == "test"
|
||||||
|
with pytest.raises(InvalidStateError):
|
||||||
|
ha.validate_state("t" * 256)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user