async def json_serialize_states(hass): """Serialize million states with websocket default encoder.""" states = [ core.State("light.kitchen", "on", {"friendly_name": "Kitchen Lights"}) for _ in range(10**6) ] start = timer() JSON_DUMP(states) return timer() - start
def _ws_formatted_get_events( msg_id: int, start_time: dt, end_time: dt, event_processor: EventProcessor, ) -> str: """Fetch events and convert them to json in the executor.""" return JSON_DUMP( messages.result_message( msg_id, event_processor.get_events(start_time, end_time) ) )
def _async_send_empty_response( connection: ActiveConnection, msg_id: int, start_time: dt, end_time: dt | None ) -> None: """Send an empty response. The current case for this is when they ask for entity_ids that will all be filtered away because they have UOMs or state_class. """ connection.send_result(msg_id) stream_end_time = end_time or dt_util.utcnow() empty_stream_message = _generate_stream_message([], start_time, stream_end_time) empty_response = messages.event_message(msg_id, empty_stream_message) connection.send_message(JSON_DUMP(empty_response))
def _ws_formatted_get_events( msg_id: int, start_day: dt, end_day: dt, formatter: Callable[[int, Any], dict[str, Any]], event_processor: EventProcessor, ) -> tuple[str, dt | None]: """Fetch events and convert them to json in the executor.""" events = event_processor.get_events(start_day, end_day) last_time = None if events: last_time = dt_util.utc_from_timestamp(events[-1]["when"]) result = formatter(msg_id, events) return JSON_DUMP(result), last_time
def _ws_stream_get_events( msg_id: int, start_day: dt, end_day: dt, formatter: Callable[[int, Any], dict[str, Any]], event_processor: EventProcessor, partial: bool, ) -> tuple[str, dt | None]: """Fetch events and convert them to json in the executor.""" events = event_processor.get_events(start_day, end_day) last_time = None if events: last_time = dt_util.utc_from_timestamp(events[-1]["when"]) message = _generate_stream_message(events, start_day, end_day) if partial: # This is a hint to consumers of the api that # we are about to send a another block of historical # data in case the UI needs to show that historical # data is still loading in the future message["partial"] = True return JSON_DUMP(formatter(msg_id, message)), last_time