Improve error logging when state is too long (#143636)

This commit is contained in:
J. Nick Koston 2025-04-24 18:37:32 -10:00 committed by GitHub
parent 605bf7e287
commit cb0523660d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 22 deletions

View File

@ -31,6 +31,7 @@ from homeassistant.const import (
ATTR_SUPPORTED_FEATURES, ATTR_SUPPORTED_FEATURES,
ATTR_UNIT_OF_MEASUREMENT, ATTR_UNIT_OF_MEASUREMENT,
DEVICE_DEFAULT_NAME, DEVICE_DEFAULT_NAME,
MAX_LENGTH_STATE_STATE,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
@ -49,11 +50,7 @@ from homeassistant.core import (
get_release_channel, get_release_channel,
) )
from homeassistant.core_config import DATA_CUSTOMIZE from homeassistant.core_config import DATA_CUSTOMIZE
from homeassistant.exceptions import ( from homeassistant.exceptions import HomeAssistantError, NoEntitySpecifiedError
HomeAssistantError,
InvalidStateError,
NoEntitySpecifiedError,
)
from homeassistant.loader import async_suggest_report_issue, bind_hass from homeassistant.loader import async_suggest_report_issue, bind_hass
from homeassistant.util import ensure_unique_string, slugify from homeassistant.util import ensure_unique_string, slugify
from homeassistant.util.frozen_dataclass_compat import FrozenOrThawed from homeassistant.util.frozen_dataclass_compat import FrozenOrThawed
@ -1223,7 +1220,17 @@ class Entity(
self._context = None self._context = None
self._context_set = None self._context_set = None
try: if len(state) > MAX_LENGTH_STATE_STATE:
_LOGGER.error(
"State %s for %s is longer than %s, falling back to %s",
state,
entity_id,
MAX_LENGTH_STATE_STATE,
STATE_UNKNOWN,
)
state = STATE_UNKNOWN
# Intentionally called with positional args for performance reasons
hass.states.async_set_internal( hass.states.async_set_internal(
entity_id, entity_id,
state, state,
@ -1233,13 +1240,6 @@ class Entity(
self._state_info, self._state_info,
time_now, 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
)
def schedule_update_ha_state(self, force_refresh: bool = False) -> None: def schedule_update_ha_state(self, force_refresh: bool = False) -> None:
"""Schedule an update ha state change task. """Schedule an update ha state change task.

View File

@ -1705,13 +1705,15 @@ async def test_invalid_state(
assert hass.states.get("test.test").state == "x" * 255 assert hass.states.get("test.test").state == "x" * 255
caplog.clear() caplog.clear()
ent._attr_state = "x" * 256 long_state = "x" * 256
ent._attr_state = long_state
ent.async_write_ha_state() ent.async_write_ha_state()
assert hass.states.get("test.test").state == STATE_UNKNOWN assert hass.states.get("test.test").state == STATE_UNKNOWN
assert ( assert (
"homeassistant.helpers.entity", "homeassistant.helpers.entity",
logging.ERROR, 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 ) in caplog.record_tuples
ent._attr_state = "x" * 255 ent._attr_state = "x" * 255