Use fixtures in deCONZ light tests PT2 (#121208)

This commit is contained in:
Robert Svensson 2024-07-05 10:57:17 +02:00 committed by GitHub
parent 2ab02c06c6
commit eb5a98e7ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 160 additions and 160 deletions

View file

@ -162,6 +162,14 @@ def fixture_group_data() -> dict[str, Any]:
@pytest.fixture(name="light_payload") @pytest.fixture(name="light_payload")
def fixture_light_0_data(light_0_payload: dict[str, Any]) -> dict[str, Any]:
"""Light data."""
if light_0_payload:
return {"0": light_0_payload}
return {}
@pytest.fixture(name="light_0_payload")
def fixture_light_data() -> dict[str, Any]: def fixture_light_data() -> dict[str, Any]:
"""Light data.""" """Light data."""
return {} return {}

View file

@ -2,7 +2,6 @@
from collections.abc import Callable from collections.abc import Callable
from typing import Any from typing import Any
from unittest.mock import patch
import pytest import pytest
@ -41,25 +40,11 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .test_gateway import (
DECONZ_WEB_REQUEST,
mock_deconz_put_request,
setup_deconz_integration,
)
from tests.test_util.aiohttp import AiohttpClientMocker from tests.test_util.aiohttp import AiohttpClientMocker
async def test_no_lights_or_groups(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that no lights or groups entities are created."""
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
@pytest.mark.parametrize( @pytest.mark.parametrize(
("input", "expected"), ("light_0_payload", "expected"),
[ [
( # RGB light in color temp color mode ( # RGB light in color temp color mode
{ {
@ -426,13 +411,11 @@ async def test_no_lights_or_groups(
], ],
) )
async def test_lights( async def test_lights(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, input, expected hass: HomeAssistant,
config_entry_setup: ConfigEntry,
expected: dict[str, Any],
) -> None: ) -> None:
"""Test that different light entities are created with expected values.""" """Test that different light entities are created with expected values."""
data = {"lights": {"0": input}}
with patch.dict(DECONZ_WEB_REQUEST, data):
config_entry = await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 1 assert len(hass.states.async_all()) == 1
light = hass.states.get(expected["entity_id"]) light = hass.states.get(expected["entity_id"])
@ -440,23 +423,21 @@ async def test_lights(
for attribute, expected_value in expected["attributes"].items(): for attribute, expected_value in expected["attributes"].items():
assert light.attributes[attribute] == expected_value assert light.attributes[attribute] == expected_value
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() states = hass.states.async_all()
for state in states: for state in states:
assert state.state == STATE_UNAVAILABLE 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() await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0 assert len(hass.states.async_all()) == 0
async def test_light_state_change( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_deconz_websocket "light_payload",
) -> None: [
"""Verify light can change state on websocket event.""" {
data = {
"lights": {
"0": { "0": {
"colorcapabilities": 31, "colorcapabilities": 31,
"ctmax": 500, "ctmax": 500,
@ -485,10 +466,11 @@ async def test_light_state_change(
"uniqueid": "00:17:88:01:01:23:45:67-00", "uniqueid": "00:17:88:01:01:23:45:67-00",
} }
} }
} ],
with patch.dict(DECONZ_WEB_REQUEST, data): )
await setup_deconz_integration(hass, aioclient_mock) @pytest.mark.usefixtures("config_entry_setup")
async def test_light_state_change(hass: HomeAssistant, mock_deconz_websocket) -> None:
"""Verify light can change state on websocket event."""
assert hass.states.get("light.hue_go").state == STATE_ON assert hass.states.get("light.hue_go").state == STATE_ON
event_changed_light = { event_changed_light = {
@ -642,44 +624,47 @@ async def test_light_state_change(
], ],
) )
async def test_light_service_calls( async def test_light_service_calls(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, input, expected hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
config_entry_factory: Callable[[], ConfigEntry],
light_payload: dict[str, Any],
mock_put_request: Callable[[str, str], AiohttpClientMocker],
input: dict[str, Any],
expected: dict[str, Any],
) -> None: ) -> None:
"""Verify light can change state on websocket event.""" """Verify light can change state on websocket event."""
data = { light_payload |= {
"lights": { "0": {
"0": { "colorcapabilities": 31,
"colorcapabilities": 31, "ctmax": 500,
"ctmax": 500, "ctmin": 153,
"ctmin": 153, "etag": "055485a82553e654f156d41c9301b7cf",
"etag": "055485a82553e654f156d41c9301b7cf", "hascolor": True,
"hascolor": True, "lastannounced": None,
"lastannounced": None, "lastseen": "2021-06-10T20:25Z",
"lastseen": "2021-06-10T20:25Z", "manufacturername": "Philips",
"manufacturername": "Philips", "modelid": "LLC020",
"modelid": "LLC020", "name": "Hue Go",
"name": "Hue Go", "state": {
"state": { "alert": "none",
"alert": "none", "bri": 254,
"bri": 254, "colormode": "ct",
"colormode": "ct", "ct": 375,
"ct": 375, "effect": "none",
"effect": "none", "hue": 8348,
"hue": 8348, "on": input["light_on"],
"on": input["light_on"], "reachable": True,
"reachable": True, "sat": 147,
"sat": 147, "xy": [0.462, 0.4111],
"xy": [0.462, 0.4111], },
}, "swversion": "5.127.1.26420",
"swversion": "5.127.1.26420", "type": "Extended color light",
"type": "Extended color light", "uniqueid": "00:17:88:01:01:23:45:67-00",
"uniqueid": "00:17:88:01:01:23:45:67-00",
}
} }
} }
with patch.dict(DECONZ_WEB_REQUEST, data): await config_entry_factory()
config_entry = await setup_deconz_integration(hass, aioclient_mock)
mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/0/state") aioclient_mock = mock_put_request("/lights/0/state")
await hass.services.async_call( await hass.services.async_call(
LIGHT_DOMAIN, LIGHT_DOMAIN,
@ -693,12 +678,10 @@ async def test_light_service_calls(
assert len(aioclient_mock.mock_calls) == 1 # not called assert len(aioclient_mock.mock_calls) == 1 # not called
async def test_ikea_default_transition_time( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker "light_payload",
) -> None: [
"""Verify that service calls to IKEA lights always extend with transition tinme 0 if absent.""" {
data = {
"lights": {
"0": { "0": {
"colorcapabilities": 0, "colorcapabilities": 0,
"ctmax": 65535, "ctmax": 65535,
@ -721,13 +704,17 @@ async def test_ikea_default_transition_time(
"swversion": "2.0.022", "swversion": "2.0.022",
"type": "Color temperature light", "type": "Color temperature light",
"uniqueid": "ec:1b:bd:ff:fe:ee:ed:dd-01", "uniqueid": "ec:1b:bd:ff:fe:ee:ed:dd-01",
}, }
}, }
} ],
with patch.dict(DECONZ_WEB_REQUEST, data): )
config_entry = await setup_deconz_integration(hass, aioclient_mock) @pytest.mark.usefixtures("config_entry_setup")
async def test_ikea_default_transition_time(
mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/0/state") hass: HomeAssistant,
mock_put_request: Callable[[str, str], AiohttpClientMocker],
) -> None:
"""Verify that service calls to IKEA lights always extend with transition tinme 0 if absent."""
aioclient_mock = mock_put_request("/lights/0/state")
await hass.services.async_call( await hass.services.async_call(
LIGHT_DOMAIN, LIGHT_DOMAIN,
@ -761,12 +748,10 @@ async def test_ikea_default_transition_time(
} }
async def test_lidl_christmas_light( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker "light_payload",
) -> None: [
"""Test that lights or groups entities are created.""" {
data = {
"lights": {
"0": { "0": {
"etag": "87a89542bf9b9d0aa8134919056844f8", "etag": "87a89542bf9b9d0aa8134919056844f8",
"hascolor": True, "hascolor": True,
@ -789,12 +774,15 @@ async def test_lidl_christmas_light(
"uniqueid": "58:8e:81:ff:fe:db:7b:be-01", "uniqueid": "58:8e:81:ff:fe:db:7b:be-01",
} }
} }
} ],
)
with patch.dict(DECONZ_WEB_REQUEST, data): @pytest.mark.usefixtures("config_entry_setup")
config_entry = await setup_deconz_integration(hass, aioclient_mock) async def test_lidl_christmas_light(
hass: HomeAssistant,
mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/0/state") mock_put_request: Callable[[str, str], AiohttpClientMocker],
) -> None:
"""Test that lights or groups entities are created."""
aioclient_mock = mock_put_request("/lights/0/state")
await hass.services.async_call( await hass.services.async_call(
LIGHT_DOMAIN, LIGHT_DOMAIN,
@ -806,16 +794,13 @@ async def test_lidl_christmas_light(
blocking=True, blocking=True,
) )
assert aioclient_mock.mock_calls[1][2] == {"on": True, "hue": 3640, "sat": 76} assert aioclient_mock.mock_calls[1][2] == {"on": True, "hue": 3640, "sat": 76}
assert hass.states.get("light.lidl_xmas_light") assert hass.states.get("light.lidl_xmas_light")
async def test_configuration_tool( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker "light_payload",
) -> None: [
"""Verify that configuration tool is not created.""" {
data = {
"lights": {
"0": { "0": {
"etag": "26839cb118f5bf7ba1f2108256644010", "etag": "26839cb118f5bf7ba1f2108256644010",
"hascolor": False, "hascolor": False,
@ -830,13 +815,53 @@ async def test_configuration_tool(
"uniqueid": "00:21:2e:ff:ff:05:a7:a3-01", "uniqueid": "00:21:2e:ff:ff:05:a7:a3-01",
} }
} }
} ],
with patch.dict(DECONZ_WEB_REQUEST, data): )
await setup_deconz_integration(hass, aioclient_mock) @pytest.mark.usefixtures("config_entry_setup")
async def test_configuration_tool(hass: HomeAssistant) -> None:
"""Verify that configuration tool is not created."""
assert len(hass.states.async_all()) == 0 assert len(hass.states.async_all()) == 0
@pytest.mark.parametrize(
"light_payload",
[
{
"1": {
"name": "RGB light",
"state": {
"on": True,
"bri": 50,
"colormode": "xy",
"effect": "colorloop",
"xy": (0.5, 0.5),
"reachable": True,
},
"type": "Extended color light",
"uniqueid": "00:00:00:00:00:00:00:00-00",
},
"2": {
"ctmax": 454,
"ctmin": 155,
"name": "Tunable white light",
"state": {
"on": True,
"colormode": "ct",
"ct": 2500,
"reachable": True,
},
"type": "Tunable white light",
"uniqueid": "00:00:00:00:00:00:00:01-00",
},
"3": {
"name": "Dimmable light",
"type": "Dimmable light",
"state": {"bri": 255, "on": True, "reachable": True},
"uniqueid": "00:00:00:00:00:00:00:02-00",
},
}
],
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
("input", "expected"), ("input", "expected"),
[ [
@ -908,69 +933,36 @@ async def test_configuration_tool(
], ],
) )
async def test_groups( async def test_groups(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, input, expected hass: HomeAssistant,
config_entry_factory: Callable[[], ConfigEntry],
group_payload: dict[str, Any],
input: dict[str, list[str]],
expected: dict[str, Any],
) -> None: ) -> None:
"""Test that different group entities are created with expected values.""" """Test that different group entities are created with expected values."""
data = { group_payload |= {
"groups": { "0": {
"0": { "id": "Light group id",
"id": "Light group id", "name": "Group",
"name": "Group", "type": "LightGroup",
"type": "LightGroup", "state": {"all_on": False, "any_on": True},
"state": {"all_on": False, "any_on": True}, "action": {
"action": { "alert": "none",
"alert": "none", "bri": 127,
"bri": 127, "colormode": "hs",
"colormode": "hs", "ct": 0,
"ct": 0, "effect": "none",
"effect": "none", "hue": 0,
"hue": 0, "on": True,
"on": True, "sat": 127,
"sat": 127, "scene": None,
"scene": None, "xy": [0, 0],
"xy": [0, 0],
},
"scenes": [],
"lights": input["lights"],
},
},
"lights": {
"1": {
"name": "RGB light",
"state": {
"on": True,
"bri": 50,
"colormode": "xy",
"effect": "colorloop",
"xy": (0.5, 0.5),
"reachable": True,
},
"type": "Extended color light",
"uniqueid": "00:00:00:00:00:00:00:00-00",
},
"2": {
"ctmax": 454,
"ctmin": 155,
"name": "Tunable white light",
"state": {
"on": True,
"colormode": "ct",
"ct": 2500,
"reachable": True,
},
"type": "Tunable white light",
"uniqueid": "00:00:00:00:00:00:00:01-00",
},
"3": {
"name": "Dimmable light",
"type": "Dimmable light",
"state": {"bri": 255, "on": True, "reachable": True},
"uniqueid": "00:00:00:00:00:00:00:02-00",
}, },
"scenes": [],
"lights": input["lights"],
}, },
} }
with patch.dict(DECONZ_WEB_REQUEST, data): config_entry = await config_entry_factory()
config_entry = await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 4 assert len(hass.states.async_all()) == 4