Add sensor tests to Luftdaten (#62663)

This commit is contained in:
Franck Nijhof 2021-12-23 15:46:17 +01:00 committed by GitHub
parent 430cc6194b
commit 40f1d53475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 121 additions and 6 deletions

View File

@ -40,7 +40,6 @@ SENSORS: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="humidity",
name="Humidity",
icon="mdi:water-percent",
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.HUMIDITY,
state_class=SensorStateClass.MEASUREMENT,
@ -48,7 +47,6 @@ SENSORS: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="pressure",
name="Pressure",
icon="mdi:arrow-down-bold",
native_unit_of_measurement=PRESSURE_PA,
device_class=SensorDeviceClass.PRESSURE,
state_class=SensorStateClass.MEASUREMENT,
@ -56,7 +54,6 @@ SENSORS: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="pressure_at_sealevel",
name="Pressure at sealevel",
icon="mdi:download",
native_unit_of_measurement=PRESSURE_PA,
device_class=SensorDeviceClass.PRESSURE,
state_class=SensorStateClass.MEASUREMENT,
@ -64,15 +61,15 @@ SENSORS: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="P1",
name="PM10",
icon="mdi:thought-bubble",
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
device_class=SensorDeviceClass.PM10,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="P2",
name="PM2.5",
icon="mdi:thought-bubble-outline",
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
device_class=SensorDeviceClass.PM25,
state_class=SensorStateClass.MEASUREMENT,
),
)

View File

@ -62,7 +62,7 @@ def mock_luftdaten() -> Generator[None, MagicMock, None]:
"humidity": 34.70,
"P1": 8.5,
"P2": 4.07,
"pressure_at_sea_level": 103102.13,
"pressure_at_sealevel": 103102.13,
"pressure": 98545.00,
"temperature": 22.30,
}

View File

@ -0,0 +1,118 @@
"""Tests for the sensors provided by the Luftdaten integration."""
from homeassistant.components.sensor import (
ATTR_STATE_CLASS,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_FRIENDLY_NAME,
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
PERCENTAGE,
PRESSURE_PA,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry
async def test_luftdaten_sensors(
hass: HomeAssistant,
init_integration: MockConfigEntry,
) -> None:
"""Test the Luftdaten sensors."""
entity_registry = er.async_get(hass)
entry = entity_registry.async_get("sensor.temperature")
assert entry
assert not entry.device_id
assert entry.unique_id == "12345_temperature"
state = hass.states.get("sensor.temperature")
assert state
assert state.state == "22.3"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Temperature"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TEMPERATURE
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS
assert ATTR_ICON not in state.attributes
entry = entity_registry.async_get("sensor.humidity")
assert entry
assert not entry.device_id
assert entry.unique_id == "12345_humidity"
state = hass.states.get("sensor.humidity")
assert state
assert state.state == "34.7"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Humidity"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.HUMIDITY
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
assert ATTR_ICON not in state.attributes
entry = entity_registry.async_get("sensor.pressure")
assert entry
assert not entry.device_id
assert entry.unique_id == "12345_pressure"
state = hass.states.get("sensor.pressure")
assert state
assert state.state == "98545.0"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Pressure"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PA
assert ATTR_ICON not in state.attributes
entry = entity_registry.async_get("sensor.pressure_at_sealevel")
assert entry
assert not entry.device_id
assert entry.unique_id == "12345_pressure_at_sealevel"
state = hass.states.get("sensor.pressure_at_sealevel")
assert state
assert state.state == "103102.13"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Pressure at sealevel"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PA
assert ATTR_ICON not in state.attributes
entry = entity_registry.async_get("sensor.pm10")
assert entry
assert not entry.device_id
assert entry.unique_id == "12345_P1"
state = hass.states.get("sensor.pm10")
assert state
assert state.state == "8.5"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "PM10"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PM10
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
assert (
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
== CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
)
assert ATTR_ICON not in state.attributes
entry = entity_registry.async_get("sensor.pm2_5")
assert entry
assert not entry.device_id
assert entry.unique_id == "12345_P2"
state = hass.states.get("sensor.pm2_5")
assert state
assert state.state == "4.07"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "PM2.5"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PM25
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
assert (
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
== CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
)
assert ATTR_ICON not in state.attributes