mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
MQTT discovery (#5724)
* Change implementation * Re-write * Remove unused consts * Update discovery.py * Add tests * fix other tests * Fix check_config script test * Lint * Lint
This commit is contained in:
committed by
Paulus Schoutsen
parent
45507cd9d1
commit
c7fd28c10f
74
tests/components/mqtt/test_discovery.py
Normal file
74
tests/components/mqtt/test_discovery.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""The tests for the MQTT component."""
|
||||
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'
|
||||
async_start(hass, discovery_topic, hass_config)
|
||||
assert mqtt_mock.subscribe.called
|
||||
call_args = mqtt_mock.subscribe.mock_calls[0][1]
|
||||
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):
|
||||
"""Test sending in invalid JSON."""
|
||||
mock_load_platform.return_value = mock_coro()
|
||||
async_start(hass, 'homeassistant', {})
|
||||
|
||||
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()
|
||||
async_start(hass, 'homeassistant', {})
|
||||
|
||||
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):
|
||||
"""Test sending in invalid JSON."""
|
||||
mock_load_platform.return_value = mock_coro()
|
||||
async_start(hass, 'homeassistant', {})
|
||||
|
||||
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):
|
||||
"""Test sending in invalid JSON."""
|
||||
async_start(hass, 'homeassistant', {})
|
||||
|
||||
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'
|
||||
Reference in New Issue
Block a user