From 40f1d53475218265f35398658c7487e8152dc4fc Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 23 Dec 2021 15:46:17 +0100 Subject: [PATCH] Add sensor tests to Luftdaten (#62663) --- homeassistant/components/luftdaten/sensor.py | 7 +- tests/components/luftdaten/conftest.py | 2 +- tests/components/luftdaten/test_sensor.py | 118 +++++++++++++++++++ 3 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 tests/components/luftdaten/test_sensor.py diff --git a/homeassistant/components/luftdaten/sensor.py b/homeassistant/components/luftdaten/sensor.py index e0bc64bc918..422e5ed7117 100644 --- a/homeassistant/components/luftdaten/sensor.py +++ b/homeassistant/components/luftdaten/sensor.py @@ -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, ), ) diff --git a/tests/components/luftdaten/conftest.py b/tests/components/luftdaten/conftest.py index 14d8991e41c..248e1344f1b 100644 --- a/tests/components/luftdaten/conftest.py +++ b/tests/components/luftdaten/conftest.py @@ -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, } diff --git a/tests/components/luftdaten/test_sensor.py b/tests/components/luftdaten/test_sensor.py new file mode 100644 index 00000000000..ae016615047 --- /dev/null +++ b/tests/components/luftdaten/test_sensor.py @@ -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