Correct calls to subscription.async_unsubscribe_topics (#19414)

* Correct calls to subscription.async_unsubscribe_topics

* Review comments

* Add testcases
This commit is contained in:
emontnemery 2018-12-19 14:05:24 +01:00 committed by Paulus Schoutsen
parent 8cec559103
commit 1568de62df
20 changed files with 333 additions and 29 deletions

View file

@ -3,7 +3,7 @@ import json
import unittest
from datetime import timedelta, datetime
from unittest.mock import patch
from unittest.mock import ANY, patch
import homeassistant.core as ha
from homeassistant.setup import setup_component, async_setup_component
@ -15,7 +15,7 @@ import homeassistant.util.dt as dt_util
from tests.common import mock_mqtt_component, fire_mqtt_message, \
assert_setup_component, async_fire_mqtt_message, \
async_mock_mqtt_component, MockConfigEntry
async_mock_mqtt_component, MockConfigEntry, mock_registry
from tests.common import get_test_home_assistant, mock_component
@ -542,3 +542,38 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
assert device.name == 'Beer'
assert device.model == 'Glass'
assert device.sw_version == '0.1-beta'
async def test_entity_id_update(hass, mqtt_mock):
"""Test MQTT subscriptions are managed when entity_id is updated."""
registry = mock_registry(hass, {})
mock_mqtt = await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: [{
'platform': 'mqtt',
'name': 'beer',
'state_topic': 'test-topic',
'availability_topic': 'avty-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})
state = hass.states.get('sensor.beer')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('avty-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.reset_mock()
registry.async_update_entity('sensor.beer', new_entity_id='sensor.milk')
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('sensor.beer')
assert state is None
state = hass.states.get('sensor.milk')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('avty-topic', ANY, 0, 'utf-8')