mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 08:47:09 +00:00
Rework mqtt callbacks for device_tracker (#118110)
This commit is contained in:
parent
521ed0a220
commit
5d7a735da6
@ -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 TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
@ -32,13 +33,7 @@ from homeassistant.helpers.typing import ConfigType
|
|||||||
from . import subscription
|
from . import subscription
|
||||||
from .config import MQTT_BASE_SCHEMA
|
from .config import MQTT_BASE_SCHEMA
|
||||||
from .const import CONF_PAYLOAD_RESET, CONF_QOS, CONF_STATE_TOPIC
|
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
|
||||||
from .mixins import (
|
|
||||||
CONF_JSON_ATTRS_TOPIC,
|
|
||||||
MqttEntity,
|
|
||||||
async_setup_entity_entry_helper,
|
|
||||||
write_state_on_attr_change,
|
|
||||||
)
|
|
||||||
from .models import MqttValueTemplate, ReceiveMessage, ReceivePayloadType
|
from .models import MqttValueTemplate, ReceiveMessage, ReceivePayloadType
|
||||||
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
||||||
from .util import valid_subscribe_topic
|
from .util import valid_subscribe_topic
|
||||||
@ -119,33 +114,31 @@ class MqttDeviceTracker(MqttEntity, TrackerEntity):
|
|||||||
config.get(CONF_VALUE_TEMPLATE), entity=self
|
config.get(CONF_VALUE_TEMPLATE), entity=self
|
||||||
).async_render_with_possible_json_value
|
).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:
|
def _prepare_subscribe_topics(self) -> None:
|
||||||
"""(Re)Subscribe to topics."""
|
"""(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)
|
state_topic: str | None = self._config.get(CONF_STATE_TOPIC)
|
||||||
if state_topic is None:
|
if state_topic is None:
|
||||||
return
|
return
|
||||||
@ -155,7 +148,12 @@ class MqttDeviceTracker(MqttEntity, TrackerEntity):
|
|||||||
{
|
{
|
||||||
"state_topic": {
|
"state_topic": {
|
||||||
"topic": 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],
|
"qos": self._config[CONF_QOS],
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user