Reconfigure MQTT binary_sensor component if discovery info is changed (#18169)

* Recreate component if discovery info is changed

* Update component instead of remove+add

* Set name and unique_id in __init__

* Update unit test

* Cleanup

* More cleanup

* Refactor according to review comments

* Change discovery_hash

* Review comments, add tests

* Fix handling of value_template
This commit is contained in:
emontnemery authored and Paulus Schoutsen committed 2018-11-19 16:49:04 +01:00
1 parent 01953ab46b
commit de9bac9ee3
8 files changed
+431 -66

No files matched your search

+67 -1
View File
@@ -284,7 +284,8 @@ async def test_discovery_removal_binary_sensor(hass, mqtt_mock, caplog):
await async_start(hass, 'homeassistant', {}, entry)
data = (
'{ "name": "Beer",'
' "status_topic": "test_topic" }'
' "state_topic": "test_topic",'
' "availability_topic": "availability_topic" }'
)
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data)
@@ -300,6 +301,71 @@ async def test_discovery_removal_binary_sensor(hass, mqtt_mock, caplog):
assert state is None
async def test_discovery_update_binary_sensor(hass, mqtt_mock, caplog):
"""Test removal of discovered binary_sensor."""
entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, 'homeassistant', {}, entry)
data1 = (
'{ "name": "Beer",'
' "state_topic": "test_topic",'
' "availability_topic": "availability_topic1" }'
)
data2 = (
'{ "name": "Milk",'
' "state_topic": "test_topic2",'
' "availability_topic": "availability_topic2" }'
)
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data1)
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.beer')
assert state is not None
assert state.name == 'Beer'
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data2)
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.beer')
assert state is not None
assert state.name == 'Milk'
state = hass.states.get('binary_sensor.milk')
assert state is None
async def test_discovery_unique_id(hass, mqtt_mock, caplog):
"""Test unique id option only creates one sensor per unique_id."""
entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, 'homeassistant', {}, entry)
data1 = (
'{ "name": "Beer",'
' "state_topic": "test_topic",'
' "unique_id": "TOTALLY_UNIQUE" }'
)
data2 = (
'{ "name": "Milk",'
' "state_topic": "test_topic",'
' "unique_id": "TOTALLY_DIFFERENT" }'
)
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data1)
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.beer')
assert state is not None
assert state.name == 'Beer'
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data2)
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.beer')
assert state is not None
assert state.name == 'Beer'
state = hass.states.get('binary_sensor.milk')
assert state is not None
assert state.name == 'Milk'
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
"""Test MQTT binary sensor device registry integration."""
entry = MockConfigEntry(domain=mqtt.DOMAIN)
+1 -1
View File
@@ -185,7 +185,7 @@ def test_discovery_incl_nodeid(hass, mqtt_mock, caplog):
assert state is not None
assert state.name == 'Beer'
assert ('binary_sensor', 'my_node_id_bla') in hass.data[ALREADY_DISCOVERED]
assert ('binary_sensor', 'my_node_id bla') in hass.data[ALREADY_DISCOVERED]
@asyncio.coroutine
+180
View File
@@ -0,0 +1,180 @@
"""The tests for the MQTT subscription component."""
from homeassistant.core import callback
from homeassistant.components.mqtt.subscription import (
async_subscribe_topics, async_unsubscribe_topics)
from tests.common import async_fire_mqtt_message, async_mock_mqtt_component
async def test_subscribe_topics(hass, mqtt_mock, caplog):
"""Test subscription to topics."""
calls1 = []
@callback
def record_calls1(*args):
"""Record calls."""
calls1.append(args)
calls2 = []
@callback
def record_calls2(*args):
"""Record calls."""
calls2.append(args)
sub_state = None
sub_state = await async_subscribe_topics(
hass, sub_state,
{'test_topic1': {'topic': 'test-topic1',
'msg_callback': record_calls1},
'test_topic2': {'topic': 'test-topic2',
'msg_callback': record_calls2}})
async_fire_mqtt_message(hass, 'test-topic1', 'test-payload1')
await hass.async_block_till_done()
assert 1 == len(calls1)
assert 'test-topic1' == calls1[0][0]
assert 'test-payload1' == calls1[0][1]
assert 0 == len(calls2)
async_fire_mqtt_message(hass, 'test-topic2', 'test-payload2')
await hass.async_block_till_done()
await hass.async_block_till_done()
assert 1 == len(calls1)
assert 1 == len(calls2)
assert 'test-topic2' == calls2[0][0]
assert 'test-payload2' == calls2[0][1]
await async_unsubscribe_topics(hass, sub_state)
async_fire_mqtt_message(hass, 'test-topic1', 'test-payload')
async_fire_mqtt_message(hass, 'test-topic2', 'test-payload')
await hass.async_block_till_done()
assert 1 == len(calls1)
assert 1 == len(calls2)
async def test_modify_topics(hass, mqtt_mock, caplog):
"""Test modification of topics."""
calls1 = []
@callback
def record_calls1(*args):
"""Record calls."""
calls1.append(args)
calls2 = []
@callback
def record_calls2(*args):
"""Record calls."""
calls2.append(args)
sub_state = None
sub_state = await async_subscribe_topics(
hass, sub_state,
{'test_topic1': {'topic': 'test-topic1',
'msg_callback': record_calls1},
'test_topic2': {'topic': 'test-topic2',
'msg_callback': record_calls2}})
async_fire_mqtt_message(hass, 'test-topic1', 'test-payload')
await hass.async_block_till_done()
assert 1 == len(calls1)
assert 0 == len(calls2)
async_fire_mqtt_message(hass, 'test-topic2', 'test-payload')
await hass.async_block_till_done()
await hass.async_block_till_done()
assert 1 == len(calls1)
assert 1 == len(calls2)
sub_state = await async_subscribe_topics(
hass, sub_state,
{'test_topic1': {'topic': 'test-topic1_1',
'msg_callback': record_calls1}})
async_fire_mqtt_message(hass, 'test-topic1', 'test-payload')
async_fire_mqtt_message(hass, 'test-topic2', 'test-payload')
await hass.async_block_till_done()
await hass.async_block_till_done()
assert 1 == len(calls1)
assert 1 == len(calls2)
async_fire_mqtt_message(hass, 'test-topic1_1', 'test-payload')
await hass.async_block_till_done()
await hass.async_block_till_done()
assert 2 == len(calls1)
assert 'test-topic1_1' == calls1[1][0]
assert 'test-payload' == calls1[1][1]
assert 1 == len(calls2)
await async_unsubscribe_topics(hass, sub_state)
async_fire_mqtt_message(hass, 'test-topic1_1', 'test-payload')
async_fire_mqtt_message(hass, 'test-topic2', 'test-payload')
await hass.async_block_till_done()
assert 2 == len(calls1)
assert 1 == len(calls2)
async def test_qos_encoding_default(hass, mqtt_mock, caplog):
"""Test default qos and encoding."""
mock_mqtt = await async_mock_mqtt_component(hass)
@callback
def msg_callback(*args):
"""Do nothing."""
pass
sub_state = None
sub_state = await async_subscribe_topics(
hass, sub_state,
{'test_topic1': {'topic': 'test-topic1',
'msg_callback': msg_callback}})
mock_mqtt.async_subscribe.assert_called_once_with(
'test-topic1', msg_callback, 0, 'utf-8')
async def test_qos_encoding_custom(hass, mqtt_mock, caplog):
"""Test custom qos and encoding."""
mock_mqtt = await async_mock_mqtt_component(hass)
@callback
def msg_callback(*args):
"""Do nothing."""
pass
sub_state = None
sub_state = await async_subscribe_topics(
hass, sub_state,
{'test_topic1': {'topic': 'test-topic1',
'msg_callback': msg_callback,
'qos': 1,
'encoding': 'utf-16'}})
mock_mqtt.async_subscribe.assert_called_once_with(
'test-topic1', msg_callback, 1, 'utf-16')
async def test_no_change(hass, mqtt_mock, caplog):
"""Test subscription to topics without change."""
mock_mqtt = await async_mock_mqtt_component(hass)
@callback
def msg_callback(*args):
"""Do nothing."""
pass
sub_state = None
sub_state = await async_subscribe_topics(
hass, sub_state,
{'test_topic1': {'topic': 'test-topic1',
'msg_callback': msg_callback}})
call_count = mock_mqtt.async_subscribe.call_count
sub_state = await async_subscribe_topics(
hass, sub_state,
{'test_topic1': {'topic': 'test-topic1',
'msg_callback': msg_callback}})
assert call_count == mock_mqtt.async_subscribe.call_count