1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Improve deCONZ tests by using aioclient_mock rather than patching web requests (#45927)

* Don't patch web requests, use aioclient_mock instead

* Remove stale prints

* Remove tests for old way of loading platforms

* Remove unused imports
This commit is contained in:
Robert Svensson
2021-02-09 08:31:29 +01:00
committed by GitHub
parent b33753f334
commit 20f45f8ab9
16 changed files with 700 additions and 857 deletions

View File

@@ -1,15 +1,15 @@
"""deCONZ scene platform tests."""
from copy import deepcopy
from unittest.mock import patch
from homeassistant.components.deconz import DOMAIN as DECONZ_DOMAIN
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN, SERVICE_TURN_ON
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.setup import async_setup_component
from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration
from .test_gateway import (
DECONZ_WEB_REQUEST,
mock_deconz_put_request,
setup_deconz_integration,
)
GROUPS = {
"1": {
@@ -24,48 +24,38 @@ GROUPS = {
}
async def test_platform_manually_configured(hass):
"""Test that we do not discover anything or try to set up a gateway."""
assert (
await async_setup_component(
hass, SCENE_DOMAIN, {"scene": {"platform": DECONZ_DOMAIN}}
)
is True
)
assert DECONZ_DOMAIN not in hass.data
async def test_no_scenes(hass):
async def test_no_scenes(hass, aioclient_mock):
"""Test that scenes can be loaded without scenes being available."""
await setup_deconz_integration(hass)
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
async def test_scenes(hass):
async def test_scenes(hass, aioclient_mock):
"""Test that scenes works."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["groups"] = deepcopy(GROUPS)
config_entry = await setup_deconz_integration(hass, get_state_response=data)
gateway = get_gateway_from_config_entry(hass, config_entry)
config_entry = await setup_deconz_integration(
hass, aioclient_mock, get_state_response=data
)
assert len(hass.states.async_all()) == 1
assert hass.states.get("scene.light_group_scene")
# Verify service calls
group_scene = gateway.api.groups["1"].scenes["1"]
mock_deconz_put_request(
aioclient_mock, config_entry.data, "/groups/1/scenes/1/recall"
)
# Service turn on scene
with patch.object(group_scene, "_request", return_value=True) as set_callback:
await hass.services.async_call(
SCENE_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "scene.light_group_scene"},
blocking=True,
)
await hass.async_block_till_done()
set_callback.assert_called_with("put", "/groups/1/scenes/1/recall", json={})
await hass.services.async_call(
SCENE_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "scene.light_group_scene"},
blocking=True,
)
assert aioclient_mock.mock_calls[1][2] == {}
await hass.config_entries.async_unload(config_entry.entry_id)