mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Async MQTT sensor room (#17765)
This commit is contained in:
parent
c099c259ea
commit
ec7d33f277
@ -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,63 +44,53 @@ 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."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
mock_mqtt_component(self.hass)
|
|
||||||
assert setup_component(self.hass, sensor.DOMAIN, {
|
|
||||||
sensor.DOMAIN: {
|
|
||||||
CONF_PLATFORM: 'mqtt_room',
|
|
||||||
CONF_NAME: NAME,
|
|
||||||
CONF_DEVICE_ID: DEVICE_ID,
|
|
||||||
CONF_STATE_TOPIC: 'room_presence',
|
|
||||||
CONF_QOS: DEFAULT_QOS,
|
|
||||||
CONF_TIMEOUT: 5
|
|
||||||
}})
|
|
||||||
|
|
||||||
# Clear state between tests
|
async def assert_state(hass, room):
|
||||||
self.hass.states.set(SENSOR_STATE, None)
|
"""Test the assertion of a room state."""
|
||||||
|
state = hass.states.get(SENSOR_STATE)
|
||||||
|
assert state.state == room
|
||||||
|
|
||||||
def teardown_method(self, method):
|
|
||||||
"""Stop everything that was started."""
|
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
def send_message(self, topic, message):
|
async def assert_distance(hass, distance):
|
||||||
"""Test the sending of a message."""
|
"""Test the assertion of a distance state."""
|
||||||
fire_mqtt_message(
|
state = hass.states.get(SENSOR_STATE)
|
||||||
self.hass, topic, json.dumps(message))
|
assert state.attributes.get('distance') == distance
|
||||||
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):
|
async def test_room_update(hass, mqtt_mock):
|
||||||
"""Test the assertion of a distance state."""
|
"""Test the updating between rooms."""
|
||||||
state = self.hass.states.get(SENSOR_STATE)
|
assert await async_setup_component(hass, sensor.DOMAIN, {
|
||||||
assert state.attributes.get('distance') == distance
|
sensor.DOMAIN: {
|
||||||
|
CONF_PLATFORM: 'mqtt_room',
|
||||||
|
CONF_NAME: NAME,
|
||||||
|
CONF_DEVICE_ID: DEVICE_ID,
|
||||||
|
CONF_STATE_TOPIC: 'room_presence',
|
||||||
|
CONF_QOS: DEFAULT_QOS,
|
||||||
|
CONF_TIMEOUT: 5
|
||||||
|
}})
|
||||||
|
|
||||||
def test_room_update(self):
|
await send_message(hass, BEDROOM_TOPIC, FAR_MESSAGE)
|
||||||
"""Test the updating between rooms."""
|
await assert_state(hass, BEDROOM)
|
||||||
self.send_message(BEDROOM_TOPIC, FAR_MESSAGE)
|
await assert_distance(hass, 10)
|
||||||
self.assert_state(BEDROOM)
|
|
||||||
self.assert_distance(10)
|
|
||||||
|
|
||||||
self.send_message(LIVING_ROOM_TOPIC, NEAR_MESSAGE)
|
await send_message(hass, LIVING_ROOM_TOPIC, NEAR_MESSAGE)
|
||||||
self.assert_state(LIVING_ROOM)
|
await assert_state(hass, LIVING_ROOM)
|
||||||
self.assert_distance(1)
|
await assert_distance(hass, 1)
|
||||||
|
|
||||||
self.send_message(BEDROOM_TOPIC, FAR_MESSAGE)
|
await send_message(hass, BEDROOM_TOPIC, FAR_MESSAGE)
|
||||||
self.assert_state(LIVING_ROOM)
|
await assert_state(hass, LIVING_ROOM)
|
||||||
self.assert_distance(1)
|
await assert_distance(hass, 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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user