Allow get_states to recover (#67146)
This commit is contained in:
parent
79bdd71da7
commit
a42547c0e5
3 changed files with 45 additions and 5 deletions
|
@ -34,6 +34,10 @@ from homeassistant.helpers.json import ExtendedJSONEncoder
|
|||
from homeassistant.helpers.service import async_get_all_descriptions
|
||||
from homeassistant.loader import IntegrationNotFound, async_get_integration
|
||||
from homeassistant.setup import DATA_SETUP_TIME, async_get_loaded_integrations
|
||||
from homeassistant.util.json import (
|
||||
find_paths_unserializable_data,
|
||||
format_unserializable_data,
|
||||
)
|
||||
|
||||
from . import const, decorators, messages
|
||||
from .connection import ActiveConnection
|
||||
|
@ -225,7 +229,35 @@ def handle_get_states(
|
|||
if entity_perm(state.entity_id, "read")
|
||||
]
|
||||
|
||||
connection.send_result(msg["id"], states)
|
||||
# JSON serialize here so we can recover if it blows up due to the
|
||||
# state machine containing unserializable data. This command is required
|
||||
# to succeed for the UI to show.
|
||||
response = messages.result_message(msg["id"], states)
|
||||
try:
|
||||
connection.send_message(const.JSON_DUMP(response))
|
||||
return
|
||||
except (ValueError, TypeError):
|
||||
connection.logger.error(
|
||||
"Unable to serialize to JSON. Bad data found at %s",
|
||||
format_unserializable_data(
|
||||
find_paths_unserializable_data(response, dump=const.JSON_DUMP)
|
||||
),
|
||||
)
|
||||
del response
|
||||
|
||||
# If we can't serialize, we'll filter out unserializable states
|
||||
serialized = []
|
||||
for state in states:
|
||||
try:
|
||||
serialized.append(const.JSON_DUMP(state))
|
||||
except (ValueError, TypeError):
|
||||
# Error is already logged above
|
||||
pass
|
||||
|
||||
# We now have partially serialized states. Craft some JSON.
|
||||
response2 = const.JSON_DUMP(messages.result_message(msg["id"], ["TO_REPLACE"]))
|
||||
response2 = response2.replace('"TO_REPLACE"', ", ".join(serialized))
|
||||
connection.send_message(response2)
|
||||
|
||||
|
||||
@decorators.websocket_command({vol.Required("type"): "get_services"})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue