Use websocket fixture in deCONZ fan tests (#47824)

Localize test data
Improve asserts
This commit is contained in:
Robert Svensson 2021-03-18 10:49:48 +01:00 committed by GitHub
parent 7ff9610e67
commit 7350215b4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,9 @@
"""deCONZ fan platform tests.""" """deCONZ fan platform tests."""
from copy import deepcopy from unittest.mock import patch
import pytest import pytest
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
from homeassistant.components.fan import ( from homeassistant.components.fan import (
ATTR_SPEED, ATTR_SPEED,
DOMAIN as FAN_DOMAIN, DOMAIN as FAN_DOMAIN,
@ -24,7 +23,17 @@ from .test_gateway import (
setup_deconz_integration, setup_deconz_integration,
) )
FANS = {
async def test_no_fans(hass, aioclient_mock):
"""Test that no fan entities are created."""
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
async def test_fans(hass, aioclient_mock, mock_deconz_websocket):
"""Test that all supported fan entities are created."""
data = {
"lights": {
"1": { "1": {
"etag": "432f3de28965052961a99e3c5494daf4", "etag": "432f3de28965052961a99e3c5494daf4",
"hascolor": False, "hascolor": False,
@ -43,43 +52,29 @@ FANS = {
"uniqueid": "00:22:a3:00:00:27:8b:81-01", "uniqueid": "00:22:a3:00:00:27:8b:81-01",
} }
} }
}
with patch.dict(DECONZ_WEB_REQUEST, data):
async def test_no_fans(hass, aioclient_mock): config_entry = await setup_deconz_integration(hass, aioclient_mock)
"""Test that no fan entities are created."""
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
async def test_fans(hass, aioclient_mock):
"""Test that all supported fan entities are created."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["lights"] = deepcopy(FANS)
config_entry = await setup_deconz_integration(
hass, aioclient_mock, get_state_response=data
)
gateway = get_gateway_from_config_entry(hass, config_entry)
assert len(hass.states.async_all()) == 2 # Light and fan assert len(hass.states.async_all()) == 2 # Light and fan
assert hass.states.get("fan.ceiling_fan") assert hass.states.get("fan.ceiling_fan").state == STATE_ON
assert hass.states.get("fan.ceiling_fan").attributes[ATTR_SPEED] == SPEED_HIGH
# Test states # Test states
assert hass.states.get("fan.ceiling_fan").state == STATE_ON event_changed_light = {
assert hass.states.get("fan.ceiling_fan").attributes["speed"] == SPEED_HIGH
state_changed_event = {
"t": "event", "t": "event",
"e": "changed", "e": "changed",
"r": "lights", "r": "lights",
"id": "1", "id": "1",
"state": {"speed": 0}, "state": {"speed": 0},
} }
gateway.api.event_handler(state_changed_event) await mock_deconz_websocket(data=event_changed_light)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("fan.ceiling_fan").state == STATE_OFF assert hass.states.get("fan.ceiling_fan").state == STATE_OFF
assert hass.states.get("fan.ceiling_fan").attributes["speed"] == SPEED_OFF assert hass.states.get("fan.ceiling_fan").attributes[ATTR_SPEED] == SPEED_OFF
# Test service calls # Test service calls
@ -157,23 +152,23 @@ async def test_fans(hass, aioclient_mock):
# Events with an unsupported speed gets converted to default speed "medium" # Events with an unsupported speed gets converted to default speed "medium"
state_changed_event = { event_changed_light = {
"t": "event", "t": "event",
"e": "changed", "e": "changed",
"r": "lights", "r": "lights",
"id": "1", "id": "1",
"state": {"speed": 3}, "state": {"speed": 3},
} }
gateway.api.event_handler(state_changed_event) await mock_deconz_websocket(data=event_changed_light)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("fan.ceiling_fan").state == STATE_ON assert hass.states.get("fan.ceiling_fan").state == STATE_ON
assert hass.states.get("fan.ceiling_fan").attributes["speed"] == SPEED_MEDIUM assert hass.states.get("fan.ceiling_fan").attributes[ATTR_SPEED] == SPEED_MEDIUM
await hass.config_entries.async_unload(config_entry.entry_id) await hass.config_entries.async_unload(config_entry.entry_id)
states = hass.states.async_all() states = hass.states.async_all()
assert len(hass.states.async_all()) == 2 assert len(states) == 2
for state in states: for state in states:
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE