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 authored and Paulus Schoutsen committed 2018-12-19 14:05:24 +01:00
1 parent 8cec559103
commit 1568de62df
20 files changed
+333 -29

No files matched your search

+39 -3
View File
@@ -155,7 +155,7 @@ light:
"""
import json
from unittest import mock
from unittest.mock import patch
from unittest.mock import ANY, patch
from homeassistant.setup import async_setup_component
from homeassistant.const import (
@@ -165,8 +165,8 @@ from homeassistant.components.mqtt.discovery import async_start
import homeassistant.core as ha
from tests.common import (
assert_setup_component, async_fire_mqtt_message,
async_mock_mqtt_component, mock_coro, MockConfigEntry)
assert_setup_component, async_fire_mqtt_message, async_mock_mqtt_component,
mock_coro, MockConfigEntry, mock_registry)
from tests.components.light import common
@@ -1180,3 +1180,39 @@ 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, light.DOMAIN, {
light.DOMAIN: [{
'platform': 'mqtt',
'name': 'beer',
'state_topic': 'test-topic',
'command_topic': 'command-topic',
'availability_topic': 'avty-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})
state = hass.states.get('light.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('light.beer', new_entity_id='light.milk')
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('light.beer')
assert state is None
state = hass.states.get('light.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')