Refactor mqtt callbacks for cover (#118044)

This commit is contained in:
Jan Bouwhuis 2024-05-24 22:26:24 +02:00 committed by GitHub
parent 4b89443f62
commit 35a20d9c60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@
from __future__ import annotations
from contextlib import suppress
from functools import partial
import logging
from typing import Any
@ -62,12 +63,7 @@ from .const import (
DEFAULT_POSITION_OPEN,
DEFAULT_RETAIN,
)
from .debug_info import log_messages
from .mixins import (
MqttEntity,
async_setup_entity_entry_helper,
write_state_on_attr_change,
)
from .mixins import MqttEntity, async_setup_entity_entry_helper
from .models import MqttCommandTemplate, MqttValueTemplate, ReceiveMessage
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
from .util import valid_publish_topic, valid_subscribe_topic
@ -360,14 +356,8 @@ class MqttCover(MqttEntity, CoverEntity):
self._attr_is_opening = state == STATE_OPENING
self._attr_is_closing = state == STATE_CLOSING
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
topics = {}
@callback
@log_messages(self.hass, self.entity_id)
@write_state_on_attr_change(self, {"_attr_current_cover_tilt_position"})
def tilt_message_received(msg: ReceiveMessage) -> None:
def _tilt_message_received(self, msg: ReceiveMessage) -> None:
"""Handle tilt updates."""
payload = self._tilt_status_template(msg.payload)
@ -378,11 +368,7 @@ class MqttCover(MqttEntity, CoverEntity):
self.tilt_payload_received(payload)
@callback
@log_messages(self.hass, self.entity_id)
@write_state_on_attr_change(
self, {"_attr_is_closed", "_attr_is_closing", "_attr_is_opening"}
)
def state_message_received(msg: ReceiveMessage) -> None:
def _state_message_received(self, msg: ReceiveMessage) -> None:
"""Handle new MQTT state messages."""
payload = self._value_template(msg.payload)
@ -424,18 +410,7 @@ class MqttCover(MqttEntity, CoverEntity):
self._update_state(state)
@callback
@log_messages(self.hass, self.entity_id)
@write_state_on_attr_change(
self,
{
"_attr_current_cover_position",
"_attr_current_cover_tilt_position",
"_attr_is_closed",
"_attr_is_closing",
"_attr_is_opening",
},
)
def position_message_received(msg: ReceiveMessage) -> None:
def _position_message_received(self, msg: ReceiveMessage) -> None:
"""Handle new MQTT position messages."""
payload: ReceivePayloadType = self._get_position_template(msg.payload)
payload_dict: Any = None
@ -475,10 +450,25 @@ class MqttCover(MqttEntity, CoverEntity):
STATE_CLOSED if self.current_cover_position == 0 else STATE_OPEN
)
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
topics = {}
if self._config.get(CONF_GET_POSITION_TOPIC):
topics["get_position_topic"] = {
"topic": self._config.get(CONF_GET_POSITION_TOPIC),
"msg_callback": position_message_received,
"msg_callback": partial(
self._message_callback,
self._position_message_received,
{
"_attr_current_cover_position",
"_attr_current_cover_tilt_position",
"_attr_is_closed",
"_attr_is_closing",
"_attr_is_opening",
},
),
"entity_id": self.entity_id,
"qos": self._config[CONF_QOS],
"encoding": self._config[CONF_ENCODING] or None,
}
@ -486,7 +476,12 @@ class MqttCover(MqttEntity, CoverEntity):
if self._config.get(CONF_STATE_TOPIC):
topics["state_topic"] = {
"topic": self._config.get(CONF_STATE_TOPIC),
"msg_callback": state_message_received,
"msg_callback": partial(
self._message_callback,
self._state_message_received,
{"_attr_is_closed", "_attr_is_closing", "_attr_is_opening"},
),
"entity_id": self.entity_id,
"qos": self._config[CONF_QOS],
"encoding": self._config[CONF_ENCODING] or None,
}
@ -494,7 +489,12 @@ class MqttCover(MqttEntity, CoverEntity):
if self._config.get(CONF_TILT_STATUS_TOPIC) is not None:
topics["tilt_status_topic"] = {
"topic": self._config.get(CONF_TILT_STATUS_TOPIC),
"msg_callback": tilt_message_received,
"msg_callback": partial(
self._message_callback,
self._tilt_message_received,
{"_attr_current_cover_tilt_position"},
),
"entity_id": self.entity_id,
"qos": self._config[CONF_QOS],
"encoding": self._config[CONF_ENCODING] or None,
}