mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Improve mqtt MessageCallback typing (#75614)
* Improve mqtt MessageCallback typing * Use MQTTMessage
This commit is contained in:
parent
9c725bc106
commit
2b617e3885
@ -2,7 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections.abc import Awaitable, Callable, Iterable
|
from collections.abc import Awaitable, Callable, Coroutine, Iterable
|
||||||
from functools import lru_cache, partial, wraps
|
from functools import lru_cache, partial, wraps
|
||||||
import inspect
|
import inspect
|
||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
@ -15,6 +15,7 @@ import uuid
|
|||||||
|
|
||||||
import attr
|
import attr
|
||||||
import certifi
|
import certifi
|
||||||
|
from paho.mqtt.client import MQTTMessage
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_CLIENT_ID,
|
CONF_CLIENT_ID,
|
||||||
@ -246,7 +247,7 @@ class Subscription:
|
|||||||
|
|
||||||
topic: str = attr.ib()
|
topic: str = attr.ib()
|
||||||
matcher: Any = attr.ib()
|
matcher: Any = attr.ib()
|
||||||
job: HassJob = attr.ib()
|
job: HassJob[[ReceiveMessage], Coroutine[Any, Any, None] | None] = attr.ib()
|
||||||
qos: int = attr.ib(default=0)
|
qos: int = attr.ib(default=0)
|
||||||
encoding: str | None = attr.ib(default="utf-8")
|
encoding: str | None = attr.ib(default="utf-8")
|
||||||
|
|
||||||
@ -444,7 +445,7 @@ class MQTT:
|
|||||||
async def async_subscribe(
|
async def async_subscribe(
|
||||||
self,
|
self,
|
||||||
topic: str,
|
topic: str,
|
||||||
msg_callback: MessageCallbackType,
|
msg_callback: AsyncMessageCallbackType | MessageCallbackType,
|
||||||
qos: int,
|
qos: int,
|
||||||
encoding: str | None = None,
|
encoding: str | None = None,
|
||||||
) -> Callable[[], None]:
|
) -> Callable[[], None]:
|
||||||
@ -597,15 +598,15 @@ class MQTT:
|
|||||||
self.hass.add_job(self._mqtt_handle_message, msg)
|
self.hass.add_job(self._mqtt_handle_message, msg)
|
||||||
|
|
||||||
@lru_cache(2048)
|
@lru_cache(2048)
|
||||||
def _matching_subscriptions(self, topic):
|
def _matching_subscriptions(self, topic: str) -> list[Subscription]:
|
||||||
subscriptions = []
|
subscriptions: list[Subscription] = []
|
||||||
for subscription in self.subscriptions:
|
for subscription in self.subscriptions:
|
||||||
if subscription.matcher(topic):
|
if subscription.matcher(topic):
|
||||||
subscriptions.append(subscription)
|
subscriptions.append(subscription)
|
||||||
return subscriptions
|
return subscriptions
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _mqtt_handle_message(self, msg) -> None:
|
def _mqtt_handle_message(self, msg: MQTTMessage) -> None:
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Received message on %s%s: %s",
|
"Received message on %s%s: %s",
|
||||||
msg.topic,
|
msg.topic,
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from ast import literal_eval
|
from ast import literal_eval
|
||||||
from collections.abc import Awaitable, Callable
|
from collections.abc import Callable, Coroutine
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
from typing import Any, Union
|
from typing import Any, Union
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ class ReceiveMessage:
|
|||||||
timestamp: dt.datetime = attr.ib(default=None)
|
timestamp: dt.datetime = attr.ib(default=None)
|
||||||
|
|
||||||
|
|
||||||
AsyncMessageCallbackType = Callable[[ReceiveMessage], Awaitable[None]]
|
AsyncMessageCallbackType = Callable[[ReceiveMessage], Coroutine[Any, Any, None]]
|
||||||
MessageCallbackType = Callable[[ReceiveMessage], None]
|
MessageCallbackType = Callable[[ReceiveMessage], None]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user