Use dataclass instead of attr slots for mqtt PublishMessage and ReceiveMessage (#101062)
Use dataclass instead of attr slots
This commit is contained in:
parent
77c519220d
commit
0ded0ef4ee
2 changed files with 17 additions and 17 deletions
|
@ -11,8 +11,6 @@ from enum import StrEnum
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Any, TypedDict
|
from typing import TYPE_CHECKING, Any, TypedDict
|
||||||
|
|
||||||
import attr
|
|
||||||
|
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME
|
from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME
|
||||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||||
from homeassistant.helpers import template
|
from homeassistant.helpers import template
|
||||||
|
@ -44,26 +42,26 @@ ATTR_THIS = "this"
|
||||||
PublishPayloadType = str | bytes | int | float | None
|
PublishPayloadType = str | bytes | int | float | None
|
||||||
|
|
||||||
|
|
||||||
@attr.s(slots=True, frozen=True)
|
@dataclass
|
||||||
class PublishMessage:
|
class PublishMessage:
|
||||||
"""MQTT Message."""
|
"""MQTT Message for publishing."""
|
||||||
|
|
||||||
topic: str = attr.ib()
|
topic: str
|
||||||
payload: PublishPayloadType = attr.ib()
|
payload: PublishPayloadType
|
||||||
qos: int = attr.ib()
|
qos: int
|
||||||
retain: bool = attr.ib()
|
retain: bool
|
||||||
|
|
||||||
|
|
||||||
@attr.s(slots=True, frozen=True)
|
@dataclass
|
||||||
class ReceiveMessage:
|
class ReceiveMessage:
|
||||||
"""MQTT Message."""
|
"""MQTT Message received."""
|
||||||
|
|
||||||
topic: str = attr.ib()
|
topic: str
|
||||||
payload: ReceivePayloadType = attr.ib()
|
payload: ReceivePayloadType
|
||||||
qos: int = attr.ib()
|
qos: int
|
||||||
retain: bool = attr.ib()
|
retain: bool
|
||||||
subscribed_topic: str = attr.ib(default=None)
|
subscribed_topic: str
|
||||||
timestamp: dt.datetime = attr.ib(default=None)
|
timestamp: dt.datetime
|
||||||
|
|
||||||
|
|
||||||
AsyncMessageCallbackType = Callable[[ReceiveMessage], Coroutine[Any, Any, None]]
|
AsyncMessageCallbackType = Callable[[ReceiveMessage], Coroutine[Any, Any, None]]
|
||||||
|
|
|
@ -2085,7 +2085,9 @@ async def test_handle_message_callback(
|
||||||
callbacks.append(args)
|
callbacks.append(args)
|
||||||
|
|
||||||
mock_mqtt = await mqtt_mock_entry()
|
mock_mqtt = await mqtt_mock_entry()
|
||||||
msg = ReceiveMessage("some-topic", b"test-payload", 1, False)
|
msg = ReceiveMessage(
|
||||||
|
"some-topic", b"test-payload", 1, False, "some-topic", datetime.now()
|
||||||
|
)
|
||||||
mqtt_client_mock.on_connect(mqtt_client_mock, None, None, 0)
|
mqtt_client_mock.on_connect(mqtt_client_mock, None, None, 0)
|
||||||
await mqtt.async_subscribe(hass, "some-topic", _callback)
|
await mqtt.async_subscribe(hass, "some-topic", _callback)
|
||||||
mqtt_client_mock.on_message(mock_mqtt, None, msg)
|
mqtt_client_mock.on_message(mock_mqtt, None, msg)
|
||||||
|
|
Loading…
Add table
Reference in a new issue