mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Add ozone and sulphur dioxide sensors to HomeKit (#188533)
This commit is contained in:
@@ -373,6 +373,10 @@ def get_accessory( # noqa: C901
|
||||
a_type = "NitrogenDioxideSensor"
|
||||
elif device_class == SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS:
|
||||
a_type = "VolatileOrganicCompoundsSensor"
|
||||
elif device_class == SensorDeviceClass.OZONE:
|
||||
a_type = "OzoneSensor"
|
||||
elif device_class == SensorDeviceClass.SULPHUR_DIOXIDE:
|
||||
a_type = "SulphurDioxideSensor"
|
||||
elif device_class == SensorDeviceClass.GAS:
|
||||
a_type = "AirQualitySensor"
|
||||
elif device_class == SensorDeviceClass.CO:
|
||||
|
||||
@@ -232,6 +232,7 @@ CHAR_OBSTRUCTION_DETECTED = "ObstructionDetected"
|
||||
CHAR_OCCUPANCY_DETECTED = "OccupancyDetected"
|
||||
CHAR_ON = "On"
|
||||
CHAR_OUTLET_IN_USE = "OutletInUse"
|
||||
CHAR_OZONE_DENSITY = "OzoneDensity"
|
||||
CHAR_POSITION_STATE = "PositionState"
|
||||
CHAR_PROGRAMMABLE_SWITCH_EVENT = "ProgrammableSwitchEvent"
|
||||
CHAR_REMAINING_DURATION = "RemainingDuration"
|
||||
@@ -247,6 +248,7 @@ CHAR_SLEEP_DISCOVER_MODE = "SleepDiscoveryMode"
|
||||
CHAR_SMOKE_DETECTED = "SmokeDetected"
|
||||
CHAR_STATUS_LOW_BATTERY = "StatusLowBattery"
|
||||
CHAR_STREAMING_STRATUS = "StreamingStatus"
|
||||
CHAR_SULPHUR_DIOXIDE_DENSITY = "SulphurDioxideDensity"
|
||||
CHAR_SWING_MODE = "SwingMode"
|
||||
CHAR_TARGET_AIR_PURIFIER_STATE = "TargetAirPurifierState"
|
||||
CHAR_TARGET_DOOR_STATE = "TargetDoorState"
|
||||
|
||||
@@ -12,9 +12,16 @@ from homeassistant.const import (
|
||||
STATE_HOME,
|
||||
STATE_ON,
|
||||
EntityStateAttribute,
|
||||
UnitOfDensity,
|
||||
UnitOfRatio,
|
||||
UnitOfTemperature,
|
||||
)
|
||||
from homeassistant.core import State, callback
|
||||
from homeassistant.util.unit_conversion import (
|
||||
BaseUnitConverter,
|
||||
OzoneConcentrationConverter,
|
||||
SulphurDioxideConcentrationConverter,
|
||||
)
|
||||
|
||||
from .accessories import TYPES, HomeAccessory
|
||||
from .const import (
|
||||
@@ -34,9 +41,11 @@ from .const import (
|
||||
CHAR_MOTION_DETECTED,
|
||||
CHAR_NITROGEN_DIOXIDE_DENSITY,
|
||||
CHAR_OCCUPANCY_DETECTED,
|
||||
CHAR_OZONE_DENSITY,
|
||||
CHAR_PM10_DENSITY,
|
||||
CHAR_PM25_DENSITY,
|
||||
CHAR_SMOKE_DETECTED,
|
||||
CHAR_SULPHUR_DIOXIDE_DENSITY,
|
||||
CHAR_VOC_DENSITY,
|
||||
CONF_THRESHOLD_CO,
|
||||
CONF_THRESHOLD_CO2,
|
||||
@@ -63,6 +72,8 @@ from .util import (
|
||||
density_to_air_quality_nitrogen_dioxide,
|
||||
density_to_air_quality_pm10,
|
||||
density_to_air_quality_voc,
|
||||
ppb_to_air_quality_ozone,
|
||||
ppb_to_air_quality_sulphur_dioxide,
|
||||
temperature_to_homekit,
|
||||
)
|
||||
|
||||
@@ -332,6 +343,69 @@ class VolatileOrganicCompoundsSensor(AirQualitySensor):
|
||||
_LOGGER.debug("%s: Set air_quality to %d", self.entity_id, air_quality)
|
||||
|
||||
|
||||
class GasConcentrationSensor(AirQualitySensor):
|
||||
"""Base for gas sensors with a μg/m3 density and a ppb based air quality level.
|
||||
|
||||
Entities without a supported unit are assumed to report μg/m3.
|
||||
"""
|
||||
|
||||
density_char: str
|
||||
converter: type[BaseUnitConverter]
|
||||
ppb_to_air_quality: Callable[[float], int]
|
||||
|
||||
@override
|
||||
def create_services(self) -> None:
|
||||
"""Initialize the air quality service with the gas density."""
|
||||
serv_air_quality = self.add_preload_service(
|
||||
SERV_AIR_QUALITY_SENSOR, [self.density_char]
|
||||
)
|
||||
self.char_quality = serv_air_quality.configure_char(CHAR_AIR_QUALITY, value=0)
|
||||
self.char_density = serv_air_quality.configure_char(self.density_char, value=0)
|
||||
|
||||
@callback
|
||||
@override
|
||||
def async_update_state(self, new_state: State) -> None:
|
||||
"""Update accessory after state change."""
|
||||
value = convert_to_float(new_state.state)
|
||||
if value is None:
|
||||
return
|
||||
unit = new_state.attributes.get(EntityStateAttribute.UNIT_OF_MEASUREMENT)
|
||||
if unit not in self.converter.VALID_UNITS:
|
||||
unit = UnitOfDensity.MICROGRAMS_PER_CUBIC_METER
|
||||
density = round(
|
||||
self.converter.convert(
|
||||
value, unit, UnitOfDensity.MICROGRAMS_PER_CUBIC_METER
|
||||
)
|
||||
)
|
||||
if self.char_density.value != density:
|
||||
self.char_density.set_value(density)
|
||||
_LOGGER.debug("%s: Set density to %d", self.entity_id, density)
|
||||
air_quality = self.ppb_to_air_quality(
|
||||
self.converter.convert(value, unit, UnitOfRatio.PARTS_PER_BILLION)
|
||||
)
|
||||
if self.char_quality.value != air_quality:
|
||||
self.char_quality.set_value(air_quality)
|
||||
_LOGGER.debug("%s: Set air_quality to %d", self.entity_id, air_quality)
|
||||
|
||||
|
||||
@TYPES.register("OzoneSensor")
|
||||
class OzoneSensor(GasConcentrationSensor):
|
||||
"""Generate an OzoneSensor accessory as ozone sensor."""
|
||||
|
||||
density_char = CHAR_OZONE_DENSITY
|
||||
converter = OzoneConcentrationConverter
|
||||
ppb_to_air_quality = staticmethod(ppb_to_air_quality_ozone)
|
||||
|
||||
|
||||
@TYPES.register("SulphurDioxideSensor")
|
||||
class SulphurDioxideSensor(GasConcentrationSensor):
|
||||
"""Generate a SulphurDioxideSensor accessory as sulphur dioxide sensor."""
|
||||
|
||||
density_char = CHAR_SULPHUR_DIOXIDE_DENSITY
|
||||
converter = SulphurDioxideConcentrationConverter
|
||||
ppb_to_air_quality = staticmethod(ppb_to_air_quality_sulphur_dioxide)
|
||||
|
||||
|
||||
@TYPES.register("CarbonMonoxideSensor")
|
||||
class CarbonMonoxideSensor(HomeAccessory):
|
||||
"""Generate a CarbonMonoxidSensor accessory as CO sensor."""
|
||||
|
||||
@@ -604,6 +604,32 @@ def density_to_air_quality_voc(density: float) -> int:
|
||||
return 5 # WHOA IAQ 5 (HomeKit: Poor)
|
||||
|
||||
|
||||
def ppb_to_air_quality_ozone(ppb: float) -> int:
|
||||
"""Map ozone ppb to HomeKit AirQuality level using US EPA 8-hour breakpoints."""
|
||||
if ppb <= 54: # US AQI 0-50 (HomeKit: Excellent)
|
||||
return 1
|
||||
if ppb <= 70: # US AQI 51-100 (HomeKit: Good)
|
||||
return 2
|
||||
if ppb <= 85: # US AQI 101-150 (HomeKit: Fair)
|
||||
return 3
|
||||
if ppb <= 105: # US AQI 151-200 (HomeKit: Inferior)
|
||||
return 4
|
||||
return 5 # US AQI 201+ (HomeKit: Poor)
|
||||
|
||||
|
||||
def ppb_to_air_quality_sulphur_dioxide(ppb: float) -> int:
|
||||
"""Map sulphur dioxide ppb to HomeKit AirQuality level using US EPA 1-hour breakpoints."""
|
||||
if ppb <= 35: # US AQI 0-50 (HomeKit: Excellent)
|
||||
return 1
|
||||
if ppb <= 75: # US AQI 51-100 (HomeKit: Good)
|
||||
return 2
|
||||
if ppb <= 185: # US AQI 101-150 (HomeKit: Fair)
|
||||
return 3
|
||||
if ppb <= 304: # US AQI 151-200 (HomeKit: Inferior)
|
||||
return 4
|
||||
return 5 # US AQI 201+ (HomeKit: Poor)
|
||||
|
||||
|
||||
def get_persist_filename_for_entry_id(entry_id: str) -> str:
|
||||
"""Determine the filename of the homekit state file."""
|
||||
return f"{DOMAIN}.{entry_id}.state"
|
||||
|
||||
@@ -305,6 +305,18 @@ def test_type_media_player(type_name, entity_id, state, attrs, config) -> None:
|
||||
"55",
|
||||
{ATTR_DEVICE_CLASS: SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS},
|
||||
),
|
||||
(
|
||||
"OzoneSensor",
|
||||
"sensor.air_quality_ozone",
|
||||
"40",
|
||||
{ATTR_DEVICE_CLASS: SensorDeviceClass.OZONE},
|
||||
),
|
||||
(
|
||||
"SulphurDioxideSensor",
|
||||
"sensor.air_quality_sulphur_dioxide",
|
||||
"20",
|
||||
{ATTR_DEVICE_CLASS: SensorDeviceClass.SULPHUR_DIOXIDE},
|
||||
),
|
||||
(
|
||||
"CarbonMonoxideSensor",
|
||||
"sensor.co",
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
|
||||
from homeassistant.components.homekit import get_accessory
|
||||
from homeassistant.components.homekit.accessories import HomeDriver
|
||||
from homeassistant.components.homekit.const import (
|
||||
CONF_THRESHOLD_CO,
|
||||
CONF_THRESHOLD_CO2,
|
||||
@@ -17,11 +20,14 @@ from homeassistant.components.homekit.type_sensors import (
|
||||
BinarySensor,
|
||||
CarbonDioxideSensor,
|
||||
CarbonMonoxideSensor,
|
||||
GasConcentrationSensor,
|
||||
HumiditySensor,
|
||||
LightSensor,
|
||||
NitrogenDioxideSensor,
|
||||
OzoneSensor,
|
||||
PM10Sensor,
|
||||
PM25Sensor,
|
||||
SulphurDioxideSensor,
|
||||
TemperatureSensor,
|
||||
VolatileOrganicCompoundsSensor,
|
||||
)
|
||||
@@ -346,6 +352,58 @@ async def test_voc(hass: HomeAssistant, hk_driver) -> None:
|
||||
assert acc.char_quality.value == 5
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("accessory_cls", "unit", "state", "density", "air_quality"),
|
||||
[
|
||||
pytest.param(OzoneSensor, "ppb", "54", 108, 1, id="ozone_excellent"),
|
||||
pytest.param(OzoneSensor, "ppb", "70", 140, 2, id="ozone_good"),
|
||||
pytest.param(OzoneSensor, "ppb", "85", 170, 3, id="ozone_fair"),
|
||||
pytest.param(OzoneSensor, "ppb", "105", 210, 4, id="ozone_inferior"),
|
||||
pytest.param(OzoneSensor, "ppb", "106", 212, 5, id="ozone_poor"),
|
||||
pytest.param(OzoneSensor, "ppm", "0.06", 120, 2, id="ozone_ppm"),
|
||||
pytest.param(OzoneSensor, "μg/m³", "150", 150, 3, id="ozone_ug_m3"),
|
||||
pytest.param(OzoneSensor, None, "100", 100, 1, id="ozone_no_unit"),
|
||||
pytest.param(OzoneSensor, "μg/m³", "2000", 1000, 5, id="ozone_above_max"),
|
||||
pytest.param(SulphurDioxideSensor, "ppb", "35", 93, 1, id="so2_excellent"),
|
||||
pytest.param(SulphurDioxideSensor, "ppb", "75", 200, 2, id="so2_good"),
|
||||
pytest.param(SulphurDioxideSensor, "ppb", "185", 493, 3, id="so2_fair"),
|
||||
pytest.param(SulphurDioxideSensor, "ppb", "304", 810, 4, id="so2_inferior"),
|
||||
pytest.param(SulphurDioxideSensor, "ppb", "305", 812, 5, id="so2_poor"),
|
||||
pytest.param(SulphurDioxideSensor, "μg/m³", "150", 150, 2, id="so2_ug_m3"),
|
||||
pytest.param(
|
||||
SulphurDioxideSensor, "ppm", "150", 150, 2, id="so2_unsupported_unit"
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_gas_concentration_sensor(
|
||||
hass: HomeAssistant,
|
||||
hk_driver: HomeDriver,
|
||||
accessory_cls: type[GasConcentrationSensor],
|
||||
unit: str | None,
|
||||
state: str,
|
||||
density: int,
|
||||
air_quality: int,
|
||||
) -> None:
|
||||
"""Test ozone and sulphur dioxide accessories convert units and map levels."""
|
||||
entity_id = "sensor.air_quality_gas"
|
||||
|
||||
hass.states.async_set(entity_id, STATE_UNKNOWN, {ATTR_UNIT_OF_MEASUREMENT: unit})
|
||||
await hass.async_block_till_done()
|
||||
acc = accessory_cls(hass, hk_driver, "Air Quality", entity_id, 2, None)
|
||||
acc.run()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert acc.aid == 2
|
||||
assert acc.category == 10 # Sensor
|
||||
assert acc.char_density.value == 0
|
||||
assert acc.char_quality.value == 0
|
||||
|
||||
hass.states.async_set(entity_id, state, {ATTR_UNIT_OF_MEASUREMENT: unit})
|
||||
await hass.async_block_till_done()
|
||||
assert acc.char_density.value == density
|
||||
assert acc.char_quality.value == air_quality
|
||||
|
||||
|
||||
async def test_co(hass: HomeAssistant, hk_driver) -> None:
|
||||
"""Test if accessory is updated after state change."""
|
||||
entity_id = "sensor.co"
|
||||
|
||||
Reference in New Issue
Block a user