mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Avoid redundant calls to async_write_ha_state in mqtt number (#100808)
Avoid redundant calls to async_write_ha_state
This commit is contained in:
parent
98cc2e8098
commit
180f248370
@ -42,7 +42,12 @@ from .const import (
|
|||||||
CONF_STATE_TOPIC,
|
CONF_STATE_TOPIC,
|
||||||
)
|
)
|
||||||
from .debug_info import log_messages
|
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 (
|
from .models import (
|
||||||
MqttCommandTemplate,
|
MqttCommandTemplate,
|
||||||
MqttValueTemplate,
|
MqttValueTemplate,
|
||||||
@ -50,7 +55,6 @@ from .models import (
|
|||||||
ReceiveMessage,
|
ReceiveMessage,
|
||||||
ReceivePayloadType,
|
ReceivePayloadType,
|
||||||
)
|
)
|
||||||
from .util import get_mqtt_data
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -183,6 +187,7 @@ class MqttNumber(MqttEntity, RestoreNumber):
|
|||||||
|
|
||||||
@callback
|
@callback
|
||||||
@log_messages(self.hass, self.entity_id)
|
@log_messages(self.hass, self.entity_id)
|
||||||
|
@write_state_on_attr_change(self, {"_attr_native_value"})
|
||||||
def message_received(msg: ReceiveMessage) -> None:
|
def message_received(msg: ReceiveMessage) -> None:
|
||||||
"""Handle new MQTT messages."""
|
"""Handle new MQTT messages."""
|
||||||
num_value: int | float | None
|
num_value: int | float | None
|
||||||
@ -214,7 +219,6 @@ class MqttNumber(MqttEntity, RestoreNumber):
|
|||||||
return
|
return
|
||||||
|
|
||||||
self._attr_native_value = num_value
|
self._attr_native_value = num_value
|
||||||
get_mqtt_data(self.hass).state_write_requests.write_state_request(self)
|
|
||||||
|
|
||||||
if self._config.get(CONF_STATE_TOPIC) is None:
|
if self._config.get(CONF_STATE_TOPIC) is None:
|
||||||
# Force into optimistic mode.
|
# Force into optimistic mode.
|
||||||
|
@ -31,6 +31,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant, State
|
from homeassistant.core import HomeAssistant, State
|
||||||
|
|
||||||
from .test_common import (
|
from .test_common import (
|
||||||
|
help_custom_config,
|
||||||
help_test_availability_when_connection_lost,
|
help_test_availability_when_connection_lost,
|
||||||
help_test_availability_without_topic,
|
help_test_availability_without_topic,
|
||||||
help_test_custom_availability_payload,
|
help_test_custom_availability_payload,
|
||||||
@ -54,6 +55,7 @@ from .test_common import (
|
|||||||
help_test_setting_attribute_via_mqtt_json_message,
|
help_test_setting_attribute_via_mqtt_json_message,
|
||||||
help_test_setting_attribute_with_template,
|
help_test_setting_attribute_with_template,
|
||||||
help_test_setting_blocked_attribute_via_mqtt_json_message,
|
help_test_setting_blocked_attribute_via_mqtt_json_message,
|
||||||
|
help_test_skipped_async_ha_write_state,
|
||||||
help_test_unique_id,
|
help_test_unique_id,
|
||||||
help_test_unload_config_entry_with_platform,
|
help_test_unload_config_entry_with_platform,
|
||||||
help_test_update_with_json_attrs_bad_json,
|
help_test_update_with_json_attrs_bad_json,
|
||||||
@ -1140,3 +1142,39 @@ async def test_entity_name(
|
|||||||
await help_test_entity_name(
|
await help_test_entity_name(
|
||||||
hass, mqtt_mock_entry, domain, config, expected_friendly_name, device_class
|
hass, mqtt_mock_entry, domain, config, expected_friendly_name, device_class
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"hass_config",
|
||||||
|
[
|
||||||
|
help_custom_config(
|
||||||
|
number.DOMAIN,
|
||||||
|
DEFAULT_CONFIG,
|
||||||
|
(
|
||||||
|
{
|
||||||
|
"availability_topic": "availability-topic",
|
||||||
|
"json_attributes_topic": "json-attributes-topic",
|
||||||
|
"state_topic": "test-topic",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("topic", "payload1", "payload2"),
|
||||||
|
[
|
||||||
|
("test-topic", "10", "20.7"),
|
||||||
|
("availability-topic", "online", "offline"),
|
||||||
|
("json-attributes-topic", '{"attr1": "val1"}', '{"attr1": "val2"}'),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_skipped_async_ha_write_state(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||||
|
topic: str,
|
||||||
|
payload1: str,
|
||||||
|
payload2: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test a write state command is only called when there is change."""
|
||||||
|
await mqtt_mock_entry()
|
||||||
|
await help_test_skipped_async_ha_write_state(hass, topic, payload1, payload2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user