Make MQTT platforms config entries (#16904)

* Make MQTT platforms config entries

* Fix tests

* Address Comment

* Rework tests

* Undo style auto-reformat style changes
This commit is contained in:
Otto Winter authored and Paulus Schoutsen committed 2018-09-27 16:07:56 +02:00
1 parent 8d65230a36
commit d1ad2cc225
4 files changed
+87 -17

No files matched your search

+38 -11
View File
@@ -2,18 +2,23 @@
import asyncio
from unittest.mock import patch
from homeassistant.components import mqtt
from homeassistant.components.mqtt.discovery import async_start, \
ALREADY_DISCOVERED
from tests.common import async_fire_mqtt_message, mock_coro
from tests.common import async_fire_mqtt_message, mock_coro, MockConfigEntry
@asyncio.coroutine
def test_subscribing_config_topic(hass, mqtt_mock):
"""Test setting up discovery."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={
mqtt.CONF_BROKER: 'test-broker'
})
hass_config = {}
discovery_topic = 'homeassistant'
yield from async_start(hass, discovery_topic, hass_config)
yield from async_start(hass, discovery_topic, hass_config, entry)
assert mqtt_mock.async_subscribe.called
call_args = mqtt_mock.async_subscribe.mock_calls[0][1]
@@ -25,8 +30,12 @@ def test_subscribing_config_topic(hass, mqtt_mock):
@asyncio.coroutine
def test_invalid_topic(mock_load_platform, hass, mqtt_mock):
"""Test sending to invalid topic."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={
mqtt.CONF_BROKER: 'test-broker'
})
mock_load_platform.return_value = mock_coro()
yield from async_start(hass, 'homeassistant', {})
yield from async_start(hass, 'homeassistant', {}, entry)
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/not_config',
'{}')
@@ -38,8 +47,12 @@ def test_invalid_topic(mock_load_platform, hass, mqtt_mock):
@asyncio.coroutine
def test_invalid_json(mock_load_platform, hass, mqtt_mock, caplog):
"""Test sending in invalid JSON."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={
mqtt.CONF_BROKER: 'test-broker'
})
mock_load_platform.return_value = mock_coro()
yield from async_start(hass, 'homeassistant', {})
yield from async_start(hass, 'homeassistant', {}, entry)
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
'not json')
@@ -52,10 +65,12 @@ def test_invalid_json(mock_load_platform, hass, mqtt_mock, caplog):
@asyncio.coroutine
def test_only_valid_components(mock_load_platform, hass, mqtt_mock, caplog):
"""Test for a valid component."""
entry = MockConfigEntry(domain=mqtt.DOMAIN)
invalid_component = "timer"
mock_load_platform.return_value = mock_coro()
yield from async_start(hass, 'homeassistant', {})
yield from async_start(hass, 'homeassistant', {}, entry)
async_fire_mqtt_message(hass, 'homeassistant/{}/bla/config'.format(
invalid_component
@@ -73,7 +88,9 @@ def test_only_valid_components(mock_load_platform, hass, mqtt_mock, caplog):
@asyncio.coroutine
def test_correct_config_discovery(hass, mqtt_mock, caplog):
"""Test sending in correct JSON."""
yield from async_start(hass, 'homeassistant', {})
entry = MockConfigEntry(domain=mqtt.DOMAIN)
yield from async_start(hass, 'homeassistant', {}, entry)
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
'{ "name": "Beer" }')
@@ -89,7 +106,9 @@ def test_correct_config_discovery(hass, mqtt_mock, caplog):
@asyncio.coroutine
def test_discover_fan(hass, mqtt_mock, caplog):
"""Test discovering an MQTT fan."""
yield from async_start(hass, 'homeassistant', {})
entry = MockConfigEntry(domain=mqtt.DOMAIN)
yield from async_start(hass, 'homeassistant', {}, entry)
async_fire_mqtt_message(hass, 'homeassistant/fan/bla/config',
('{ "name": "Beer",'
@@ -106,7 +125,9 @@ def test_discover_fan(hass, mqtt_mock, caplog):
@asyncio.coroutine
def test_discover_climate(hass, mqtt_mock, caplog):
"""Test discovering an MQTT climate component."""
yield from async_start(hass, 'homeassistant', {})
entry = MockConfigEntry(domain=mqtt.DOMAIN)
yield from async_start(hass, 'homeassistant', {}, entry)
data = (
'{ "name": "ClimateTest",'
@@ -127,7 +148,9 @@ def test_discover_climate(hass, mqtt_mock, caplog):
@asyncio.coroutine
def test_discover_alarm_control_panel(hass, mqtt_mock, caplog):
"""Test discovering an MQTT alarm control panel component."""
yield from async_start(hass, 'homeassistant', {})
entry = MockConfigEntry(domain=mqtt.DOMAIN)
yield from async_start(hass, 'homeassistant', {}, entry)
data = (
'{ "name": "AlarmControlPanelTest",'
@@ -149,7 +172,9 @@ def test_discover_alarm_control_panel(hass, mqtt_mock, caplog):
@asyncio.coroutine
def test_discovery_incl_nodeid(hass, mqtt_mock, caplog):
"""Test sending in correct JSON with optional node_id included."""
yield from async_start(hass, 'homeassistant', {})
entry = MockConfigEntry(domain=mqtt.DOMAIN)
yield from async_start(hass, 'homeassistant', {}, entry)
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/my_node_id/bla'
'/config', '{ "name": "Beer" }')
@@ -165,7 +190,9 @@ def test_discovery_incl_nodeid(hass, mqtt_mock, caplog):
@asyncio.coroutine
def test_non_duplicate_discovery(hass, mqtt_mock, caplog):
"""Test for a non duplicate component."""
yield from async_start(hass, 'homeassistant', {})
entry = MockConfigEntry(domain=mqtt.DOMAIN)
yield from async_start(hass, 'homeassistant', {}, entry)
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
'{ "name": "Beer" }')
+4 -2
View File
@@ -6,6 +6,7 @@ from unittest.mock import patch
import homeassistant.core as ha
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.components import mqtt
from homeassistant.components.mqtt.discovery import async_start
import homeassistant.components.sensor as sensor
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE
@@ -13,7 +14,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
async_mock_mqtt_component, MockConfigEntry
from tests.common import get_test_home_assistant, mock_component
@@ -392,7 +393,8 @@ async def test_unique_id(hass):
async def test_discovery_removal_sensor(hass, mqtt_mock, caplog):
"""Test removal of discovered sensor."""
await async_start(hass, 'homeassistant', {})
entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, 'homeassistant', {}, entry)
data = (
'{ "name": "Beer",'
' "status_topic": "test_topic" }'