Convert remaining mqtt attrs classes to dataclasses (#118073)

This commit is contained in:
J. Nick Koston 2024-05-24 17:44:50 -10:00 committed by GitHub
parent 42232ecc8a
commit d71c7705ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 29 deletions

View file

@ -3,10 +3,10 @@
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass, field
import logging
from typing import TYPE_CHECKING, Any
import attr
import voluptuous as vol
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
@ -84,14 +84,14 @@ TRIGGER_DISCOVERY_SCHEMA = MQTT_BASE_SCHEMA.extend(
LOG_NAME = "Device trigger"
@attr.s(slots=True)
@dataclass(slots=True)
class TriggerInstance:
"""Attached trigger settings."""
action: TriggerActionType = attr.ib()
trigger_info: TriggerInfo = attr.ib()
trigger: Trigger = attr.ib()
remove: CALLBACK_TYPE | None = attr.ib(default=None)
action: TriggerActionType
trigger_info: TriggerInfo
trigger: Trigger
remove: CALLBACK_TYPE | None = None
async def async_attach_trigger(self) -> None:
"""Attach MQTT trigger."""
@ -117,21 +117,21 @@ class TriggerInstance:
)
@attr.s(slots=True)
@dataclass(slots=True, kw_only=True)
class Trigger:
"""Device trigger settings."""
device_id: str = attr.ib()
discovery_data: DiscoveryInfoType | None = attr.ib()
discovery_id: str | None = attr.ib()
hass: HomeAssistant = attr.ib()
payload: str | None = attr.ib()
qos: int | None = attr.ib()
subtype: str = attr.ib()
topic: str | None = attr.ib()
type: str = attr.ib()
value_template: str | None = attr.ib()
trigger_instances: list[TriggerInstance] = attr.ib(factory=list)
device_id: str
discovery_data: DiscoveryInfoType | None = None
discovery_id: str | None = None
hass: HomeAssistant
payload: str | None
qos: int | None
subtype: str
topic: str | None
type: str
value_template: str | None
trigger_instances: list[TriggerInstance] = field(default_factory=list)
async def add_trigger(
self, action: TriggerActionType, trigger_info: TriggerInfo

View file

@ -3,10 +3,9 @@
from __future__ import annotations
from collections.abc import Callable, Coroutine
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
import attr
from homeassistant.core import HomeAssistant
from .. import mqtt
@ -15,18 +14,18 @@ from .const import DEFAULT_QOS
from .models import MessageCallbackType
@attr.s(slots=True)
@dataclass(slots=True)
class EntitySubscription:
"""Class to hold data about an active entity topic subscription."""
hass: HomeAssistant = attr.ib()
topic: str | None = attr.ib()
message_callback: MessageCallbackType = attr.ib()
subscribe_task: Coroutine[Any, Any, Callable[[], None]] | None = attr.ib()
unsubscribe_callback: Callable[[], None] | None = attr.ib()
qos: int = attr.ib(default=0)
encoding: str = attr.ib(default="utf-8")
entity_id: str | None = attr.ib(default=None)
hass: HomeAssistant
topic: str | None
message_callback: MessageCallbackType
subscribe_task: Coroutine[Any, Any, Callable[[], None]] | None
unsubscribe_callback: Callable[[], None] | None
qos: int = 0
encoding: str = "utf-8"
entity_id: str | None = None
def resubscribe_if_necessary(
self, hass: HomeAssistant, other: EntitySubscription | None