Initial orjson support take 3 (#73849)
* Initial orjson support take 2 Still need to work out problem building wheels -- Redux of #72754 / #32153 Now possible since the following is solved: ijl/orjson#220 (comment) This implements orjson where we use our default encoder. This does not implement orjson where `ExtendedJSONEncoder` is used as these areas tend to be called far less frequently. If its desired, this could be done in a followup, but it seemed like a case of diminishing returns (except maybe for large diagnostics files, or traces, but those are not expected to be downloaded frequently). Areas where this makes a perceptible difference: - Anything that subscribes to entities (Initial subscribe_entities payload) - Initial download of registries on first connection / restore - History queries - Saving states to the database - Large logbook queries - Anything that subscribes to events (appdaemon) Cavets: orjson supports serializing dataclasses natively (and much faster) which eliminates the need to implement `as_dict` in many places when the data is already in a dataclass. This works well as long as all the data in the dataclass can also be serialized. I audited all places where we have an `as_dict` for a dataclass and found only backups needs to be adjusted (support for `Path` needed to be added for backups). I was a little bit worried about `SensorExtraStoredData` with `Decimal` but it all seems to work out from since it converts it before it gets to the json encoding cc @dgomes If it turns out to be a problem we can disable this with option |= [orjson.OPT_PASSTHROUGH_DATACLASS](https://github.com/ijl/orjson#opt_passthrough_dataclass) and it will fallback to `as_dict` Its quite impressive for history queries <img width="1271" alt="Screen_Shot_2022-05-30_at_23_46_30" src="https://user-images.githubusercontent.com/663432/171145699-661ad9db-d91d-4b2d-9c1a-9d7866c03a73.png"> * use for views as well * handle UnicodeEncodeError * tweak * DRY * DRY * not needed * fix tests * Update tests/components/http/test_view.py * Update tests/components/http/test_view.py * black * templates
This commit is contained in:
parent
9ac28d2076
commit
8b067e83f7
22 changed files with 149 additions and 80 deletions
|
@ -29,7 +29,7 @@ from homeassistant.helpers.event import (
|
|||
TrackTemplateResult,
|
||||
async_track_template_result,
|
||||
)
|
||||
from homeassistant.helpers.json import ExtendedJSONEncoder
|
||||
from homeassistant.helpers.json import JSON_DUMP, 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
|
||||
|
@ -241,13 +241,13 @@ def handle_get_states(
|
|||
# to succeed for the UI to show.
|
||||
response = messages.result_message(msg["id"], states)
|
||||
try:
|
||||
connection.send_message(const.JSON_DUMP(response))
|
||||
connection.send_message(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)
|
||||
find_paths_unserializable_data(response, dump=JSON_DUMP)
|
||||
),
|
||||
)
|
||||
del response
|
||||
|
@ -256,13 +256,13 @@ def handle_get_states(
|
|||
serialized = []
|
||||
for state in states:
|
||||
try:
|
||||
serialized.append(const.JSON_DUMP(state))
|
||||
serialized.append(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 = JSON_DUMP(messages.result_message(msg["id"], ["TO_REPLACE"]))
|
||||
response2 = response2.replace('"TO_REPLACE"', ", ".join(serialized))
|
||||
connection.send_message(response2)
|
||||
|
||||
|
@ -315,13 +315,13 @@ def handle_subscribe_entities(
|
|||
# to succeed for the UI to show.
|
||||
response = messages.event_message(msg["id"], data)
|
||||
try:
|
||||
connection.send_message(const.JSON_DUMP(response))
|
||||
connection.send_message(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)
|
||||
find_paths_unserializable_data(response, dump=JSON_DUMP)
|
||||
),
|
||||
)
|
||||
del response
|
||||
|
@ -330,14 +330,14 @@ def handle_subscribe_entities(
|
|||
cannot_serialize: list[str] = []
|
||||
for entity_id, state_dict in add_entities.items():
|
||||
try:
|
||||
const.JSON_DUMP(state_dict)
|
||||
JSON_DUMP(state_dict)
|
||||
except (ValueError, TypeError):
|
||||
cannot_serialize.append(entity_id)
|
||||
|
||||
for entity_id in cannot_serialize:
|
||||
del add_entities[entity_id]
|
||||
|
||||
connection.send_message(const.JSON_DUMP(messages.event_message(msg["id"], data)))
|
||||
connection.send_message(JSON_DUMP(messages.event_message(msg["id"], data)))
|
||||
|
||||
|
||||
@decorators.websocket_command({vol.Required("type"): "get_services"})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue