예제 #1
0
파일: db_schema.py 프로젝트: jbouwh/core
 def from_event(event: Event) -> EventData:
     """Create object from an event."""
     shared_data = json_bytes(event.data)
     return EventData(
         shared_data=shared_data.decode("utf-8"),
         hash=EventData.hash_shared_data_bytes(shared_data),
     )
예제 #2
0
파일: db_schema.py 프로젝트: jbouwh/core
 def from_event(event: Event) -> StateAttributes:
     """Create object from a state_changed event."""
     state: State | None = event.data.get("new_state")
     # None state means the state was removed from the state machine
     attr_bytes = b"{}" if state is None else json_bytes(state.attributes)
     dbstate = StateAttributes(shared_attrs=attr_bytes.decode("utf-8"))
     dbstate.hash = StateAttributes.hash_shared_attrs_bytes(attr_bytes)
     return dbstate
예제 #3
0
파일: db_schema.py 프로젝트: jbouwh/core
 def shared_attrs_bytes_from_event(
         event: Event, exclude_attrs_by_domain: dict[str,
                                                     set[str]]) -> bytes:
     """Create shared_attrs from a state_changed event."""
     state: State | None = event.data.get("new_state")
     # None state means the state was removed from the state machine
     if state is None:
         return b"{}"
     domain = split_entity_id(state.entity_id)[0]
     exclude_attrs = (exclude_attrs_by_domain.get(domain, set())
                      | ALL_DOMAIN_EXCLUDE_ATTRS)
     return json_bytes({
         k: v
         for k, v in state.attributes.items() if k not in exclude_attrs
     })
예제 #4
0
 def json(
     result: Any,
     status_code: HTTPStatus | int = HTTPStatus.OK,
     headers: LooseHeaders | None = None,
 ) -> web.Response:
     """Return a JSON response."""
     try:
         msg = json_bytes(result)
     except JSON_ENCODE_EXCEPTIONS as err:
         _LOGGER.error("Unable to serialize to JSON: %s\n%s", err, result)
         raise HTTPInternalServerError from err
     response = web.Response(
         body=msg,
         content_type=CONTENT_TYPE_JSON,
         status=int(status_code),
         headers=headers,
     )
     response.enable_compression()
     return response
예제 #5
0
파일: db_schema.py 프로젝트: jbouwh/core
 def shared_data_bytes_from_event(event: Event) -> bytes:
     """Create shared_data from an event."""
     return json_bytes(event.data)