mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Refactor mqtt callbacks for alarm_control_panel (#118037)
This commit is contained in:
parent
ee38099a91
commit
4b89443f62
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -24,7 +25,7 @@ from homeassistant.const import (
|
|||||||
STATE_ALARM_PENDING,
|
STATE_ALARM_PENDING,
|
||||||
STATE_ALARM_TRIGGERED,
|
STATE_ALARM_TRIGGERED,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
@ -40,12 +41,7 @@ from .const import (
|
|||||||
CONF_STATE_TOPIC,
|
CONF_STATE_TOPIC,
|
||||||
CONF_SUPPORTED_FEATURES,
|
CONF_SUPPORTED_FEATURES,
|
||||||
)
|
)
|
||||||
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
|
||||||
@ -177,13 +173,7 @@ class MqttAlarm(MqttEntity, alarm.AlarmControlPanelEntity):
|
|||||||
self._attr_code_format = alarm.CodeFormat.TEXT
|
self._attr_code_format = alarm.CodeFormat.TEXT
|
||||||
self._attr_code_arm_required = bool(self._config[CONF_CODE_ARM_REQUIRED])
|
self._attr_code_arm_required = bool(self._config[CONF_CODE_ARM_REQUIRED])
|
||||||
|
|
||||||
def _prepare_subscribe_topics(self) -> None:
|
def _state_message_received(self, msg: ReceiveMessage) -> None:
|
||||||
"""(Re)Subscribe to topics."""
|
|
||||||
|
|
||||||
@callback
|
|
||||||
@log_messages(self.hass, self.entity_id)
|
|
||||||
@write_state_on_attr_change(self, {"_attr_state"})
|
|
||||||
def message_received(msg: ReceiveMessage) -> None:
|
|
||||||
"""Run when new MQTT message has been received."""
|
"""Run when new MQTT message has been received."""
|
||||||
payload = self._value_template(msg.payload)
|
payload = self._value_template(msg.payload)
|
||||||
if payload not in (
|
if payload not in (
|
||||||
@ -202,13 +192,21 @@ class MqttAlarm(MqttEntity, alarm.AlarmControlPanelEntity):
|
|||||||
return
|
return
|
||||||
self._attr_state = str(payload)
|
self._attr_state = str(payload)
|
||||||
|
|
||||||
|
def _prepare_subscribe_topics(self) -> None:
|
||||||
|
"""(Re)Subscribe to topics."""
|
||||||
|
|
||||||
self._sub_state = subscription.async_prepare_subscribe_topics(
|
self._sub_state = subscription.async_prepare_subscribe_topics(
|
||||||
self.hass,
|
self.hass,
|
||||||
self._sub_state,
|
self._sub_state,
|
||||||
{
|
{
|
||||||
"state_topic": {
|
"state_topic": {
|
||||||
"topic": self._config[CONF_STATE_TOPIC],
|
"topic": self._config[CONF_STATE_TOPIC],
|
||||||
"msg_callback": message_received,
|
"msg_callback": partial(
|
||||||
|
self._message_callback,
|
||||||
|
self._state_message_received,
|
||||||
|
{"_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