From b7db65a5d320c6d019c2c29a12805d076e07c2cb Mon Sep 17 00:00:00 2001 From: Alexey Guseynov Date: Mon, 6 May 2024 23:58:58 +0100 Subject: [PATCH 1/3] Add Total Volatile Organic Compounds (tVOC) matter discovery schema. Adds a little bit of missing configuration to propagate tVOC readings from a Matter device to Home Assistant. Tested with SenseCAP indicator with indicator-matter firmware. NOTE: Device needs to be deleted and re-added for the sensor to appear. --- homeassistant/components/matter/sensor.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/homeassistant/components/matter/sensor.py b/homeassistant/components/matter/sensor.py index ff5848ef54e32..4e2644a1ff7ec 100644 --- a/homeassistant/components/matter/sensor.py +++ b/homeassistant/components/matter/sensor.py @@ -219,6 +219,19 @@ def _update_from_device(self) -> None: clusters.CarbonDioxideConcentrationMeasurement.Attributes.MeasuredValue, ), ), + MatterDiscoverySchema( + platform=Platform.SENSOR, + entity_description=MatterSensorEntityDescription( + key="TotalVolatileOrganicCompoundsSensor", + native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, + device_class=SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS, + state_class=SensorStateClass.MEASUREMENT, + ), + entity_class=MatterSensor, + required_attributes=( + clusters.TotalVolatileOrganicCompoundsConcentrationMeasurement.Attributes.MeasuredValue, + ), + ), MatterDiscoverySchema( platform=Platform.SENSOR, entity_description=MatterSensorEntityDescription( From 611e859242b361fda528c7a2063202ad07b370e0 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Thu, 30 May 2024 11:08:08 +0200 Subject: [PATCH 2/3] add tests --- tests/components/matter/test_sensor.py | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/components/matter/test_sensor.py b/tests/components/matter/test_sensor.py index 4ee6180ad772b..83276f19bd4e0 100644 --- a/tests/components/matter/test_sensor.py +++ b/tests/components/matter/test_sensor.py @@ -84,6 +84,16 @@ async def air_quality_sensor_node_fixture( ) +@pytest.fixture(name="air_purifier_node") +async def air_purifier_node_fixture( + hass: HomeAssistant, matter_client: MagicMock +) -> MatterNode: + """Fixture for an air purifier node.""" + return await setup_integration_with_node_fixture( + hass, "air-purifier", matter_client + ) + + # This tests needs to be adjusted to remove lingering tasks @pytest.mark.parametrize("expected_lingering_tasks", [True]) async def test_sensor_null_value( @@ -333,3 +343,47 @@ async def test_air_quality_sensor( state = hass.states.get("sensor.lightfi_aq1_air_quality_sensor_pm10") assert state assert state.state == "50.0" + + +# This tests needs to be adjusted to remove lingering tasks +@pytest.mark.parametrize("expected_lingering_tasks", [True]) +async def test_air_purifier_sensor( + hass: HomeAssistant, + matter_client: MagicMock, + air_purifier_node: MatterNode, +) -> None: + """Test Air quality sensors are creayted for air purifier device.""" + # Carbon Dioxide + state = hass.states.get("sensor.air_purifier_carbon_dioxide") + assert state + assert state.state == "2.0" + + # PM1 + state = hass.states.get("sensor.air_purifier_pm1") + assert state + assert state.state == "2.0" + + # PM2.5 + state = hass.states.get("sensor.air_purifier_pm2_5") + assert state + assert state.state == "2.0" + + # PM10 + state = hass.states.get("sensor.air_purifier_pm10") + assert state + assert state.state == "2.0" + + # Temperature + state = hass.states.get("sensor.air_purifier_temperature") + assert state + assert state.state == "20.0" + + # Humidity + state = hass.states.get("sensor.air_purifier_humidity") + assert state + assert state.state == "50.0" + + # VOCS + state = hass.states.get("sensor.air_purifier_vocs") + assert state + assert state.state == "2.0" From ebcf92870f5e06e06fbd96ebe21944fd29c7be9a Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Thu, 30 May 2024 11:11:53 +0200 Subject: [PATCH 3/3] add some voc specific tests --- tests/components/matter/test_sensor.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/components/matter/test_sensor.py b/tests/components/matter/test_sensor.py index 83276f19bd4e0..42b13e24c9e7d 100644 --- a/tests/components/matter/test_sensor.py +++ b/tests/components/matter/test_sensor.py @@ -387,3 +387,7 @@ async def test_air_purifier_sensor( state = hass.states.get("sensor.air_purifier_vocs") assert state assert state.state == "2.0" + assert state.attributes["state_class"] == "measurement" + assert state.attributes["unit_of_measurement"] == "ppm" + assert state.attributes["device_class"] == "volatile_organic_compounds_parts" + assert state.attributes["friendly_name"] == "Air Purifier VOCs"