Refactor mqtt callbacks for humidifier (#118116)
This commit is contained in:
parent
b4acadc992
commit
6580a07308
1 changed files with 144 additions and 146 deletions
|
@ -3,6 +3,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
@ -51,12 +52,7 @@ from .const import (
|
||||||
CONF_STATE_VALUE_TEMPLATE,
|
CONF_STATE_VALUE_TEMPLATE,
|
||||||
PAYLOAD_NONE,
|
PAYLOAD_NONE,
|
||||||
)
|
)
|
||||||
from .debug_info import log_messages
|
from .mixins import MqttEntity, async_setup_entity_entry_helper
|
||||||
from .mixins import (
|
|
||||||
MqttEntity,
|
|
||||||
async_setup_entity_entry_helper,
|
|
||||||
write_state_on_attr_change,
|
|
||||||
)
|
|
||||||
from .models import (
|
from .models import (
|
||||||
MqttCommandTemplate,
|
MqttCommandTemplate,
|
||||||
MqttValueTemplate,
|
MqttValueTemplate,
|
||||||
|
@ -284,25 +280,23 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
||||||
topics: dict[str, dict[str, Any]],
|
topics: dict[str, dict[str, Any]],
|
||||||
topic: str,
|
topic: str,
|
||||||
msg_callback: Callable[[ReceiveMessage], None],
|
msg_callback: Callable[[ReceiveMessage], None],
|
||||||
|
tracked_attributes: set[str],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add a subscription."""
|
"""Add a subscription."""
|
||||||
qos: int = self._config[CONF_QOS]
|
qos: int = self._config[CONF_QOS]
|
||||||
if topic in self._topic and self._topic[topic] is not None:
|
if topic in self._topic and self._topic[topic] is not None:
|
||||||
topics[topic] = {
|
topics[topic] = {
|
||||||
"topic": self._topic[topic],
|
"topic": self._topic[topic],
|
||||||
"msg_callback": msg_callback,
|
"msg_callback": partial(
|
||||||
|
self._message_callback, msg_callback, tracked_attributes
|
||||||
|
),
|
||||||
|
"entity_id": self.entity_id,
|
||||||
"qos": qos,
|
"qos": qos,
|
||||||
"encoding": self._config[CONF_ENCODING] or None,
|
"encoding": self._config[CONF_ENCODING] or None,
|
||||||
}
|
}
|
||||||
|
|
||||||
def _prepare_subscribe_topics(self) -> None:
|
|
||||||
"""(Re)Subscribe to topics."""
|
|
||||||
topics: dict[str, Any] = {}
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@log_messages(self.hass, self.entity_id)
|
def _state_received(self, msg: ReceiveMessage) -> None:
|
||||||
@write_state_on_attr_change(self, {"_attr_is_on"})
|
|
||||||
def state_received(msg: ReceiveMessage) -> None:
|
|
||||||
"""Handle new received MQTT message."""
|
"""Handle new received MQTT message."""
|
||||||
payload = self._value_templates[CONF_STATE](msg.payload)
|
payload = self._value_templates[CONF_STATE](msg.payload)
|
||||||
if not payload:
|
if not payload:
|
||||||
|
@ -315,12 +309,8 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
||||||
elif payload == PAYLOAD_NONE:
|
elif payload == PAYLOAD_NONE:
|
||||||
self._attr_is_on = None
|
self._attr_is_on = None
|
||||||
|
|
||||||
self.add_subscription(topics, CONF_STATE_TOPIC, state_received)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@log_messages(self.hass, self.entity_id)
|
def _action_received(self, msg: ReceiveMessage) -> None:
|
||||||
@write_state_on_attr_change(self, {"_attr_action"})
|
|
||||||
def action_received(msg: ReceiveMessage) -> None:
|
|
||||||
"""Handle new received MQTT message."""
|
"""Handle new received MQTT message."""
|
||||||
action_payload = self._value_templates[ATTR_ACTION](msg.payload)
|
action_payload = self._value_templates[ATTR_ACTION](msg.payload)
|
||||||
if not action_payload or action_payload == PAYLOAD_NONE:
|
if not action_payload or action_payload == PAYLOAD_NONE:
|
||||||
|
@ -337,12 +327,8 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
self.add_subscription(topics, CONF_ACTION_TOPIC, action_received)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@log_messages(self.hass, self.entity_id)
|
def _current_humidity_received(self, msg: ReceiveMessage) -> None:
|
||||||
@write_state_on_attr_change(self, {"_attr_current_humidity"})
|
|
||||||
def current_humidity_received(msg: ReceiveMessage) -> None:
|
|
||||||
"""Handle new received MQTT message for the current humidity."""
|
"""Handle new received MQTT message for the current humidity."""
|
||||||
rendered_current_humidity_payload = self._value_templates[
|
rendered_current_humidity_payload = self._value_templates[
|
||||||
ATTR_CURRENT_HUMIDITY
|
ATTR_CURRENT_HUMIDITY
|
||||||
|
@ -373,14 +359,8 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
||||||
return
|
return
|
||||||
self._attr_current_humidity = current_humidity
|
self._attr_current_humidity = current_humidity
|
||||||
|
|
||||||
self.add_subscription(
|
|
||||||
topics, CONF_CURRENT_HUMIDITY_TOPIC, current_humidity_received
|
|
||||||
)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@log_messages(self.hass, self.entity_id)
|
def _target_humidity_received(self, msg: ReceiveMessage) -> None:
|
||||||
@write_state_on_attr_change(self, {"_attr_target_humidity"})
|
|
||||||
def target_humidity_received(msg: ReceiveMessage) -> None:
|
|
||||||
"""Handle new received MQTT message for the target humidity."""
|
"""Handle new received MQTT message for the target humidity."""
|
||||||
rendered_target_humidity_payload = self._value_templates[ATTR_HUMIDITY](
|
rendered_target_humidity_payload = self._value_templates[ATTR_HUMIDITY](
|
||||||
msg.payload
|
msg.payload
|
||||||
|
@ -414,14 +394,8 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
||||||
return
|
return
|
||||||
self._attr_target_humidity = target_humidity
|
self._attr_target_humidity = target_humidity
|
||||||
|
|
||||||
self.add_subscription(
|
|
||||||
topics, CONF_TARGET_HUMIDITY_STATE_TOPIC, target_humidity_received
|
|
||||||
)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@log_messages(self.hass, self.entity_id)
|
def _mode_received(self, msg: ReceiveMessage) -> None:
|
||||||
@write_state_on_attr_change(self, {"_attr_mode"})
|
|
||||||
def mode_received(msg: ReceiveMessage) -> None:
|
|
||||||
"""Handle new received MQTT message for mode."""
|
"""Handle new received MQTT message for mode."""
|
||||||
mode = str(self._value_templates[ATTR_MODE](msg.payload))
|
mode = str(self._value_templates[ATTR_MODE](msg.payload))
|
||||||
if mode == self._payload["MODE_RESET"]:
|
if mode == self._payload["MODE_RESET"]:
|
||||||
|
@ -441,7 +415,31 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
||||||
|
|
||||||
self._attr_mode = mode
|
self._attr_mode = mode
|
||||||
|
|
||||||
self.add_subscription(topics, CONF_MODE_STATE_TOPIC, mode_received)
|
def _prepare_subscribe_topics(self) -> None:
|
||||||
|
"""(Re)Subscribe to topics."""
|
||||||
|
topics: dict[str, Any] = {}
|
||||||
|
|
||||||
|
self.add_subscription(
|
||||||
|
topics, CONF_STATE_TOPIC, self._state_received, {"_attr_is_on"}
|
||||||
|
)
|
||||||
|
self.add_subscription(
|
||||||
|
topics, CONF_ACTION_TOPIC, self._action_received, {"_attr_action"}
|
||||||
|
)
|
||||||
|
self.add_subscription(
|
||||||
|
topics,
|
||||||
|
CONF_CURRENT_HUMIDITY_TOPIC,
|
||||||
|
self._current_humidity_received,
|
||||||
|
{"_attr_current_humidity"},
|
||||||
|
)
|
||||||
|
self.add_subscription(
|
||||||
|
topics,
|
||||||
|
CONF_TARGET_HUMIDITY_STATE_TOPIC,
|
||||||
|
self._target_humidity_received,
|
||||||
|
{"_attr_target_humidity"},
|
||||||
|
)
|
||||||
|
self.add_subscription(
|
||||||
|
topics, CONF_MODE_STATE_TOPIC, self._mode_received, {"_attr_mode"}
|
||||||
|
)
|
||||||
|
|
||||||
self._sub_state = subscription.async_prepare_subscribe_topics(
|
self._sub_state = subscription.async_prepare_subscribe_topics(
|
||||||
self.hass, self._sub_state, topics
|
self.hass, self._sub_state, topics
|
||||||
|
|
Loading…
Add table
Reference in a new issue