From ea1108503d1665f03fc210d5fa05077e6f86b2a0 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Mon, 25 Sep 2023 21:08:14 +0200 Subject: [PATCH] Rework and fix mqtt siren writing state and attributes (#100871) Rework mqtt siren writing state and attributes --- homeassistant/components/mqtt/siren.py | 10 ++++++---- tests/components/mqtt/test_siren.py | 11 ++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/mqtt/siren.py b/homeassistant/components/mqtt/siren.py index aeabd0fe148..0d8a53e98ea 100644 --- a/homeassistant/components/mqtt/siren.py +++ b/homeassistant/components/mqtt/siren.py @@ -265,6 +265,7 @@ class MqttSiren(MqttEntity, SirenEntity): if json_payload[STATE] == PAYLOAD_NONE: self._attr_is_on = None del json_payload[STATE] + get_mqtt_data(self.hass).state_write_requests.write_state_request(self) if json_payload: # process attributes @@ -279,7 +280,7 @@ class MqttSiren(MqttEntity, SirenEntity): ) return self._update(process_turn_on_params(self, params)) - get_mqtt_data(self.hass).state_write_requests.write_state_request(self) + 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. @@ -379,6 +380,7 @@ class MqttSiren(MqttEntity, SirenEntity): """Update the extra siren state attributes.""" for attribute, support in SUPPORTED_ATTRIBUTES.items(): if self._attr_supported_features & support and attribute in data: - self._extra_attributes[attribute] = data[ - attribute # type: ignore[literal-required] - ] + data_attr = data[attribute] # type: ignore[literal-required] + if self._extra_attributes.get(attribute) == data_attr: + continue + self._extra_attributes[attribute] = data_attr diff --git a/tests/components/mqtt/test_siren.py b/tests/components/mqtt/test_siren.py index 7c448eba85e..4f703ff0023 100644 --- a/tests/components/mqtt/test_siren.py +++ b/tests/components/mqtt/test_siren.py @@ -257,7 +257,7 @@ async def test_controlling_state_and_attributes_with_json_message_without_templa async_fire_mqtt_message( hass, "state-topic", - '{"state":"beer off", "duration": 5, "volume_level": 0.6}', + '{"state":"beer off", "tone": "bell", "duration": 5, "volume_level": 0.6}', ) state = hass.states.get("siren.test") @@ -270,14 +270,15 @@ async def test_controlling_state_and_attributes_with_json_message_without_templa async_fire_mqtt_message( hass, "state-topic", - '{"state":"beer on", "duration": 6, "volume_level": 2 }', + '{"state":"beer on", "duration": 6, "volume_level": 2,"tone": "ping"}', ) state = hass.states.get("siren.test") assert ( - "Unable to update siren state attributes from payload '{'duration': 6, 'volume_level': 2}': value must be at most 1 for dictionary value @ data['volume_level']" + "Unable to update siren state attributes from payload '{'duration': 6, 'volume_level': 2, 'tone': 'ping'}': value must be at most 1 for dictionary value @ data['volume_level']" in caplog.text ) - assert state.state == STATE_OFF + # Only the on/of state was updated, not the attributes + assert state.state == STATE_ON assert state.attributes.get(siren.ATTR_TONE) == "bell" assert state.attributes.get(siren.ATTR_DURATION) == 5 assert state.attributes.get(siren.ATTR_VOLUME_LEVEL) == 0.6 @@ -287,7 +288,7 @@ async def test_controlling_state_and_attributes_with_json_message_without_templa "state-topic", "{}", ) - assert state.state == STATE_OFF + assert state.state == STATE_ON assert state.attributes.get(siren.ATTR_TONE) == "bell" assert state.attributes.get(siren.ATTR_DURATION) == 5 assert state.attributes.get(siren.ATTR_VOLUME_LEVEL) == 0.6