Convert remaining mqtt attrs classes to dataclasses (#118073)
This commit is contained in:
parent
42232ecc8a
commit
d71c7705ae
2 changed files with 28 additions and 29 deletions
|
@ -3,10 +3,10 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass, field
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
import attr
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
|
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"
|
LOG_NAME = "Device trigger"
|
||||||
|
|
||||||
|
|
||||||
@attr.s(slots=True)
|
@dataclass(slots=True)
|
||||||
class TriggerInstance:
|
class TriggerInstance:
|
||||||
"""Attached trigger settings."""
|
"""Attached trigger settings."""
|
||||||
|
|
||||||
action: TriggerActionType = attr.ib()
|
action: TriggerActionType
|
||||||
trigger_info: TriggerInfo = attr.ib()
|
trigger_info: TriggerInfo
|
||||||
trigger: Trigger = attr.ib()
|
trigger: Trigger
|
||||||
remove: CALLBACK_TYPE | None = attr.ib(default=None)
|
remove: CALLBACK_TYPE | None = None
|
||||||
|
|
||||||
async def async_attach_trigger(self) -> None:
|
async def async_attach_trigger(self) -> None:
|
||||||
"""Attach MQTT trigger."""
|
"""Attach MQTT trigger."""
|
||||||
|
@ -117,21 +117,21 @@ class TriggerInstance:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(slots=True)
|
@dataclass(slots=True, kw_only=True)
|
||||||
class Trigger:
|
class Trigger:
|
||||||
"""Device trigger settings."""
|
"""Device trigger settings."""
|
||||||
|
|
||||||
device_id: str = attr.ib()
|
device_id: str
|
||||||
discovery_data: DiscoveryInfoType | None = attr.ib()
|
discovery_data: DiscoveryInfoType | None = None
|
||||||
discovery_id: str | None = attr.ib()
|
discovery_id: str | None = None
|
||||||
hass: HomeAssistant = attr.ib()
|
hass: HomeAssistant
|
||||||
payload: str | None = attr.ib()
|
payload: str | None
|
||||||
qos: int | None = attr.ib()
|
qos: int | None
|
||||||
subtype: str = attr.ib()
|
subtype: str
|
||||||
topic: str | None = attr.ib()
|
topic: str | None
|
||||||
type: str = attr.ib()
|
type: str
|
||||||
value_template: str | None = attr.ib()
|
value_template: str | None
|
||||||
trigger_instances: list[TriggerInstance] = attr.ib(factory=list)
|
trigger_instances: list[TriggerInstance] = field(default_factory=list)
|
||||||
|
|
||||||
async def add_trigger(
|
async def add_trigger(
|
||||||
self, action: TriggerActionType, trigger_info: TriggerInfo
|
self, action: TriggerActionType, trigger_info: TriggerInfo
|
||||||
|
|
|
@ -3,10 +3,9 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable, Coroutine
|
from collections.abc import Callable, Coroutine
|
||||||
|
from dataclasses import dataclass
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
import attr
|
|
||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .. import mqtt
|
from .. import mqtt
|
||||||
|
@ -15,18 +14,18 @@ from .const import DEFAULT_QOS
|
||||||
from .models import MessageCallbackType
|
from .models import MessageCallbackType
|
||||||
|
|
||||||
|
|
||||||
@attr.s(slots=True)
|
@dataclass(slots=True)
|
||||||
class EntitySubscription:
|
class EntitySubscription:
|
||||||
"""Class to hold data about an active entity topic subscription."""
|
"""Class to hold data about an active entity topic subscription."""
|
||||||
|
|
||||||
hass: HomeAssistant = attr.ib()
|
hass: HomeAssistant
|
||||||
topic: str | None = attr.ib()
|
topic: str | None
|
||||||
message_callback: MessageCallbackType = attr.ib()
|
message_callback: MessageCallbackType
|
||||||
subscribe_task: Coroutine[Any, Any, Callable[[], None]] | None = attr.ib()
|
subscribe_task: Coroutine[Any, Any, Callable[[], None]] | None
|
||||||
unsubscribe_callback: Callable[[], None] | None = attr.ib()
|
unsubscribe_callback: Callable[[], None] | None
|
||||||
qos: int = attr.ib(default=0)
|
qos: int = 0
|
||||||
encoding: str = attr.ib(default="utf-8")
|
encoding: str = "utf-8"
|
||||||
entity_id: str | None = attr.ib(default=None)
|
entity_id: str | None = None
|
||||||
|
|
||||||
def resubscribe_if_necessary(
|
def resubscribe_if_necessary(
|
||||||
self, hass: HomeAssistant, other: EntitySubscription | None
|
self, hass: HomeAssistant, other: EntitySubscription | None
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue