Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions harbor/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import asyncio
import json
import logging
import sys
from collections.abc import Awaitable, Callable
from typing import Any
from typing import TYPE_CHECKING, Any
from uuid import uuid4

from aiomqtt import Client, MqttError
Expand All @@ -12,6 +14,9 @@
from .data.mqtt_models import GetCameraSettingsRequest, SettingsEvent
from .utils import get_camera_host, get_ssl_cache_key, get_ssl_context

if TYPE_CHECKING:
from .events import HarborEvent

_LOGGER = logging.getLogger(__name__)

DEFAULT_CONNECTION_GRACE_PERIOD = 90.0
Expand All @@ -26,7 +31,7 @@ def __init__(
self,
config: HarborCameraConfig,
topics: list[str],
message_handler: Callable[[str, Any], Awaitable[None]],
message_handler: Callable[[str, Any], Awaitable[HarborEvent | None]],
client_id: str | None = None,
ssl_context_cache: dict | None = None,
on_connection_change: Callable[[bool], Awaitable[None]] | None = None,
Expand Down
22 changes: 22 additions & 0 deletions tests/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json

from harbor.config import HarborCameraConfig
from harbor.events import HarborEvent
from harbor.mqtt import GET_SETTINGS_COMMAND, HarborMQTTClient


Expand Down Expand Up @@ -51,6 +52,27 @@ async def message_handler(topic: str, payload: object) -> None:
assert messages == [("test/topic", {"test": "data"})]


async def test_message_handler_may_return_event() -> None:
"""MQTT handlers may return parsed events; the client ignores the value."""

called = False

async def message_handler(topic: str, payload: object) -> HarborEvent | None:
nonlocal called
called = True
return None

client = HarborMQTTClient(
config=_create_config(),
topics=[],
message_handler=message_handler,
)

await client._handle_message("test/topic", "{}")

assert called is True


async def _noop_handler(topic: str, payload: object) -> None:
pass

Expand Down
Loading