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