From 167a8c6613265cc1404924953b008ca84cf3745e Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Mon, 1 Jul 2024 19:49:12 +0200 Subject: [PATCH] Use fixtures in deCONZ fan tests (#120953) --- tests/components/deconz/test_fan.py | 286 ++-------------------------- 1 file changed, 20 insertions(+), 266 deletions(-) diff --git a/tests/components/deconz/test_fan.py b/tests/components/deconz/test_fan.py index 5da0398c3e6..0f22c0b2b3b 100644 --- a/tests/components/deconz/test_fan.py +++ b/tests/components/deconz/test_fan.py @@ -1,9 +1,8 @@ """deCONZ fan platform tests.""" -from unittest.mock import patch +from collections.abc import Callable import pytest -from voluptuous.error import MultipleInvalid from homeassistant.components.fan import ( ATTR_PERCENTAGE, @@ -12,14 +11,11 @@ from homeassistant.components.fan import ( SERVICE_TURN_OFF, SERVICE_TURN_ON, ) +from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant -from .test_gateway import ( - DECONZ_WEB_REQUEST, - mock_deconz_put_request, - setup_deconz_integration, -) +from .test_gateway import setup_deconz_integration from tests.test_util.aiohttp import AiohttpClientMocker @@ -32,12 +28,10 @@ async def test_no_fans( assert len(hass.states.async_all()) == 0 -async def test_fans( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_deconz_websocket -) -> None: - """Test that all supported fan entities are created.""" - data = { - "lights": { +@pytest.mark.parametrize( + "light_payload", + [ + { "1": { "etag": "432f3de28965052961a99e3c5494daf4", "hascolor": False, @@ -56,11 +50,16 @@ async def test_fans( "uniqueid": "00:22:a3:00:00:27:8b:81-01", } } - } - - with patch.dict(DECONZ_WEB_REQUEST, data): - config_entry = await setup_deconz_integration(hass, aioclient_mock) - + ], +) +async def test_fans( + hass: HomeAssistant, + aioclient_mock: AiohttpClientMocker, + config_entry_setup: ConfigEntry, + mock_put_request: Callable[[str, str], AiohttpClientMocker], + mock_deconz_websocket, +) -> None: + """Test that all supported fan entities are created.""" assert len(hass.states.async_all()) == 2 # Light and fan assert hass.states.get("fan.ceiling_fan").state == STATE_ON assert hass.states.get("fan.ceiling_fan").attributes[ATTR_PERCENTAGE] == 100 @@ -134,7 +133,7 @@ async def test_fans( # Test service calls - mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/1/state") + aioclient_mock = mock_put_request("/lights/1/state") # Service turn on fan using saved default_on_speed @@ -231,258 +230,13 @@ async def test_fans( assert hass.states.get("fan.ceiling_fan").state == STATE_ON assert not hass.states.get("fan.ceiling_fan").attributes[ATTR_PERCENTAGE] - await hass.config_entries.async_unload(config_entry.entry_id) + await hass.config_entries.async_unload(config_entry_setup.entry_id) states = hass.states.async_all() assert len(states) == 2 for state in states: assert state.state == STATE_UNAVAILABLE - await hass.config_entries.async_remove(config_entry.entry_id) - await hass.async_block_till_done() - assert len(hass.states.async_all()) == 0 - - -async def test_fans_legacy_speed_modes( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_deconz_websocket -) -> None: - """Test that all supported fan entities are created. - - Legacy fan support. - """ - data = { - "lights": { - "1": { - "etag": "432f3de28965052961a99e3c5494daf4", - "hascolor": False, - "manufacturername": "King Of Fans, Inc.", - "modelid": "HDC52EastwindFan", - "name": "Ceiling fan", - "state": { - "alert": "none", - "bri": 254, - "on": False, - "reachable": True, - "speed": 4, - }, - "swversion": "0000000F", - "type": "Fan", - "uniqueid": "00:22:a3:00:00:27:8b:81-01", - } - } - } - - with patch.dict(DECONZ_WEB_REQUEST, data): - config_entry = await setup_deconz_integration(hass, aioclient_mock) - - assert len(hass.states.async_all()) == 2 # Light and fan - assert hass.states.get("fan.ceiling_fan").state == STATE_ON - - # Test states - - event_changed_light = { - "t": "event", - "e": "changed", - "r": "lights", - "id": "1", - "state": {"speed": 1}, - } - await mock_deconz_websocket(data=event_changed_light) - await hass.async_block_till_done() - - assert hass.states.get("fan.ceiling_fan").state == STATE_ON - assert hass.states.get("fan.ceiling_fan").attributes[ATTR_PERCENTAGE] == 25 - - event_changed_light = { - "t": "event", - "e": "changed", - "r": "lights", - "id": "1", - "state": {"speed": 2}, - } - await mock_deconz_websocket(data=event_changed_light) - await hass.async_block_till_done() - - assert hass.states.get("fan.ceiling_fan").state == STATE_ON - assert hass.states.get("fan.ceiling_fan").attributes[ATTR_PERCENTAGE] == 50 - - event_changed_light = { - "t": "event", - "e": "changed", - "r": "lights", - "id": "1", - "state": {"speed": 3}, - } - await mock_deconz_websocket(data=event_changed_light) - await hass.async_block_till_done() - - assert hass.states.get("fan.ceiling_fan").state == STATE_ON - assert hass.states.get("fan.ceiling_fan").attributes[ATTR_PERCENTAGE] == 75 - - event_changed_light = { - "t": "event", - "e": "changed", - "r": "lights", - "id": "1", - "state": {"speed": 4}, - } - await mock_deconz_websocket(data=event_changed_light) - await hass.async_block_till_done() - - assert hass.states.get("fan.ceiling_fan").state == STATE_ON - assert hass.states.get("fan.ceiling_fan").attributes[ATTR_PERCENTAGE] == 100 - - event_changed_light = { - "t": "event", - "e": "changed", - "r": "lights", - "id": "1", - "state": {"speed": 0}, - } - await mock_deconz_websocket(data=event_changed_light) - await hass.async_block_till_done() - - assert hass.states.get("fan.ceiling_fan").state == STATE_OFF - assert hass.states.get("fan.ceiling_fan").attributes[ATTR_PERCENTAGE] == 0 - - # Test service calls - - mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/1/state") - - # Service turn on fan using saved default_on_speed - - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "fan.ceiling_fan"}, - blocking=True, - ) - assert aioclient_mock.mock_calls[1][2] == {"speed": 4} - - # Service turn on fan with speed_off - # async_turn_on_compat use speed_to_percentage which will return 0 - - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_PERCENTAGE: 0}, - blocking=True, - ) - assert aioclient_mock.mock_calls[2][2] == {"speed": 0} - - # Service turn on fan with bad speed - # async_turn_on_compat use speed_to_percentage which will convert to SPEED_MEDIUM -> 2 - - with pytest.raises(MultipleInvalid): - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_PERCENTAGE: "bad"}, - blocking=True, - ) - - # Service turn on fan to low speed - - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_PERCENTAGE: 25}, - blocking=True, - ) - assert aioclient_mock.mock_calls[3][2] == {"speed": 1} - - # Service turn on fan to medium speed - - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_PERCENTAGE: 50}, - blocking=True, - ) - assert aioclient_mock.mock_calls[4][2] == {"speed": 2} - - # Service turn on fan to high speed - - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_PERCENTAGE: 100}, - blocking=True, - ) - assert aioclient_mock.mock_calls[5][2] == {"speed": 4} - - # Service set fan speed to low - - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_SET_PERCENTAGE, - {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_PERCENTAGE: 25}, - blocking=True, - ) - assert aioclient_mock.mock_calls[6][2] == {"speed": 1} - - # Service set fan speed to medium - - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_SET_PERCENTAGE, - {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_PERCENTAGE: 50}, - blocking=True, - ) - assert aioclient_mock.mock_calls[7][2] == {"speed": 2} - - # Service set fan speed to high - - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_SET_PERCENTAGE, - {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_PERCENTAGE: 100}, - blocking=True, - ) - assert aioclient_mock.mock_calls[8][2] == {"speed": 4} - - # Service set fan speed to off - - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_SET_PERCENTAGE, - {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_PERCENTAGE: 0}, - blocking=True, - ) - assert aioclient_mock.mock_calls[9][2] == {"speed": 0} - - # Service set fan speed to unsupported value - - with pytest.raises(MultipleInvalid): - await hass.services.async_call( - FAN_DOMAIN, - SERVICE_SET_PERCENTAGE, - {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_PERCENTAGE: "bad value"}, - blocking=True, - ) - - # Events with an unsupported speed gets converted to default speed "medium" - - event_changed_light = { - "t": "event", - "e": "changed", - "r": "lights", - "id": "1", - "state": {"speed": 3}, - } - await mock_deconz_websocket(data=event_changed_light) - await hass.async_block_till_done() - - assert hass.states.get("fan.ceiling_fan").state == STATE_ON - assert hass.states.get("fan.ceiling_fan").attributes[ATTR_PERCENTAGE] == 75 - - await hass.config_entries.async_unload(config_entry.entry_id) - - states = hass.states.async_all() - assert len(states) == 2 - for state in states: - assert state.state == STATE_UNAVAILABLE - - await hass.config_entries.async_remove(config_entry.entry_id) + await hass.config_entries.async_remove(config_entry_setup.entry_id) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0