Improve rflink coverage (#19596)

* some minor tests refactor
* async/await refactor
* toggle have not brightness
* test for race condition in unknown device
* test for 'no_command' and 'not_connected'
* test for race condition in unknown device
* sensor events are handled in sensor devices, RflinkDevice handle
command events
* test race conditions & bogus entity remove
* two more tests
* Test race condition for unknown components
* Test cleanup for `commands events` and `sensor events`
This commit is contained in:
javicalle 2019-01-01 15:35:31 +01:00 committed by emontnemery
parent 2ea53e0787
commit b9f4a7220e
4 changed files with 250 additions and 11 deletions

View file

@ -6,7 +6,8 @@ automatic sensor creation.
"""
from homeassistant.components.rflink import (
CONF_RECONNECT_INTERVAL)
CONF_RECONNECT_INTERVAL, TMP_ENTITY, DATA_ENTITY_LOOKUP,
EVENT_KEY_COMMAND, EVENT_KEY_SENSOR)
from homeassistant.const import STATE_UNKNOWN
from ..test_rflink import mock_rflink
@ -133,3 +134,111 @@ async def test_entity_availability(hass, monkeypatch):
# Entities should be available again
assert hass.states.get('sensor.test').state == STATE_UNKNOWN
async def test_aliasses(hass, monkeypatch):
"""Validate the response to sensor's alias (with aliasses)."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'test_02': {
'name': 'test_02',
'sensor_type': 'humidity',
'aliasses': ['test_alias_02_0'],
},
},
},
}
# setup mocking rflink module
event_callback, _, _, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
# test default state of sensor loaded from config
config_sensor = hass.states.get('sensor.test_02')
assert config_sensor
assert config_sensor.state == 'unknown'
# test event for config sensor
event_callback({
'id': 'test_alias_02_0',
'sensor': 'humidity',
'value': 65,
'unit': '%',
})
await hass.async_block_till_done()
# test state of new sensor
updated_sensor = hass.states.get('sensor.test_02')
assert updated_sensor
assert updated_sensor.state == '65'
assert updated_sensor.attributes['unit_of_measurement'] == '%'
async def test_race_condition(hass, monkeypatch):
"""Test race condition for unknown components."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
},
}
tmp_entity = TMP_ENTITY.format('test3')
# setup mocking rflink module
event_callback, _, _, _ = await mock_rflink(
hass, config, DOMAIN, monkeypatch)
# test event for new unconfigured sensor
event_callback({
'id': 'test3',
'sensor': 'battery',
'value': 'ok',
'unit': '',
})
event_callback({
'id': 'test3',
'sensor': 'battery',
'value': 'ko',
'unit': '',
})
# tmp_entity added to EVENT_KEY_SENSOR
assert tmp_entity in hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_SENSOR]['test3']
# tmp_entity must no be added to EVENT_KEY_COMMAND
assert tmp_entity not in hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_COMMAND]['test3']
await hass.async_block_till_done()
# test state of new sensor
updated_sensor = hass.states.get('sensor.test3')
assert updated_sensor
# test state of new sensor
new_sensor = hass.states.get(DOMAIN+'.test3')
assert new_sensor
assert new_sensor.state == 'ok'
event_callback({
'id': 'test3',
'sensor': 'battery',
'value': 'ko',
'unit': '',
})
await hass.async_block_till_done()
# tmp_entity must be deleted from EVENT_KEY_COMMAND
assert tmp_entity not in hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_SENSOR]['test3']
# test state of new sensor
new_sensor = hass.states.get(DOMAIN+'.test3')
assert new_sensor
assert new_sensor.state == 'ko'