Fix MQTT leaving files behind (#16840)

This commit is contained in:
Paulus Schoutsen 2018-09-25 12:22:27 +02:00 committed by GitHub
parent e4898bb05c
commit 7840b1e387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 45 deletions

View File

@ -2,14 +2,15 @@
import unittest import unittest
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.setup import setup_component from homeassistant.setup import setup_component, async_setup_component
import homeassistant.components.binary_sensor as binary_sensor import homeassistant.components.binary_sensor as binary_sensor
from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE
from tests.common import get_test_home_assistant, fire_mqtt_message from tests.common import (
from tests.common import mock_component, mock_mqtt_component get_test_home_assistant, fire_mqtt_message, async_fire_mqtt_message,
mock_component, mock_mqtt_component, async_mock_mqtt_component)
class TestSensorMQTT(unittest.TestCase): class TestSensorMQTT(unittest.TestCase):
@ -77,25 +78,6 @@ class TestSensorMQTT(unittest.TestCase):
state = self.hass.states.get('binary_sensor.test') state = self.hass.states.get('binary_sensor.test')
self.assertIsNone(state) self.assertIsNone(state)
def test_unique_id(self):
"""Test unique id option only creates one sensor per unique_id."""
assert setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: [{
'platform': 'mqtt',
'name': 'Test 1',
'state_topic': 'test-topic',
'unique_id': 'TOTALLY_UNIQUE'
}, {
'platform': 'mqtt',
'name': 'Test 2',
'state_topic': 'test-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})
fire_mqtt_message(self.hass, 'test-topic', 'payload')
self.hass.block_till_done()
assert len(self.hass.states.all()) == 1
def test_availability_without_topic(self): def test_availability_without_topic(self):
"""Test availability without defined availability topic.""" """Test availability without defined availability topic."""
self.assertTrue(setup_component(self.hass, binary_sensor.DOMAIN, { self.assertTrue(setup_component(self.hass, binary_sensor.DOMAIN, {
@ -223,3 +205,24 @@ class TestSensorMQTT(unittest.TestCase):
fire_mqtt_message(self.hass, 'test-topic', 'ON') fire_mqtt_message(self.hass, 'test-topic', 'ON')
self.hass.block_till_done() self.hass.block_till_done()
self.assertEqual(2, len(events)) self.assertEqual(2, len(events))
async def test_unique_id(hass):
"""Test unique id option only creates one sensor per unique_id."""
await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: [{
'platform': 'mqtt',
'name': 'Test 1',
'state_topic': 'test-topic',
'unique_id': 'TOTALLY_UNIQUE'
}, {
'platform': 'mqtt',
'name': 'Test 2',
'state_topic': 'test-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})
async_fire_mqtt_message(hass, 'test-topic', 'payload')
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 1

View File

@ -2,13 +2,14 @@
import unittest import unittest
from unittest.mock import patch from unittest.mock import patch
from homeassistant.setup import setup_component from homeassistant.setup import setup_component, async_setup_component
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE,\ from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE,\
ATTR_ASSUMED_STATE ATTR_ASSUMED_STATE
import homeassistant.core as ha import homeassistant.core as ha
import homeassistant.components.switch as switch import homeassistant.components.switch as switch
from tests.common import ( from tests.common import (
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, mock_coro) mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, mock_coro,
async_mock_mqtt_component, async_fire_mqtt_message)
class TestSwitchMQTT(unittest.TestCase): class TestSwitchMQTT(unittest.TestCase):
@ -280,25 +281,28 @@ class TestSwitchMQTT(unittest.TestCase):
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
def test_unique_id(self):
"""Test unique id option only creates one switch per unique_id."""
assert setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: [{
'platform': 'mqtt',
'name': 'Test 1',
'state_topic': 'test-topic',
'command_topic': 'command-topic',
'unique_id': 'TOTALLY_UNIQUE'
}, {
'platform': 'mqtt',
'name': 'Test 2',
'state_topic': 'test-topic',
'command_topic': 'command-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})
fire_mqtt_message(self.hass, 'test-topic', 'payload') async def test_unique_id(hass):
self.hass.block_till_done() """Test unique id option only creates one switch per unique_id."""
assert len(self.hass.states.async_entity_ids()) == 2 await async_mock_mqtt_component(hass)
# all switches group is 1, unique id created is 1 assert await async_setup_component(hass, switch.DOMAIN, {
switch.DOMAIN: [{
'platform': 'mqtt',
'name': 'Test 1',
'state_topic': 'test-topic',
'command_topic': 'command-topic',
'unique_id': 'TOTALLY_UNIQUE'
}, {
'platform': 'mqtt',
'name': 'Test 2',
'state_topic': 'test-topic',
'command_topic': 'command-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})
async_fire_mqtt_message(hass, 'test-topic', 'payload')
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids()) == 2
# all switches group is 1, unique id created is 1