mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 00:37:13 +00:00
Remove code that is no longer used in mqtt (#118143)
This commit is contained in:
parent
6a0e7cfea5
commit
5eeeb8c11f
@ -3,10 +3,8 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from collections.abc import Callable
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
from functools import wraps
|
|
||||||
import time
|
import time
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
@ -21,34 +19,6 @@ from .models import DATA_MQTT, MessageCallbackType, PublishPayloadType
|
|||||||
STORED_MESSAGES = 10
|
STORED_MESSAGES = 10
|
||||||
|
|
||||||
|
|
||||||
def log_messages(
|
|
||||||
hass: HomeAssistant, entity_id: str
|
|
||||||
) -> Callable[[MessageCallbackType], MessageCallbackType]:
|
|
||||||
"""Wrap an MQTT message callback to support message logging."""
|
|
||||||
|
|
||||||
debug_info_entities = hass.data[DATA_MQTT].debug_info_entities
|
|
||||||
|
|
||||||
def _log_message(msg: Any) -> None:
|
|
||||||
"""Log message."""
|
|
||||||
messages = debug_info_entities[entity_id]["subscriptions"][
|
|
||||||
msg.subscribed_topic
|
|
||||||
]["messages"]
|
|
||||||
if msg not in messages:
|
|
||||||
messages.append(msg)
|
|
||||||
|
|
||||||
def _decorator(msg_callback: MessageCallbackType) -> MessageCallbackType:
|
|
||||||
@wraps(msg_callback)
|
|
||||||
def wrapper(msg: Any) -> None:
|
|
||||||
"""Log message."""
|
|
||||||
_log_message(msg)
|
|
||||||
msg_callback(msg)
|
|
||||||
|
|
||||||
setattr(wrapper, "__entity_id", entity_id)
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
return _decorator
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class TimestampedPublishMessage:
|
class TimestampedPublishMessage:
|
||||||
"""MQTT Message."""
|
"""MQTT Message."""
|
||||||
|
@ -5,7 +5,7 @@ from __future__ import annotations
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from collections.abc import Callable, Coroutine
|
from collections.abc import Callable, Coroutine
|
||||||
import functools
|
import functools
|
||||||
from functools import partial, wraps
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Any, Protocol, cast, final
|
from typing import TYPE_CHECKING, Any, Protocol, cast, final
|
||||||
|
|
||||||
@ -359,45 +359,6 @@ def init_entity_id_from_config(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def write_state_on_attr_change(
|
|
||||||
entity: Entity, attributes: set[str]
|
|
||||||
) -> Callable[[MessageCallbackType], MessageCallbackType]:
|
|
||||||
"""Wrap an MQTT message callback to track state attribute changes."""
|
|
||||||
|
|
||||||
def _attrs_have_changed(tracked_attrs: dict[str, Any]) -> bool:
|
|
||||||
"""Return True if attributes on entity changed or if update is forced."""
|
|
||||||
if not (write_state := (getattr(entity, "_attr_force_update", False))):
|
|
||||||
for attribute, last_value in tracked_attrs.items():
|
|
||||||
if getattr(entity, attribute, UNDEFINED) != last_value:
|
|
||||||
write_state = True
|
|
||||||
break
|
|
||||||
|
|
||||||
return write_state
|
|
||||||
|
|
||||||
def _decorator(msg_callback: MessageCallbackType) -> MessageCallbackType:
|
|
||||||
@wraps(msg_callback)
|
|
||||||
def wrapper(msg: ReceiveMessage) -> None:
|
|
||||||
"""Track attributes for write state requests."""
|
|
||||||
tracked_attrs: dict[str, Any] = {
|
|
||||||
attribute: getattr(entity, attribute, UNDEFINED)
|
|
||||||
for attribute in attributes
|
|
||||||
}
|
|
||||||
try:
|
|
||||||
msg_callback(msg)
|
|
||||||
except MqttValueTemplateException as exc:
|
|
||||||
_LOGGER.warning(exc)
|
|
||||||
return
|
|
||||||
if not _attrs_have_changed(tracked_attrs):
|
|
||||||
return
|
|
||||||
|
|
||||||
mqtt_data = entity.hass.data[DATA_MQTT]
|
|
||||||
mqtt_data.state_write_requests.write_state_request(entity)
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
return _decorator
|
|
||||||
|
|
||||||
|
|
||||||
class MqttAttributesMixin(Entity):
|
class MqttAttributesMixin(Entity):
|
||||||
"""Mixin used for platforms that support JSON attributes."""
|
"""Mixin used for platforms that support JSON attributes."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user