mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Refactor mqtt callbacks for vacuum (#118137)
This commit is contained in:
parent
fc9f7aee7e
commit
ae0c00218a
@ -8,6 +8,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, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
@ -49,12 +50,7 @@ from .const import (
|
|||||||
CONF_STATE_TOPIC,
|
CONF_STATE_TOPIC,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
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 ReceiveMessage
|
from .models import ReceiveMessage
|
||||||
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
||||||
from .util import valid_publish_topic
|
from .util import valid_publish_topic
|
||||||
@ -322,16 +318,8 @@ class MqttStateVacuum(MqttEntity, StateVacuumEntity):
|
|||||||
self._attr_fan_speed = self._state_attrs.get(FAN_SPEED, 0)
|
self._attr_fan_speed = self._state_attrs.get(FAN_SPEED, 0)
|
||||||
self._attr_battery_level = max(0, min(100, self._state_attrs.get(BATTERY, 0)))
|
self._attr_battery_level = max(0, min(100, self._state_attrs.get(BATTERY, 0)))
|
||||||
|
|
||||||
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_message_received(self, msg: ReceiveMessage) -> None:
|
||||||
@write_state_on_attr_change(
|
|
||||||
self, {"_attr_battery_level", "_attr_fan_speed", "_attr_state"}
|
|
||||||
)
|
|
||||||
def state_message_received(msg: ReceiveMessage) -> None:
|
|
||||||
"""Handle state MQTT message."""
|
"""Handle state MQTT message."""
|
||||||
payload = json_loads_object(msg.payload)
|
payload = json_loads_object(msg.payload)
|
||||||
if STATE in payload and (
|
if STATE in payload and (
|
||||||
@ -343,10 +331,19 @@ class MqttStateVacuum(MqttEntity, StateVacuumEntity):
|
|||||||
del payload[STATE]
|
del payload[STATE]
|
||||||
self._update_state_attributes(payload)
|
self._update_state_attributes(payload)
|
||||||
|
|
||||||
|
def _prepare_subscribe_topics(self) -> None:
|
||||||
|
"""(Re)Subscribe to topics."""
|
||||||
|
topics: dict[str, Any] = {}
|
||||||
|
|
||||||
if state_topic := self._config.get(CONF_STATE_TOPIC):
|
if state_topic := self._config.get(CONF_STATE_TOPIC):
|
||||||
topics["state_position_topic"] = {
|
topics["state_position_topic"] = {
|
||||||
"topic": state_topic,
|
"topic": state_topic,
|
||||||
"msg_callback": state_message_received,
|
"msg_callback": partial(
|
||||||
|
self._message_callback,
|
||||||
|
self._state_message_received,
|
||||||
|
{"_attr_battery_level", "_attr_fan_speed", "_attr_state"},
|
||||||
|
),
|
||||||
|
"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