Use fixtures in deCONZ sensor tests PT1 (#121116)
This commit is contained in:
parent
02e7707f91
commit
d429bcef16
1 changed files with 190 additions and 205 deletions
|
@ -1,6 +1,8 @@
|
|||
"""deCONZ sensor platform tests."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
@ -11,7 +13,7 @@ from homeassistant.components.sensor import (
|
|||
SensorDeviceClass,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY
|
||||
from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY, ConfigEntry
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
|
@ -982,12 +984,10 @@ async def test_sensors(
|
|||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_not_allow_clip_sensor(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test that CLIP sensors are not allowed."""
|
||||
data = {
|
||||
"sensors": {
|
||||
@pytest.mark.parametrize(
|
||||
"sensor_payload",
|
||||
[
|
||||
{
|
||||
"1": {
|
||||
"name": "CLIP temperature sensor",
|
||||
"type": "CLIPTemperature",
|
||||
|
@ -996,22 +996,19 @@ async def test_not_allow_clip_sensor(
|
|||
"uniqueid": "00:00:00:00:00:00:00:02-00",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||
await setup_deconz_integration(
|
||||
hass, aioclient_mock, options={CONF_ALLOW_CLIP_SENSOR: False}
|
||||
)
|
||||
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("config_entry_options", [{CONF_ALLOW_CLIP_SENSOR: False}])
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_not_allow_clip_sensor(hass: HomeAssistant) -> None:
|
||||
"""Test that CLIP sensors are not allowed."""
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_allow_clip_sensors(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test that CLIP sensors can be allowed."""
|
||||
data = {
|
||||
"sensors": {
|
||||
@pytest.mark.parametrize(
|
||||
"sensor_payload",
|
||||
[
|
||||
{
|
||||
"1": {
|
||||
"name": "Light level sensor",
|
||||
"type": "ZHALightLevel",
|
||||
|
@ -1039,14 +1036,13 @@ async def test_allow_clip_sensors(
|
|||
"uniqueid": "/sensors/3",
|
||||
},
|
||||
}
|
||||
}
|
||||
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||
config_entry = await setup_deconz_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={CONF_ALLOW_CLIP_SENSOR: True},
|
||||
)
|
||||
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("config_entry_options", [{CONF_ALLOW_CLIP_SENSOR: True}])
|
||||
async def test_allow_clip_sensors(
|
||||
hass: HomeAssistant, config_entry_setup: ConfigEntry
|
||||
) -> None:
|
||||
"""Test that CLIP sensors can be allowed."""
|
||||
assert len(hass.states.async_all()) == 4
|
||||
assert hass.states.get("sensor.clip_light_level_sensor").state == "999.8"
|
||||
assert hass.states.get("sensor.clip_flur").state == "0"
|
||||
|
@ -1054,7 +1050,7 @@ async def test_allow_clip_sensors(
|
|||
# Disallow clip sensors
|
||||
|
||||
hass.config_entries.async_update_entry(
|
||||
config_entry, options={CONF_ALLOW_CLIP_SENSOR: False}
|
||||
config_entry_setup, options={CONF_ALLOW_CLIP_SENSOR: False}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -1065,7 +1061,7 @@ async def test_allow_clip_sensors(
|
|||
# Allow clip sensors
|
||||
|
||||
hass.config_entries.async_update_entry(
|
||||
config_entry, options={CONF_ALLOW_CLIP_SENSOR: True}
|
||||
config_entry_setup, options={CONF_ALLOW_CLIP_SENSOR: True}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -1074,9 +1070,8 @@ async def test_allow_clip_sensors(
|
|||
assert hass.states.get("sensor.clip_flur").state == "0"
|
||||
|
||||
|
||||
async def test_add_new_sensor(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_deconz_websocket
|
||||
) -> None:
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_add_new_sensor(hass: HomeAssistant, mock_deconz_websocket) -> None:
|
||||
"""Test that adding a new sensor works."""
|
||||
event_added_sensor = {
|
||||
"t": "event",
|
||||
|
@ -1093,8 +1088,6 @@ async def test_add_new_sensor(
|
|||
},
|
||||
}
|
||||
|
||||
await setup_deconz_integration(hass, aioclient_mock)
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
await mock_deconz_websocket(data=event_added_sensor)
|
||||
|
@ -1115,13 +1108,13 @@ BAD_SENSOR_DATA = [
|
|||
@pytest.mark.parametrize(("sensor_type", "sensor_property"), BAD_SENSOR_DATA)
|
||||
async def test_dont_add_sensor_if_state_is_none(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
sensor_type,
|
||||
sensor_property,
|
||||
config_entry_factory: Callable[[], ConfigEntry],
|
||||
sensor_payload: dict[str, Any],
|
||||
sensor_type: str,
|
||||
sensor_property: str,
|
||||
) -> None:
|
||||
"""Test sensor with scaled data is not created if state is None."""
|
||||
data = {
|
||||
"sensors": {
|
||||
sensor_payload |= {
|
||||
"1": {
|
||||
"name": "Sensor 1",
|
||||
"type": sensor_type,
|
||||
|
@ -1130,19 +1123,15 @@ async def test_dont_add_sensor_if_state_is_none(
|
|||
"uniqueid": "00:00:00:00:00:00:00:00-00",
|
||||
}
|
||||
}
|
||||
}
|
||||
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||
await setup_deconz_integration(hass, aioclient_mock)
|
||||
await config_entry_factory()
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_air_quality_sensor_without_ppb(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test sensor with scaled data is not created if state is None."""
|
||||
data = {
|
||||
"sensors": {
|
||||
@pytest.mark.parametrize(
|
||||
"sensor_payload",
|
||||
[
|
||||
{
|
||||
"1": {
|
||||
"config": {
|
||||
"on": True,
|
||||
|
@ -1163,23 +1152,18 @@ async def test_air_quality_sensor_without_ppb(
|
|||
"uniqueid": "00:00:00:00:00:00:00:00-02-fdef",
|
||||
}
|
||||
}
|
||||
}
|
||||
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||
await setup_deconz_integration(hass, aioclient_mock)
|
||||
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_air_quality_sensor_without_ppb(hass: HomeAssistant) -> None:
|
||||
"""Test sensor with scaled data is not created if state is None."""
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
|
||||
async def test_add_battery_later(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_deconz_websocket
|
||||
) -> None:
|
||||
"""Test that a battery sensor can be created later on.
|
||||
|
||||
Without an initial battery state a battery sensor
|
||||
can be created once a value is reported.
|
||||
"""
|
||||
data = {
|
||||
"sensors": {
|
||||
@pytest.mark.parametrize(
|
||||
"sensor_payload",
|
||||
[
|
||||
{
|
||||
"1": {
|
||||
"name": "Switch 1",
|
||||
"type": "ZHASwitch",
|
||||
|
@ -1195,10 +1179,15 @@ async def test_add_battery_later(
|
|||
"uniqueid": "00:00:00:00:00:00:00:00-00-0001",
|
||||
},
|
||||
}
|
||||
}
|
||||
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||
await setup_deconz_integration(hass, aioclient_mock)
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_add_battery_later(hass: HomeAssistant, mock_deconz_websocket) -> None:
|
||||
"""Test that a battery sensor can be created later on.
|
||||
|
||||
Without an initial battery state a battery sensor
|
||||
can be created once a value is reported.
|
||||
"""
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
event_changed_sensor = {
|
||||
|
@ -1230,15 +1219,17 @@ async def test_add_battery_later(
|
|||
|
||||
@pytest.mark.parametrize("model_id", ["0x8030", "0x8031", "0x8034", "0x8035"])
|
||||
async def test_special_danfoss_battery_creation(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, model_id
|
||||
hass: HomeAssistant,
|
||||
config_entry_factory: Callable[[], ConfigEntry],
|
||||
sensor_payload: dict[str, Any],
|
||||
model_id: str,
|
||||
) -> None:
|
||||
"""Test the special Danfoss battery creation works.
|
||||
|
||||
Normally there should only be one battery sensor per device from deCONZ.
|
||||
With specific Danfoss devices each endpoint can report its own battery state.
|
||||
"""
|
||||
data = {
|
||||
"sensors": {
|
||||
sensor_payload |= {
|
||||
"1": {
|
||||
"config": {
|
||||
"battery": 70,
|
||||
|
@ -1361,24 +1352,18 @@ async def test_special_danfoss_battery_creation(
|
|||
"uniqueid": "58:8e:81:ff:fe:00:11:22-05-0201",
|
||||
},
|
||||
}
|
||||
}
|
||||
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||
await setup_deconz_integration(hass, aioclient_mock)
|
||||
|
||||
await config_entry_factory()
|
||||
|
||||
assert len(hass.states.async_all()) == 10
|
||||
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 5
|
||||
|
||||
|
||||
async def test_unsupported_sensor(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
@pytest.mark.parametrize(
|
||||
"sensor_payload",
|
||||
[{"0": {"type": "not supported", "name": "name", "state": {}, "config": {}}}],
|
||||
)
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_unsupported_sensor(hass: HomeAssistant) -> None:
|
||||
"""Test that unsupported sensors doesn't break anything."""
|
||||
data = {
|
||||
"sensors": {
|
||||
"0": {"type": "not supported", "name": "name", "state": {}, "config": {}}
|
||||
}
|
||||
}
|
||||
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||
await setup_deconz_integration(hass, aioclient_mock)
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
|
Loading…
Add table
Reference in a new issue