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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 700 additions and 857 deletions

View file

@ -2,19 +2,14 @@
from copy import deepcopy
from homeassistant.components.deconz.const import (
CONF_ALLOW_CLIP_SENSOR,
DOMAIN as DECONZ_DOMAIN,
)
from homeassistant.components.deconz.const import CONF_ALLOW_CLIP_SENSOR
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_POWER,
STATE_UNAVAILABLE,
)
from homeassistant.setup import async_setup_component
from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration
@ -86,28 +81,19 @@ SENSORS = {
}
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, SENSOR_DOMAIN, {"sensor": {"platform": DECONZ_DOMAIN}}
)
is True
)
assert DECONZ_DOMAIN not in hass.data
async def test_no_sensors(hass):
async def test_no_sensors(hass, aioclient_mock):
"""Test that no sensors in deconz results in no sensor entities."""
await setup_deconz_integration(hass)
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
async def test_sensors(hass):
async def test_sensors(hass, aioclient_mock):
"""Test successful creation of sensor entities."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["sensors"] = deepcopy(SENSORS)
config_entry = await setup_deconz_integration(hass, get_state_response=data)
config_entry = await setup_deconz_integration(
hass, aioclient_mock, get_state_response=data
)
gateway = get_gateway_from_config_entry(hass, config_entry)
assert len(hass.states.async_all()) == 5
@ -176,12 +162,13 @@ async def test_sensors(hass):
assert len(hass.states.async_all()) == 0
async def test_allow_clip_sensors(hass):
async def test_allow_clip_sensors(hass, aioclient_mock):
"""Test that CLIP sensors can be allowed."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["sensors"] = deepcopy(SENSORS)
config_entry = await setup_deconz_integration(
hass,
aioclient_mock,
options={CONF_ALLOW_CLIP_SENSOR: True},
get_state_response=data,
)
@ -210,9 +197,9 @@ async def test_allow_clip_sensors(hass):
assert hass.states.get("sensor.clip_light_level_sensor")
async def test_add_new_sensor(hass):
async def test_add_new_sensor(hass, aioclient_mock):
"""Test that adding a new sensor works."""
config_entry = await setup_deconz_integration(hass)
config_entry = await setup_deconz_integration(hass, aioclient_mock)
gateway = get_gateway_from_config_entry(hass, config_entry)
assert len(hass.states.async_all()) == 0
@ -230,11 +217,13 @@ async def test_add_new_sensor(hass):
assert hass.states.get("sensor.light_level_sensor").state == "999.8"
async def test_add_battery_later(hass):
async def test_add_battery_later(hass, aioclient_mock):
"""Test that a sensor without an initial battery state creates a battery sensor once state exist."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["sensors"] = {"1": deepcopy(SENSORS["3"])}
config_entry = await setup_deconz_integration(hass, get_state_response=data)
config_entry = await setup_deconz_integration(
hass, aioclient_mock, get_state_response=data
)
gateway = get_gateway_from_config_entry(hass, config_entry)
remote = gateway.api.sensors["1"]
@ -252,7 +241,7 @@ async def test_add_battery_later(hass):
assert hass.states.get("sensor.switch_1_battery_level")
async def test_air_quality_sensor(hass):
async def test_air_quality_sensor(hass, aioclient_mock):
"""Test successful creation of air quality sensor entities."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["sensors"] = {
@ -274,7 +263,7 @@ async def test_air_quality_sensor(hass):
"uniqueid": "00:12:4b:00:14:4d:00:07-02-fdef",
}
}
await setup_deconz_integration(hass, get_state_response=data)
await setup_deconz_integration(hass, aioclient_mock, get_state_response=data)
assert len(hass.states.async_all()) == 1
@ -282,7 +271,7 @@ async def test_air_quality_sensor(hass):
assert air_quality.state == "poor"
async def test_time_sensor(hass):
async def test_time_sensor(hass, aioclient_mock):
"""Test successful creation of time sensor entities."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["sensors"] = {
@ -305,7 +294,7 @@ async def test_time_sensor(hass):
"uniqueid": "cc:cc:cc:ff:fe:38:4d:b3-01-000a",
}
}
await setup_deconz_integration(hass, get_state_response=data)
await setup_deconz_integration(hass, aioclient_mock, get_state_response=data)
assert len(hass.states.async_all()) == 2
@ -316,13 +305,13 @@ async def test_time_sensor(hass):
assert time_battery.state == "40"
async def test_unsupported_sensor(hass):
async def test_unsupported_sensor(hass, aioclient_mock):
"""Test that unsupported sensors doesn't break anything."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["sensors"] = {
"0": {"type": "not supported", "name": "name", "state": {}, "config": {}}
}
await setup_deconz_integration(hass, get_state_response=data)
await setup_deconz_integration(hass, aioclient_mock, get_state_response=data)
assert len(hass.states.async_all()) == 1