deCONZ - Improve service tests (#26663)

* Improve configure service tests

* Add refresh device service test

* Add tests for setup and unload services

* Remove refresh device test from test_init

* Extra verification of deconz services existance in hass.data
This commit is contained in:
Robert Svensson 2019-09-16 10:08:13 +02:00 committed by Pascal Vizeli
parent 719a601880
commit 5116d02747
3 changed files with 248 additions and 98 deletions

View file

@ -3,7 +3,6 @@ from unittest.mock import Mock, patch
import asyncio
import pytest
import voluptuous as vol
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.components import deconz
@ -168,98 +167,3 @@ async def test_unload_entry_multiple_gateways(hass):
assert ENTRY2_BRIDGEID in hass.data[deconz.DOMAIN]
assert hass.data[deconz.DOMAIN][ENTRY2_BRIDGEID].master
async def test_service_configure(hass):
"""Test that service invokes pydeconz with the correct path and data."""
entry = MockConfigEntry(
domain=deconz.DOMAIN,
data={
deconz.config_flow.CONF_HOST: ENTRY1_HOST,
deconz.config_flow.CONF_PORT: ENTRY1_PORT,
deconz.config_flow.CONF_API_KEY: ENTRY1_API_KEY,
deconz.CONF_BRIDGEID: ENTRY1_BRIDGEID,
},
)
entry.add_to_hass(hass)
await setup_entry(hass, entry)
hass.data[deconz.DOMAIN][ENTRY1_BRIDGEID].deconz_ids = {"light.test": "/light/1"}
data = {"on": True, "attr1": 10, "attr2": 20}
# only field
with patch("pydeconz.DeconzSession.async_put_state", return_value=mock_coro(True)):
await hass.services.async_call(
"deconz", "configure", service_data={"field": "/light/42", "data": data}
)
await hass.async_block_till_done()
# only entity
with patch("pydeconz.DeconzSession.async_put_state", return_value=mock_coro(True)):
await hass.services.async_call(
"deconz", "configure", service_data={"entity": "light.test", "data": data}
)
await hass.async_block_till_done()
# entity + field
with patch("pydeconz.DeconzSession.async_put_state", return_value=mock_coro(True)):
await hass.services.async_call(
"deconz",
"configure",
service_data={"entity": "light.test", "field": "/state", "data": data},
)
await hass.async_block_till_done()
# non-existing entity (or not from deCONZ)
with patch("pydeconz.DeconzSession.async_put_state", return_value=mock_coro(True)):
await hass.services.async_call(
"deconz",
"configure",
service_data={
"entity": "light.nonexisting",
"field": "/state",
"data": data,
},
)
await hass.async_block_till_done()
# field does not start with /
with pytest.raises(vol.Invalid):
with patch(
"pydeconz.DeconzSession.async_put_state", return_value=mock_coro(True)
):
await hass.services.async_call(
"deconz",
"configure",
service_data={"entity": "light.test", "field": "state", "data": data},
)
await hass.async_block_till_done()
async def test_service_refresh_devices(hass):
"""Test that service can refresh devices."""
entry = MockConfigEntry(
domain=deconz.DOMAIN,
data={
deconz.config_flow.CONF_HOST: ENTRY1_HOST,
deconz.config_flow.CONF_PORT: ENTRY1_PORT,
deconz.config_flow.CONF_API_KEY: ENTRY1_API_KEY,
deconz.CONF_BRIDGEID: ENTRY1_BRIDGEID,
},
)
entry.add_to_hass(hass)
await setup_entry(hass, entry)
with patch(
"pydeconz.DeconzSession.async_load_parameters", return_value=mock_coro(True)
):
await hass.services.async_call("deconz", "device_refresh", service_data={})
await hass.async_block_till_done()
with patch(
"pydeconz.DeconzSession.async_load_parameters", return_value=mock_coro(False)
):
await hass.services.async_call("deconz", "device_refresh", service_data={})
await hass.async_block_till_done()