diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index bdcda58c054..909480d165b 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -31,6 +31,7 @@ from homeassistant.const import ( ATTR_SUPPORTED_FEATURES, ATTR_UNIT_OF_MEASUREMENT, DEVICE_DEFAULT_NAME, + MAX_LENGTH_STATE_STATE, STATE_OFF, STATE_ON, STATE_UNAVAILABLE, @@ -49,11 +50,7 @@ from homeassistant.core import ( get_release_channel, ) from homeassistant.core_config import DATA_CUSTOMIZE -from homeassistant.exceptions import ( - HomeAssistantError, - InvalidStateError, - NoEntitySpecifiedError, -) +from homeassistant.exceptions import HomeAssistantError, NoEntitySpecifiedError from homeassistant.loader import async_suggest_report_issue, bind_hass from homeassistant.util import ensure_unique_string, slugify from homeassistant.util.frozen_dataclass_compat import FrozenOrThawed @@ -1223,23 +1220,26 @@ class Entity( self._context = None self._context_set = None - try: - hass.states.async_set_internal( - entity_id, + if len(state) > MAX_LENGTH_STATE_STATE: + _LOGGER.error( + "State %s for %s is longer than %s, falling back to %s", state, - attr, - self.force_update, - self._context, - self._state_info, - time_now, - ) - except InvalidStateError: - _LOGGER.exception( - "Failed to set state for %s, fall back to %s", entity_id, STATE_UNKNOWN - ) - hass.states.async_set( - entity_id, STATE_UNKNOWN, {}, self.force_update, self._context + entity_id, + MAX_LENGTH_STATE_STATE, + STATE_UNKNOWN, ) + state = STATE_UNKNOWN + + # Intentionally called with positional args for performance reasons + hass.states.async_set_internal( + entity_id, + state, + attr, + self.force_update, + self._context, + self._state_info, + time_now, + ) def schedule_update_ha_state(self, force_refresh: bool = False) -> None: """Schedule an update ha state change task. diff --git a/tests/helpers/test_entity.py b/tests/helpers/test_entity.py index 6cf0e7c54d2..04ea0295d72 100644 --- a/tests/helpers/test_entity.py +++ b/tests/helpers/test_entity.py @@ -1705,13 +1705,15 @@ async def test_invalid_state( assert hass.states.get("test.test").state == "x" * 255 caplog.clear() - ent._attr_state = "x" * 256 + long_state = "x" * 256 + ent._attr_state = long_state ent.async_write_ha_state() assert hass.states.get("test.test").state == STATE_UNKNOWN assert ( "homeassistant.helpers.entity", logging.ERROR, - f"Failed to set state for test.test, fall back to {STATE_UNKNOWN}", + f"State {long_state} for test.test is longer than 255, " + f"falling back to {STATE_UNKNOWN}", ) in caplog.record_tuples ent._attr_state = "x" * 255