mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Refactor mqtt callbacks for cover (#118044)
This commit is contained in:
parent
4b89443f62
commit
35a20d9c60
@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -62,12 +63,7 @@ from .const import (
|
|||||||
DEFAULT_POSITION_OPEN,
|
DEFAULT_POSITION_OPEN,
|
||||||
DEFAULT_RETAIN,
|
DEFAULT_RETAIN,
|
||||||
)
|
)
|
||||||
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 MqttCommandTemplate, MqttValueTemplate, ReceiveMessage
|
from .models import MqttCommandTemplate, MqttValueTemplate, ReceiveMessage
|
||||||
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
||||||
from .util import valid_publish_topic, valid_subscribe_topic
|
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_opening = state == STATE_OPENING
|
||||||
self._attr_is_closing = state == STATE_CLOSING
|
self._attr_is_closing = state == STATE_CLOSING
|
||||||
|
|
||||||
def _prepare_subscribe_topics(self) -> None:
|
|
||||||
"""(Re)Subscribe to topics."""
|
|
||||||
topics = {}
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@log_messages(self.hass, self.entity_id)
|
def _tilt_message_received(self, msg: ReceiveMessage) -> None:
|
||||||
@write_state_on_attr_change(self, {"_attr_current_cover_tilt_position"})
|
|
||||||
def tilt_message_received(msg: ReceiveMessage) -> None:
|
|
||||||
"""Handle tilt updates."""
|
"""Handle tilt updates."""
|
||||||
payload = self._tilt_status_template(msg.payload)
|
payload = self._tilt_status_template(msg.payload)
|
||||||
|
|
||||||
@ -378,11 +368,7 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||||||
self.tilt_payload_received(payload)
|
self.tilt_payload_received(payload)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@log_messages(self.hass, self.entity_id)
|
def _state_message_received(self, msg: ReceiveMessage) -> None:
|
||||||
@write_state_on_attr_change(
|
|
||||||
self, {"_attr_is_closed", "_attr_is_closing", "_attr_is_opening"}
|
|
||||||
)
|
|
||||||
def state_message_received(msg: ReceiveMessage) -> None:
|
|
||||||
"""Handle new MQTT state messages."""
|
"""Handle new MQTT state messages."""
|
||||||
payload = self._value_template(msg.payload)
|
payload = self._value_template(msg.payload)
|
||||||
|
|
||||||
@ -424,18 +410,7 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||||||
self._update_state(state)
|
self._update_state(state)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@log_messages(self.hass, self.entity_id)
|
def _position_message_received(self, msg: ReceiveMessage) -> None:
|
||||||
@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:
|
|
||||||
"""Handle new MQTT position messages."""
|
"""Handle new MQTT position messages."""
|
||||||
payload: ReceivePayloadType = self._get_position_template(msg.payload)
|
payload: ReceivePayloadType = self._get_position_template(msg.payload)
|
||||||
payload_dict: Any = None
|
payload_dict: Any = None
|
||||||
@ -475,10 +450,25 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||||||
STATE_CLOSED if self.current_cover_position == 0 else STATE_OPEN
|
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):
|
if self._config.get(CONF_GET_POSITION_TOPIC):
|
||||||
topics["get_position_topic"] = {
|
topics["get_position_topic"] = {
|
||||||
"topic": self._config.get(CONF_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],
|
"qos": self._config[CONF_QOS],
|
||||||
"encoding": self._config[CONF_ENCODING] or None,
|
"encoding": self._config[CONF_ENCODING] or None,
|
||||||
}
|
}
|
||||||
@ -486,7 +476,12 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||||||
if self._config.get(CONF_STATE_TOPIC):
|
if self._config.get(CONF_STATE_TOPIC):
|
||||||
topics["state_topic"] = {
|
topics["state_topic"] = {
|
||||||
"topic": self._config.get(CONF_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],
|
"qos": self._config[CONF_QOS],
|
||||||
"encoding": self._config[CONF_ENCODING] or None,
|
"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:
|
if self._config.get(CONF_TILT_STATUS_TOPIC) is not None:
|
||||||
topics["tilt_status_topic"] = {
|
topics["tilt_status_topic"] = {
|
||||||
"topic": self._config.get(CONF_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],
|
"qos": self._config[CONF_QOS],
|
||||||
"encoding": self._config[CONF_ENCODING] or None,
|
"encoding": self._config[CONF_ENCODING] or None,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user