2017-02-25 20:55:01 +00:00
|
|
|
"""The tests for the MQTT discovery."""
|
2017-02-07 17:13:24 +00:00
|
|
|
import asyncio
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from homeassistant.components.mqtt.discovery import async_start
|
|
|
|
|
|
|
|
from tests.common import async_fire_mqtt_message, mock_coro
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_subscribing_config_topic(hass, mqtt_mock):
|
|
|
|
"""Test setting up discovery."""
|
|
|
|
hass_config = {}
|
|
|
|
discovery_topic = 'homeassistant'
|
2017-02-18 22:17:18 +00:00
|
|
|
yield from async_start(hass, discovery_topic, hass_config)
|
|
|
|
|
|
|
|
assert mqtt_mock.async_subscribe.called
|
|
|
|
call_args = mqtt_mock.async_subscribe.mock_calls[0][1]
|
2017-02-07 17:13:24 +00:00
|
|
|
assert call_args[0] == discovery_topic + '/#'
|
|
|
|
assert call_args[1] == 0
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
@patch('homeassistant.components.mqtt.discovery.async_load_platform')
|
|
|
|
def test_invalid_topic(mock_load_platform, hass, mqtt_mock):
|
2017-02-25 20:55:01 +00:00
|
|
|
"""Test sending to invalid topic."""
|
2017-02-07 17:13:24 +00:00
|
|
|
mock_load_platform.return_value = mock_coro()
|
2017-02-18 22:17:18 +00:00
|
|
|
yield from async_start(hass, 'homeassistant', {})
|
2017-02-07 17:13:24 +00:00
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/not_config',
|
|
|
|
'{}')
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
assert not mock_load_platform.called
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
@patch('homeassistant.components.mqtt.discovery.async_load_platform')
|
|
|
|
def test_invalid_json(mock_load_platform, hass, mqtt_mock, caplog):
|
|
|
|
"""Test sending in invalid JSON."""
|
|
|
|
mock_load_platform.return_value = mock_coro()
|
2017-02-18 22:17:18 +00:00
|
|
|
yield from async_start(hass, 'homeassistant', {})
|
2017-02-07 17:13:24 +00:00
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
|
|
|
|
'not json')
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
assert 'Unable to parse JSON' in caplog.text
|
|
|
|
assert not mock_load_platform.called
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
@patch('homeassistant.components.mqtt.discovery.async_load_platform')
|
|
|
|
def test_only_valid_components(mock_load_platform, hass, mqtt_mock, caplog):
|
2017-02-25 20:55:01 +00:00
|
|
|
"""Test for a valid component."""
|
2017-02-07 17:13:24 +00:00
|
|
|
mock_load_platform.return_value = mock_coro()
|
2017-02-18 22:17:18 +00:00
|
|
|
yield from async_start(hass, 'homeassistant', {})
|
2017-02-07 17:13:24 +00:00
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config', '{}')
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
assert 'Component climate is not supported' in caplog.text
|
|
|
|
assert not mock_load_platform.called
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_correct_config_discovery(hass, mqtt_mock, caplog):
|
2017-02-25 20:55:01 +00:00
|
|
|
"""Test sending in correct JSON."""
|
2017-02-18 22:17:18 +00:00
|
|
|
yield from async_start(hass, 'homeassistant', {})
|
2017-02-07 17:13:24 +00:00
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
|
|
|
|
'{ "name": "Beer" }')
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get('binary_sensor.beer')
|
|
|
|
|
|
|
|
assert state is not None
|
|
|
|
assert state.name == 'Beer'
|