Rework mqtt callbacks for device_tracker (#118110)

This commit is contained in:
Jan Bouwhuis 2024-05-25 23:07:50 +02:00 committed by GitHub
parent 521ed0a220
commit 5d7a735da6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@
from __future__ import annotations
from collections.abc import Callable
from functools import partial
import logging
from typing import TYPE_CHECKING
@ -32,13 +33,7 @@ from homeassistant.helpers.typing import ConfigType
from . import subscription
from .config import MQTT_BASE_SCHEMA
from .const import CONF_PAYLOAD_RESET, CONF_QOS, CONF_STATE_TOPIC
from .debug_info import log_messages
from .mixins import (
CONF_JSON_ATTRS_TOPIC,
MqttEntity,
async_setup_entity_entry_helper,
write_state_on_attr_change,
)
from .mixins import CONF_JSON_ATTRS_TOPIC, MqttEntity, async_setup_entity_entry_helper
from .models import MqttValueTemplate, ReceiveMessage, ReceivePayloadType
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
from .util import valid_subscribe_topic
@ -119,33 +114,31 @@ class MqttDeviceTracker(MqttEntity, TrackerEntity):
config.get(CONF_VALUE_TEMPLATE), entity=self
).async_render_with_possible_json_value
@callback
def _tracker_message_received(self, msg: ReceiveMessage) -> None:
"""Handle new MQTT messages."""
payload = self._value_template(msg.payload)
if not payload.strip(): # No output from template, ignore
_LOGGER.debug(
"Ignoring empty payload '%s' after rendering for topic %s",
payload,
msg.topic,
)
return
if payload == self._config[CONF_PAYLOAD_HOME]:
self._location_name = STATE_HOME
elif payload == self._config[CONF_PAYLOAD_NOT_HOME]:
self._location_name = STATE_NOT_HOME
elif payload == self._config[CONF_PAYLOAD_RESET]:
self._location_name = None
else:
if TYPE_CHECKING:
assert isinstance(msg.payload, str)
self._location_name = msg.payload
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
@callback
@log_messages(self.hass, self.entity_id)
@write_state_on_attr_change(self, {"_location_name"})
def message_received(msg: ReceiveMessage) -> None:
"""Handle new MQTT messages."""
payload = self._value_template(msg.payload)
if not payload.strip(): # No output from template, ignore
_LOGGER.debug(
"Ignoring empty payload '%s' after rendering for topic %s",
payload,
msg.topic,
)
return
if payload == self._config[CONF_PAYLOAD_HOME]:
self._location_name = STATE_HOME
elif payload == self._config[CONF_PAYLOAD_NOT_HOME]:
self._location_name = STATE_NOT_HOME
elif payload == self._config[CONF_PAYLOAD_RESET]:
self._location_name = None
else:
if TYPE_CHECKING:
assert isinstance(msg.payload, str)
self._location_name = msg.payload
state_topic: str | None = self._config.get(CONF_STATE_TOPIC)
if state_topic is None:
return
@ -155,7 +148,12 @@ class MqttDeviceTracker(MqttEntity, TrackerEntity):
{
"state_topic": {
"topic": state_topic,
"msg_callback": message_received,
"msg_callback": partial(
self._message_callback,
self._tracker_message_received,
{"_location_name"},
),
"entity_id": self.entity_id,
"qos": self._config[CONF_QOS],
}
},