Avoid redundant calls to async_ha_write_state in mqtt switch (#100815)

Avoid redundant calls to async_ha_write_state
This commit is contained in:
Jan Bouwhuis 2023-09-25 18:06:19 +02:00 committed by GitHub
parent 33d45b3454
commit 8d10cdce4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 4 deletions

View file

@ -37,9 +37,13 @@ from .const import (
PAYLOAD_NONE,
)
from .debug_info import log_messages
from .mixins import MQTT_ENTITY_COMMON_SCHEMA, MqttEntity, async_setup_entry_helper
from .mixins import (
MQTT_ENTITY_COMMON_SCHEMA,
MqttEntity,
async_setup_entry_helper,
write_state_on_attr_change,
)
from .models import MqttValueTemplate, ReceiveMessage
from .util import get_mqtt_data
DEFAULT_NAME = "MQTT Switch"
DEFAULT_PAYLOAD_ON = "ON"
@ -136,6 +140,7 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
@callback
@log_messages(self.hass, self.entity_id)
@write_state_on_attr_change(self, {"_attr_is_on"})
def state_message_received(msg: ReceiveMessage) -> None:
"""Handle new MQTT state messages."""
payload = self._value_template(msg.payload)
@ -146,8 +151,6 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
elif payload == PAYLOAD_NONE:
self._attr_is_on = None
get_mqtt_data(self.hass).state_write_requests.write_state_request(self)
if self._config.get(CONF_STATE_TOPIC) is None:
# Force into optimistic mode.
self._optimistic = True