Use fixtures in deCONZ sensor tests PT1 (#121116)

This commit is contained in:
Robert Svensson 2024-07-04 11:50:23 +02:00 committed by GitHub
parent 02e7707f91
commit d429bcef16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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,34 +1108,30 @@ 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": {
"1": {
"name": "Sensor 1",
"type": sensor_type,
"state": {sensor_property: None},
"config": {},
"uniqueid": "00:00:00:00:00:00:00:00-00",
}
sensor_payload |= {
"1": {
"name": "Sensor 1",
"type": sensor_type,
"state": {sensor_property: None},
"config": {},
"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,155 +1219,151 @@ 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": {
"1": {
"config": {
"battery": 70,
"heatsetpoint": 2300,
"offset": 0,
"on": True,
"reachable": True,
"schedule": {},
"schedule_on": False,
},
"ep": 1,
"etag": "982d9acc38bee5b251e24a9be26558e4",
"lastseen": "2021-02-15T12:23Z",
"manufacturername": "Danfoss",
"modelid": model_id,
"name": "0x8030",
"state": {
"lastupdated": "2021-02-15T12:23:07.994",
"on": False,
"temperature": 2307,
},
"swversion": "YYYYMMDD",
"type": "ZHAThermostat",
"uniqueid": "58:8e:81:ff:fe:00:11:22-01-0201",
sensor_payload |= {
"1": {
"config": {
"battery": 70,
"heatsetpoint": 2300,
"offset": 0,
"on": True,
"reachable": True,
"schedule": {},
"schedule_on": False,
},
"2": {
"config": {
"battery": 86,
"heatsetpoint": 2300,
"offset": 0,
"on": True,
"reachable": True,
"schedule": {},
"schedule_on": False,
},
"ep": 2,
"etag": "62f12749f9f51c950086aff37dd02b61",
"lastseen": "2021-02-15T12:23Z",
"manufacturername": "Danfoss",
"modelid": model_id,
"name": "0x8030",
"state": {
"lastupdated": "2021-02-15T12:23:22.399",
"on": False,
"temperature": 2316,
},
"swversion": "YYYYMMDD",
"type": "ZHAThermostat",
"uniqueid": "58:8e:81:ff:fe:00:11:22-02-0201",
"ep": 1,
"etag": "982d9acc38bee5b251e24a9be26558e4",
"lastseen": "2021-02-15T12:23Z",
"manufacturername": "Danfoss",
"modelid": model_id,
"name": "0x8030",
"state": {
"lastupdated": "2021-02-15T12:23:07.994",
"on": False,
"temperature": 2307,
},
"3": {
"config": {
"battery": 86,
"heatsetpoint": 2350,
"offset": 0,
"on": True,
"reachable": True,
"schedule": {},
"schedule_on": False,
},
"ep": 3,
"etag": "f50061174bb7f18a3d95789bab8b646d",
"lastseen": "2021-02-15T12:23Z",
"manufacturername": "Danfoss",
"modelid": model_id,
"name": "0x8030",
"state": {
"lastupdated": "2021-02-15T12:23:25.466",
"on": False,
"temperature": 2337,
},
"swversion": "YYYYMMDD",
"type": "ZHAThermostat",
"uniqueid": "58:8e:81:ff:fe:00:11:22-03-0201",
"swversion": "YYYYMMDD",
"type": "ZHAThermostat",
"uniqueid": "58:8e:81:ff:fe:00:11:22-01-0201",
},
"2": {
"config": {
"battery": 86,
"heatsetpoint": 2300,
"offset": 0,
"on": True,
"reachable": True,
"schedule": {},
"schedule_on": False,
},
"4": {
"config": {
"battery": 85,
"heatsetpoint": 2300,
"offset": 0,
"on": True,
"reachable": True,
"schedule": {},
"schedule_on": False,
},
"ep": 4,
"etag": "eea97adf8ce1b971b8b6a3a31793f96b",
"lastseen": "2021-02-15T12:23Z",
"manufacturername": "Danfoss",
"modelid": model_id,
"name": "0x8030",
"state": {
"lastupdated": "2021-02-15T12:23:41.939",
"on": False,
"temperature": 2333,
},
"swversion": "YYYYMMDD",
"type": "ZHAThermostat",
"uniqueid": "58:8e:81:ff:fe:00:11:22-04-0201",
"ep": 2,
"etag": "62f12749f9f51c950086aff37dd02b61",
"lastseen": "2021-02-15T12:23Z",
"manufacturername": "Danfoss",
"modelid": model_id,
"name": "0x8030",
"state": {
"lastupdated": "2021-02-15T12:23:22.399",
"on": False,
"temperature": 2316,
},
"5": {
"config": {
"battery": 83,
"heatsetpoint": 2300,
"offset": 0,
"on": True,
"reachable": True,
"schedule": {},
"schedule_on": False,
},
"ep": 5,
"etag": "1f7cd1a5d66dc27ac5eb44b8c47362fb",
"lastseen": "2021-02-15T12:23Z",
"manufacturername": "Danfoss",
"modelid": model_id,
"name": "0x8030",
"state": {"lastupdated": "none", "on": False, "temperature": 2325},
"swversion": "YYYYMMDD",
"type": "ZHAThermostat",
"uniqueid": "58:8e:81:ff:fe:00:11:22-05-0201",
"swversion": "YYYYMMDD",
"type": "ZHAThermostat",
"uniqueid": "58:8e:81:ff:fe:00:11:22-02-0201",
},
"3": {
"config": {
"battery": 86,
"heatsetpoint": 2350,
"offset": 0,
"on": True,
"reachable": True,
"schedule": {},
"schedule_on": False,
},
}
"ep": 3,
"etag": "f50061174bb7f18a3d95789bab8b646d",
"lastseen": "2021-02-15T12:23Z",
"manufacturername": "Danfoss",
"modelid": model_id,
"name": "0x8030",
"state": {
"lastupdated": "2021-02-15T12:23:25.466",
"on": False,
"temperature": 2337,
},
"swversion": "YYYYMMDD",
"type": "ZHAThermostat",
"uniqueid": "58:8e:81:ff:fe:00:11:22-03-0201",
},
"4": {
"config": {
"battery": 85,
"heatsetpoint": 2300,
"offset": 0,
"on": True,
"reachable": True,
"schedule": {},
"schedule_on": False,
},
"ep": 4,
"etag": "eea97adf8ce1b971b8b6a3a31793f96b",
"lastseen": "2021-02-15T12:23Z",
"manufacturername": "Danfoss",
"modelid": model_id,
"name": "0x8030",
"state": {
"lastupdated": "2021-02-15T12:23:41.939",
"on": False,
"temperature": 2333,
},
"swversion": "YYYYMMDD",
"type": "ZHAThermostat",
"uniqueid": "58:8e:81:ff:fe:00:11:22-04-0201",
},
"5": {
"config": {
"battery": 83,
"heatsetpoint": 2300,
"offset": 0,
"on": True,
"reachable": True,
"schedule": {},
"schedule_on": False,
},
"ep": 5,
"etag": "1f7cd1a5d66dc27ac5eb44b8c47362fb",
"lastseen": "2021-02-15T12:23Z",
"manufacturername": "Danfoss",
"modelid": model_id,
"name": "0x8030",
"state": {"lastupdated": "none", "on": False, "temperature": 2325},
"swversion": "YYYYMMDD",
"type": "ZHAThermostat",
"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