Use fixtures in deCONZ cover tests (#120954)

This commit is contained in:
Robert Svensson 2024-07-01 20:31:42 +02:00 committed by GitHub
parent 167a8c6613
commit 690164a518
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,8 @@
"""deCONZ cover platform tests.""" """deCONZ cover platform tests."""
from unittest.mock import patch from collections.abc import Callable
import pytest
from homeassistant.components.cover import ( from homeassistant.components.cover import (
ATTR_CURRENT_POSITION, ATTR_CURRENT_POSITION,
@ -17,6 +19,7 @@ from homeassistant.components.cover import (
SERVICE_STOP_COVER, SERVICE_STOP_COVER,
SERVICE_STOP_COVER_TILT, SERVICE_STOP_COVER_TILT,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
STATE_CLOSED, STATE_CLOSED,
@ -25,29 +28,13 @@ 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_covers( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker "light_payload",
) -> None: [
"""Test that no cover entities are created.""" {
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
async def test_cover(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_deconz_websocket
) -> None:
"""Test that all supported cover entities are created."""
data = {
"lights": {
"1": { "1": {
"name": "Window covering device", "name": "Window covering device",
"type": "Window covering device", "type": "Window covering device",
@ -62,10 +49,15 @@ async def test_cover(
"uniqueid": "00:00:00:00:00:00:00:02-00", "uniqueid": "00:00:00:00:00:00:00:02-00",
}, },
} }
} ],
with patch.dict(DECONZ_WEB_REQUEST, data): )
config_entry = await setup_deconz_integration(hass, aioclient_mock) async def test_cover(
hass: HomeAssistant,
config_entry_setup: ConfigEntry,
mock_put_request: Callable[[str, str], AiohttpClientMocker],
mock_deconz_websocket,
) -> None:
"""Test that all supported cover entities are created."""
assert len(hass.states.async_all()) == 2 assert len(hass.states.async_all()) == 2
cover = hass.states.get("cover.window_covering_device") cover = hass.states.get("cover.window_covering_device")
assert cover.state == STATE_CLOSED assert cover.state == STATE_CLOSED
@ -90,7 +82,7 @@ async def test_cover(
# Verify service calls for cover # Verify service calls for cover
mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/1/state") aioclient_mock = mock_put_request("/lights/1/state")
# Service open cover # Service open cover
@ -132,24 +124,22 @@ async def test_cover(
) )
assert aioclient_mock.mock_calls[4][2] == {"stop": True} assert aioclient_mock.mock_calls[4][2] == {"stop": True}
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()
assert len(states) == 2 assert len(states) == 2
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_tilt_cover( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker "light_payload",
) -> None: [
"""Test that tilting a cover works.""" {
data = {
"lights": {
"0": { "0": {
"etag": "87269755b9b3a046485fdae8d96b252c", "etag": "87269755b9b3a046485fdae8d96b252c",
"lastannounced": None, "lastannounced": None,
@ -170,10 +160,13 @@ async def test_tilt_cover(
"uniqueid": "00:24:46:00:00:12:34:56-01", "uniqueid": "00:24:46:00:00:12:34:56-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_tilt_cover(
hass: HomeAssistant, mock_put_request: Callable[[str, str], AiohttpClientMocker]
) -> None:
"""Test that tilting a cover works."""
assert len(hass.states.async_all()) == 1 assert len(hass.states.async_all()) == 1
covering_device = hass.states.get("cover.covering_device") covering_device = hass.states.get("cover.covering_device")
assert covering_device.state == STATE_OPEN assert covering_device.state == STATE_OPEN
@ -181,7 +174,7 @@ async def test_tilt_cover(
# Verify service calls for tilting cover # Verify service calls for tilting cover
mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/0/state") aioclient_mock = mock_put_request("/lights/0/state")
# Service set tilt cover # Service set tilt cover
@ -224,12 +217,10 @@ async def test_tilt_cover(
assert aioclient_mock.mock_calls[4][2] == {"stop": True} assert aioclient_mock.mock_calls[4][2] == {"stop": True}
async def test_level_controllable_output_cover( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker "light_payload",
) -> None: [
"""Test that tilting a cover works.""" {
data = {
"lights": {
"0": { "0": {
"etag": "4cefc909134c8e99086b55273c2bde67", "etag": "4cefc909134c8e99086b55273c2bde67",
"hascolor": False, "hascolor": False,
@ -250,10 +241,13 @@ async def test_level_controllable_output_cover(
"uniqueid": "00:22:a3:00:00:00:00:00-01", "uniqueid": "00:22:a3:00:00:00:00:00-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_level_controllable_output_cover(
hass: HomeAssistant, mock_put_request: Callable[[str, str], AiohttpClientMocker]
) -> None:
"""Test that tilting a cover works."""
assert len(hass.states.async_all()) == 1 assert len(hass.states.async_all()) == 1
covering_device = hass.states.get("cover.vent") covering_device = hass.states.get("cover.vent")
assert covering_device.state == STATE_OPEN assert covering_device.state == STATE_OPEN
@ -261,7 +255,7 @@ async def test_level_controllable_output_cover(
# Verify service calls for tilting cover # Verify service calls for tilting cover
mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/0/state") aioclient_mock = mock_put_request("/lights/0/state")
# Service open cover # Service open cover