Correct handling of existing MQTT subscriptions (#43056)
This commit is contained in:
parent
d7bcf4a3b4
commit
4e49bd0596
2 changed files with 22 additions and 7 deletions
|
@ -29,6 +29,7 @@ class EntitySubscription:
|
|||
async def resubscribe_if_necessary(self, hass, other):
|
||||
"""Re-subscribe to the new topic if necessary."""
|
||||
if not self._should_resubscribe(other):
|
||||
self.unsubscribe_callback = other.unsubscribe_callback
|
||||
return
|
||||
|
||||
if other is not None and other.unsubscribe_callback is not None:
|
||||
|
|
|
@ -160,21 +160,35 @@ async def test_qos_encoding_custom(hass, mqtt_mock, caplog):
|
|||
async def test_no_change(hass, mqtt_mock, caplog):
|
||||
"""Test subscription to topics without change."""
|
||||
|
||||
calls = []
|
||||
|
||||
@callback
|
||||
def msg_callback(*args):
|
||||
"""Do nothing."""
|
||||
pass
|
||||
def record_calls(*args):
|
||||
"""Record calls."""
|
||||
calls.append(args)
|
||||
|
||||
sub_state = None
|
||||
sub_state = await async_subscribe_topics(
|
||||
hass,
|
||||
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(
|
||||
hass,
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue