Correct handling of existing MQTT subscriptions (#43056)

This commit is contained in:
Erik Montnemery 2020-11-10 21:55:26 +01:00 committed by GitHub
parent d7bcf4a3b4
commit 4e49bd0596
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 7 deletions

View file

@ -29,6 +29,7 @@ class EntitySubscription:
async def resubscribe_if_necessary(self, hass, other): async def resubscribe_if_necessary(self, hass, other):
"""Re-subscribe to the new topic if necessary.""" """Re-subscribe to the new topic if necessary."""
if not self._should_resubscribe(other): if not self._should_resubscribe(other):
self.unsubscribe_callback = other.unsubscribe_callback
return return
if other is not None and other.unsubscribe_callback is not None: if other is not None and other.unsubscribe_callback is not None:

View file

@ -160,21 +160,35 @@ async def test_qos_encoding_custom(hass, mqtt_mock, caplog):
async def test_no_change(hass, mqtt_mock, caplog): async def test_no_change(hass, mqtt_mock, caplog):
"""Test subscription to topics without change.""" """Test subscription to topics without change."""
calls = []
@callback @callback
def msg_callback(*args): def record_calls(*args):
"""Do nothing.""" """Record calls."""
pass calls.append(args)
sub_state = None sub_state = None
sub_state = await async_subscribe_topics( sub_state = await async_subscribe_topics(
hass, hass,
sub_state, sub_state,
{"test_topic1": {"topic": "test-topic1", "msg_callback": msg_callback}}, {"test_topic1": {"topic": "test-topic1", "msg_callback": record_calls}},
) )
call_count = mqtt_mock.async_subscribe.call_count subscribe_call_count = mqtt_mock.async_subscribe.call_count
async_fire_mqtt_message(hass, "test-topic1", "test-payload")
assert len(calls) == 1
sub_state = await async_subscribe_topics( sub_state = await async_subscribe_topics(
hass, hass,
sub_state, sub_state,
{"test_topic1": {"topic": "test-topic1", "msg_callback": msg_callback}}, {"test_topic1": {"topic": "test-topic1", "msg_callback": record_calls}},
) )
assert call_count == mqtt_mock.async_subscribe.call_count assert subscribe_call_count == mqtt_mock.async_subscribe.call_count
async_fire_mqtt_message(hass, "test-topic1", "test-payload")
assert len(calls) == 2
await async_unsubscribe_topics(hass, sub_state)
async_fire_mqtt_message(hass, "test-topic1", "test-payload")
assert len(calls) == 2