Home Assistant Unofficial Reference 2024.12.1
homeassistant.components.websocket_api.messages Namespace Reference

Functions

bytes|None _message_to_json_bytes_or_none (dict[str, Any] message)
 
bytes _partial_cached_event_message (Event event)
 
bytes _partial_cached_state_diff_message (Event[EventStateChangedData] event)
 
dict[ str, list[str]|dict[str, CompressedState]|dict[str, dict[str, dict[str, str|list[str]]]],] _state_diff_event (Event[EventStateChangedData] event)
 
bytes cached_event_message (bytes message_id_as_bytes, Event event)
 
bytes cached_state_diff_message (bytes message_id_as_bytes, Event[EventStateChangedData] event)
 
bytes construct_result_message (int iden, bytes payload)
 
dict[str, Any] error_message (int|None iden, str code, str message, str|None translation_key=None, str|None translation_domain=None, dict[str, Any]|None translation_placeholders=None)
 
dict[str, Any] event_message (int iden, Any event)
 
bytes message_to_json_bytes (dict[str, Any] message)
 
dict[str, Any] result_message (int iden, Any result=None)
 

Variables

dictionary BASE_ERROR_MESSAGE
 
string ENTITY_EVENT_ADD = "a"
 
string ENTITY_EVENT_CHANGE = "c"
 
string ENTITY_EVENT_REMOVE = "r"
 
 extra
 
 Final
 
 INVALID_JSON_PARTIAL_MESSAGE
 
string STATE_DIFF_ADDITIONS = "+"
 
string STATE_DIFF_REMOVALS = "-"
 

Detailed Description

Message templates for websocket commands.

Function Documentation

◆ _message_to_json_bytes_or_none()

bytes | None homeassistant.components.websocket_api.messages._message_to_json_bytes_or_none ( dict[str, Any]  message)
private
Serialize a websocket message to json or return None.

Definition at line 241 of file messages.py.

◆ _partial_cached_event_message()

bytes homeassistant.components.websocket_api.messages._partial_cached_event_message ( Event  event)
private
Cache and serialize the event to json.

The message is constructed without the id which appended
in cached_event_message.

Definition at line 132 of file messages.py.

◆ _partial_cached_state_diff_message()

bytes homeassistant.components.websocket_api.messages._partial_cached_state_diff_message ( Event[EventStateChangedData event)
private
Cache and serialize the event to json.

The message is constructed without the id which
will be appended in cached_state_diff_message

Definition at line 166 of file messages.py.

◆ _state_diff_event()

dict[ str, list[str] | dict[str, CompressedState] | dict[str, dict[str, dict[str, str | list[str]]]], ] homeassistant.components.websocket_api.messages._state_diff_event ( Event[EventStateChangedData event)
private
Convert a state_changed event to the minimal version.

State update example

{
    "a": {entity_id: compressed_state,…}
    "c": {entity_id: diff,…}
    "r": [entity_id,…]
}

Definition at line 180 of file messages.py.

◆ cached_event_message()

bytes homeassistant.components.websocket_api.messages.cached_event_message ( bytes  message_id_as_bytes,
Event  event 
)
Return an event message.

Serialize to json once per message.

Since we can have many clients connected that are
all getting many of the same events (mostly state changed)
we can avoid serializing the same data for each connection.

Definition at line 112 of file messages.py.

◆ cached_state_diff_message()

bytes homeassistant.components.websocket_api.messages.cached_state_diff_message ( bytes  message_id_as_bytes,
Event[EventStateChangedData]   event 
)
Return an event message.

Serialize to json once per message.

Since we can have many clients connected that are
all getting many of the same events (mostly state changed)
we can avoid serializing the same data for each connection.

Definition at line 144 of file messages.py.

◆ construct_result_message()

bytes homeassistant.components.websocket_api.messages.construct_result_message ( int  iden,
bytes  payload 
)
Construct a success result message JSON.

Definition at line 68 of file messages.py.

◆ error_message()

dict[str, Any] homeassistant.components.websocket_api.messages.error_message ( int | None  iden,
str  code,
str  message,
str | None   translation_key = None,
str | None   translation_domain = None,
dict[str, Any] | None   translation_placeholders = None 
)
Return an error result message.

Definition at line 81 of file messages.py.

◆ event_message()

dict[str, Any] homeassistant.components.websocket_api.messages.event_message ( int  iden,
Any  event 
)
Return an event message.

Definition at line 107 of file messages.py.

◆ message_to_json_bytes()

bytes homeassistant.components.websocket_api.messages.message_to_json_bytes ( dict[str, Any]  message)
Serialize a websocket message to json or return an error.

Definition at line 255 of file messages.py.

◆ result_message()

dict[str, Any] homeassistant.components.websocket_api.messages.result_message ( int  iden,
Any   result = None 
)
Return a success result message.

Definition at line 63 of file messages.py.

Variable Documentation

◆ BASE_ERROR_MESSAGE

dictionary homeassistant.components.websocket_api.messages.BASE_ERROR_MESSAGE
Initial value:
1 = {
2  "type": const.TYPE_RESULT,
3  "success": False,
4 }

Definition at line 47 of file messages.py.

◆ ENTITY_EVENT_ADD

string homeassistant.components.websocket_api.messages.ENTITY_EVENT_ADD = "a"

Definition at line 43 of file messages.py.

◆ ENTITY_EVENT_CHANGE

string homeassistant.components.websocket_api.messages.ENTITY_EVENT_CHANGE = "c"

Definition at line 45 of file messages.py.

◆ ENTITY_EVENT_REMOVE

string homeassistant.components.websocket_api.messages.ENTITY_EVENT_REMOVE = "r"

Definition at line 44 of file messages.py.

◆ extra

homeassistant.components.websocket_api.messages.extra

Definition at line 34 of file messages.py.

◆ Final

homeassistant.components.websocket_api.messages.Final

Definition at line 29 of file messages.py.

◆ INVALID_JSON_PARTIAL_MESSAGE

homeassistant.components.websocket_api.messages.INVALID_JSON_PARTIAL_MESSAGE
Initial value:
1 = json_bytes(
2  {
3  **BASE_ERROR_MESSAGE,
4  "error": {
5  "code": const.ERR_UNKNOWN_ERROR,
6  "message": "Invalid JSON in response",
7  },
8  }
9 )

Definition at line 52 of file messages.py.

◆ STATE_DIFF_ADDITIONS

string homeassistant.components.websocket_api.messages.STATE_DIFF_ADDITIONS = "+"

Definition at line 40 of file messages.py.

◆ STATE_DIFF_REMOVALS

string homeassistant.components.websocket_api.messages.STATE_DIFF_REMOVALS = "-"

Definition at line 41 of file messages.py.