Skip to content

Commit

Permalink
Move mqtt_json_to_event from interfaces.mqtt to models.mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 committed Aug 7, 2024
1 parent df110ea commit 786a4bb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
23 changes: 0 additions & 23 deletions axis/interfaces/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import Any

import orjson

from ..models.api_discovery import ApiId
from ..models.mqtt import (
API_VERSION,
Expand All @@ -25,27 +23,6 @@
DEFAULT_TOPICS = ["//."]


def mqtt_json_to_event(msg: bytes | str) -> dict[str, Any]:
"""Convert JSON message from MQTT to event format."""
message = orjson.loads(msg)

source = source_idx = ""
if message["message"]["source"]:
source, source_idx = next(iter(message["message"]["source"].items()))

data_type = data_value = ""
if message["message"]["data"]:
data_type, data_value = next(iter(message["message"]["data"].items()))

return {
"topic": message["topic"],
"source": source,
"source_idx": source_idx,
"type": data_type,
"value": data_value,
}


class MqttClientHandler(ApiHandler[Any]):
"""MQTT Client for Axis devices."""

Expand Down
7 changes: 5 additions & 2 deletions axis/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _decode_from_dict(cls, data: dict[str, Any]) -> Self:
source_idx = data.get(EVENT_SOURCE_IDX, "")
value = data.get(EVENT_VALUE, "")

if (topic_base := EventTopic(topic)) == EventTopic.UNKNOWN:
if (topic_base := EventTopic(topic)) is EventTopic.UNKNOWN:
_topic_base, _, _source_idx = topic.rpartition("/")
topic_base = EventTopic(_topic_base)
if source_idx == "":
Expand All @@ -153,7 +153,10 @@ def _decode_from_dict(cls, data: dict[str, Any]) -> Self:
def _decode_from_bytes(cls, data: bytes) -> Self:
"""Parse metadata xml."""
raw = xmltodict.parse(
data, attr_prefix="", process_namespaces=True, namespaces=XML_NAMESPACES
data,
attr_prefix="",
process_namespaces=True,
namespaces=XML_NAMESPACES,
)

if raw.get("MetadataStream") is None:
Expand Down
30 changes: 29 additions & 1 deletion axis/models/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,44 @@

from dataclasses import dataclass
import enum
from typing import Literal, NotRequired, Self
from typing import Any, Literal, NotRequired, Self

import orjson
from typing_extensions import TypedDict

from .api import CONTEXT, ApiRequest, ApiResponse
from .event import (
EVENT_SOURCE,
EVENT_SOURCE_IDX,
EVENT_TOPIC,
EVENT_TYPE,
EVENT_VALUE,
)

API_VERSION = "1.0"


def mqtt_json_to_event(msg: bytes | str) -> dict[str, Any]:
"""Convert JSON message from MQTT to event format."""
message = orjson.loads(msg)

source = source_idx = ""
if message["message"]["source"]:
source, source_idx = next(iter(message["message"]["source"].items()))

data_type = data_value = ""
if message["message"]["data"]:
data_type, data_value = next(iter(message["message"]["data"].items()))

return {
EVENT_TOPIC: message["topic"],
EVENT_SOURCE: source,
EVENT_SOURCE_IDX: source_idx,
EVENT_TYPE: data_type,
EVENT_VALUE: data_value,
}


class ErrorDataT(TypedDict):
"""Error data in response."""

Expand Down
11 changes: 9 additions & 2 deletions tests/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
import pytest

from axis.device import AxisDevice
from axis.interfaces.mqtt import MqttClientHandler, mqtt_json_to_event
from axis.models.mqtt import ClientConfig, Message, Server, ServerProtocol, Ssl
from axis.interfaces.mqtt import MqttClientHandler
from axis.models.mqtt import (
ClientConfig,
Message,
Server,
ServerProtocol,
Ssl,
mqtt_json_to_event,
)


@pytest.fixture
Expand Down

0 comments on commit 786a4bb

Please sign in to comment.