Async MQTT sensor room (#17765)

This commit is contained in:
kennedyshead 2018-10-24 22:20:52 +02:00 committed by Paulus Schoutsen
parent c099c259ea
commit ec7d33f277

View File

@ -1,18 +1,16 @@
"""The tests for the MQTT room presence sensor.""" """The tests for the MQTT room presence sensor."""
import json import json
import datetime import datetime
import unittest
from unittest.mock import patch from unittest.mock import patch
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
import homeassistant.components.sensor as sensor import homeassistant.components.sensor as sensor
from homeassistant.components.mqtt import (CONF_STATE_TOPIC, CONF_QOS, from homeassistant.components.mqtt import (CONF_STATE_TOPIC, CONF_QOS,
DEFAULT_QOS) DEFAULT_QOS)
from homeassistant.const import (CONF_NAME, CONF_PLATFORM) from homeassistant.const import (CONF_NAME, CONF_PLATFORM)
from homeassistant.util import dt from homeassistant.util import dt
from tests.common import ( from tests.common import async_fire_mqtt_message
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message)
DEVICE_ID = '123TESTMAC' DEVICE_ID = '123TESTMAC'
NAME = 'test_device' NAME = 'test_device'
@ -46,14 +44,29 @@ REALLY_FAR_MESSAGE = {
} }
class TestMQTTRoomSensor(unittest.TestCase): async def send_message(hass, topic, message):
"""Test the room presence sensor.""" """Test the sending of a message."""
async_fire_mqtt_message(
hass, topic, json.dumps(message))
await hass.async_block_till_done()
await hass.async_block_till_done()
def setup_method(self, method):
"""Set up things to be run when tests are started.""" async def assert_state(hass, room):
self.hass = get_test_home_assistant() """Test the assertion of a room state."""
mock_mqtt_component(self.hass) state = hass.states.get(SENSOR_STATE)
assert setup_component(self.hass, sensor.DOMAIN, { assert state.state == room
async def assert_distance(hass, distance):
"""Test the assertion of a distance state."""
state = hass.states.get(SENSOR_STATE)
assert state.attributes.get('distance') == distance
async def test_room_update(hass, mqtt_mock):
"""Test the updating between rooms."""
assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: { sensor.DOMAIN: {
CONF_PLATFORM: 'mqtt_room', CONF_PLATFORM: 'mqtt_room',
CONF_NAME: NAME, CONF_NAME: NAME,
@ -63,46 +76,21 @@ class TestMQTTRoomSensor(unittest.TestCase):
CONF_TIMEOUT: 5 CONF_TIMEOUT: 5
}}) }})
# Clear state between tests await send_message(hass, BEDROOM_TOPIC, FAR_MESSAGE)
self.hass.states.set(SENSOR_STATE, None) await assert_state(hass, BEDROOM)
await assert_distance(hass, 10)
def teardown_method(self, method): await send_message(hass, LIVING_ROOM_TOPIC, NEAR_MESSAGE)
"""Stop everything that was started.""" await assert_state(hass, LIVING_ROOM)
self.hass.stop() await assert_distance(hass, 1)
def send_message(self, topic, message): await send_message(hass, BEDROOM_TOPIC, FAR_MESSAGE)
"""Test the sending of a message.""" await assert_state(hass, LIVING_ROOM)
fire_mqtt_message( await assert_distance(hass, 1)
self.hass, topic, json.dumps(message))
self.hass.block_till_done()
def assert_state(self, room):
"""Test the assertion of a room state."""
state = self.hass.states.get(SENSOR_STATE)
assert state.state == room
def assert_distance(self, distance):
"""Test the assertion of a distance state."""
state = self.hass.states.get(SENSOR_STATE)
assert state.attributes.get('distance') == distance
def test_room_update(self):
"""Test the updating between rooms."""
self.send_message(BEDROOM_TOPIC, FAR_MESSAGE)
self.assert_state(BEDROOM)
self.assert_distance(10)
self.send_message(LIVING_ROOM_TOPIC, NEAR_MESSAGE)
self.assert_state(LIVING_ROOM)
self.assert_distance(1)
self.send_message(BEDROOM_TOPIC, FAR_MESSAGE)
self.assert_state(LIVING_ROOM)
self.assert_distance(1)
time = dt.utcnow() + datetime.timedelta(seconds=7) time = dt.utcnow() + datetime.timedelta(seconds=7)
with patch('homeassistant.helpers.condition.dt_util.utcnow', with patch('homeassistant.helpers.condition.dt_util.utcnow',
return_value=time): return_value=time):
self.send_message(BEDROOM_TOPIC, FAR_MESSAGE) await send_message(hass, BEDROOM_TOPIC, FAR_MESSAGE)
self.assert_state(BEDROOM) await assert_state(hass, BEDROOM)
self.assert_distance(10) await assert_distance(hass, 10)