Rework and fix mqtt siren writing state and attributes (#100871)

Rework mqtt siren writing state and attributes
This commit is contained in:
Jan Bouwhuis 2023-09-25 21:08:14 +02:00 committed by GitHub
parent d76c5ed351
commit ea1108503d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 9 deletions

View file

@ -265,6 +265,7 @@ class MqttSiren(MqttEntity, SirenEntity):
if json_payload[STATE] == PAYLOAD_NONE: if json_payload[STATE] == PAYLOAD_NONE:
self._attr_is_on = None self._attr_is_on = None
del json_payload[STATE] del json_payload[STATE]
get_mqtt_data(self.hass).state_write_requests.write_state_request(self)
if json_payload: if json_payload:
# process attributes # process attributes
@ -279,7 +280,7 @@ class MqttSiren(MqttEntity, SirenEntity):
) )
return return
self._update(process_turn_on_params(self, params)) 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: if self._config.get(CONF_STATE_TOPIC) is None:
# Force into optimistic mode. # Force into optimistic mode.
@ -379,6 +380,7 @@ class MqttSiren(MqttEntity, SirenEntity):
"""Update the extra siren state attributes.""" """Update the extra siren state attributes."""
for attribute, support in SUPPORTED_ATTRIBUTES.items(): for attribute, support in SUPPORTED_ATTRIBUTES.items():
if self._attr_supported_features & support and attribute in data: if self._attr_supported_features & support and attribute in data:
self._extra_attributes[attribute] = data[ data_attr = data[attribute] # type: ignore[literal-required]
attribute # type: ignore[literal-required] if self._extra_attributes.get(attribute) == data_attr:
] continue
self._extra_attributes[attribute] = data_attr

View file

@ -257,7 +257,7 @@ async def test_controlling_state_and_attributes_with_json_message_without_templa
async_fire_mqtt_message( async_fire_mqtt_message(
hass, hass,
"state-topic", "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") 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( async_fire_mqtt_message(
hass, hass,
"state-topic", "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") state = hass.states.get("siren.test")
assert ( 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 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_TONE) == "bell"
assert state.attributes.get(siren.ATTR_DURATION) == 5 assert state.attributes.get(siren.ATTR_DURATION) == 5
assert state.attributes.get(siren.ATTR_VOLUME_LEVEL) == 0.6 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", "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_TONE) == "bell"
assert state.attributes.get(siren.ATTR_DURATION) == 5 assert state.attributes.get(siren.ATTR_DURATION) == 5
assert state.attributes.get(siren.ATTR_VOLUME_LEVEL) == 0.6 assert state.attributes.get(siren.ATTR_VOLUME_LEVEL) == 0.6