-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add Indoor Air Quality integration #169552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liudger
wants to merge
15
commits into
home-assistant:dev
Choose a base branch
from
liudger:add-indoor-air-quality
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a281c2b
Add Indoor Air Quality integration
liudger 537e5f0
Add tests for the Indoor Air Quality integration
liudger 7e32b78
Add state reset functionality for Indoor Air Quality controller
liudger 822c815
Enhance entry data validation for Indoor Air Quality integration
liudger 820a99f
Add CODEOWNERS entry for Indoor Air Quality tests
liudger 285bcd3
Refactor Indoor Air Quality integration configuration and remove unus…
liudger 3eaf64c
Improve logging for missing entities in Indoor Air Quality integration
liudger 9da8f69
Fix listener removal to prevent KeyError in Indoor Air Quality contro…
liudger d3fcbda
Clarify comments on parallel updates in Indoor Air Quality integration
liudger f4957b8
Enhance unit definitions for Indoor Air Quality integration by using …
liudger 4c8cc23
Enhance entity_ids_from_sources to return a de-duplicated list of ent…
liudger b541bdd
Add tests for unit conversion aliases in Indoor Air Quality integration
liudger 32f90da
Add validation for known sources in Indoor Air Quality integration
liudger a5d3140
Refactor source handling in Indoor Air Quality config flow and add te…
liudger e3933da
Clarify IAQ score normalization and enhance cross-family conversion d…
liudger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
116 changes: 116 additions & 0 deletions
116
homeassistant/components/indoor_air_quality/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """The Indoor Air Quality integration. | ||
|
|
||
| Calculates an Indoor Air Quality (IAQ) index from a configurable set of | ||
| sensor sources, using a configurable rating standard. | ||
| """ | ||
|
|
||
| import logging | ||
| from typing import Any, Final, cast | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import CONF_DEVICE_ID, Platform | ||
| from homeassistant.core import Event, HomeAssistant, callback | ||
| from homeassistant.exceptions import ConfigEntryError | ||
| from homeassistant.helpers.event import ( | ||
| EventStateChangedData, | ||
| async_track_state_change_event, | ||
| ) | ||
|
|
||
| from .const import CONF_SOURCES, CONF_STANDARD, DEFAULT_STANDARD, STANDARDS | ||
| from .coordinator import IndoorAirQualityController | ||
| from .helpers import entity_ids_from_sources | ||
|
|
||
| _LOGGER: Final = logging.getLogger(__name__) | ||
|
|
||
| PLATFORMS: Final = [Platform.SENSOR] | ||
|
|
||
|
|
||
| _ENTRY_DATA_SCHEMA: Final = vol.Schema( | ||
| { | ||
| vol.Required(CONF_SOURCES): vol.All( | ||
| vol.Schema({str: vol.Any(str, [str])}), vol.Length(min=1) | ||
| ), | ||
| vol.Optional(CONF_STANDARD, default=DEFAULT_STANDARD): vol.In(STANDARDS), | ||
| vol.Optional(CONF_DEVICE_ID): vol.Any(str, None), | ||
| }, | ||
| extra=vol.ALLOW_EXTRA, | ||
| ) | ||
|
|
||
|
|
||
| type IndoorAirQualityConfigEntry = ConfigEntry[IndoorAirQualityController] | ||
|
|
||
|
|
||
| def _validate_entry_data(entry: IndoorAirQualityConfigEntry) -> dict[str, Any]: | ||
| """Validate ``entry.data``; raise :class:`ConfigEntryError` on failure.""" | ||
| try: | ||
| validated = _ENTRY_DATA_SCHEMA(dict(entry.data)) | ||
| except vol.Invalid as err: | ||
| raise ConfigEntryError( | ||
| f"Invalid Indoor Air Quality config entry data: {err}" | ||
| ) from err | ||
| return cast(dict[str, Any], validated) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, entry: IndoorAirQualityConfigEntry | ||
| ) -> bool: | ||
| """Set up Indoor Air Quality from a config entry.""" | ||
| data = _validate_entry_data(entry) | ||
| sources: dict[str, str | list[str]] = data[CONF_SOURCES] | ||
| device_id: str | None = data.get(CONF_DEVICE_ID) | ||
| standard: str = data[CONF_STANDARD] | ||
|
|
||
| _LOGGER.debug( | ||
| "Initialize controller %s (standard=%s) for sources: %s", | ||
| entry.entry_id, | ||
| standard, | ||
| ", ".join(f"{key}={value}" for key, value in sources.items()), | ||
| ) | ||
|
|
||
| controller = IndoorAirQualityController( | ||
| hass, entry.entry_id, entry.title, sources, device_id, standard=standard | ||
| ) | ||
| entry.runtime_data = controller | ||
|
|
||
| @callback | ||
| def _handle_state_change(event: Event[EventStateChangedData]) -> None: | ||
| """Recalculate the IAQ index and notify subscribed entities.""" | ||
| controller.update() | ||
| controller.async_update_listeners() | ||
|
|
||
| entity_ids = entity_ids_from_sources(sources) | ||
| if entity_ids: | ||
| entry.async_on_unload( | ||
| async_track_state_change_event(hass, entity_ids, _handle_state_change) | ||
| ) | ||
|
|
||
| # Compute initial state. | ||
| controller.update() | ||
|
|
||
| await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) | ||
| entry.async_on_unload(entry.add_update_listener(_async_reload_entry)) | ||
| return True | ||
|
|
||
|
|
||
| async def async_unload_entry( | ||
| hass: HomeAssistant, entry: IndoorAirQualityConfigEntry | ||
| ) -> bool: | ||
| """Unload a config entry.""" | ||
| return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) | ||
|
|
||
|
|
||
| async def _async_reload_entry( | ||
| hass: HomeAssistant, entry: IndoorAirQualityConfigEntry | ||
| ) -> None: | ||
| """Reload config entry on options/data change.""" | ||
| await hass.config_entries.async_reload(entry.entry_id) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "IndoorAirQualityConfigEntry", | ||
| "IndoorAirQualityController", | ||
| "async_setup_entry", | ||
| "async_unload_entry", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| """Index band tables and scoring for the Indoor Air Quality integration. | ||
|
|
||
| Adding a new rating standard is purely additive: define its per-source band | ||
| tuple, its level cut-offs and its top level, and register it in ``BANDS``, | ||
| ``LEVEL_BANDS`` and ``LEVEL_TOP``. | ||
| """ | ||
|
|
||
| from typing import Final, NamedTuple | ||
|
|
||
| from .const import ( | ||
| CONF_CO, | ||
| CONF_CO2, | ||
| CONF_HCHO, | ||
| CONF_HUMIDITY, | ||
| CONF_NO2, | ||
| CONF_PM, | ||
| CONF_RADON, | ||
| CONF_TEMPERATURE, | ||
| CONF_TVOC, | ||
| CONF_VOC_INDEX, | ||
| LEVEL_EXCELLENT, | ||
| LEVEL_FAIR, | ||
| LEVEL_GOOD, | ||
| LEVEL_INADEQUATE, | ||
| LEVEL_POOR, | ||
| STANDARD_UK, | ||
| ) | ||
|
|
||
|
|
||
| class ScoreBand(NamedTuple): | ||
| """Range -> score mapping used by the index calculator. | ||
|
|
||
| A value matches a band when it satisfies both the optional lower and | ||
| upper bound. ``low_strict`` / ``high_strict`` switch the corresponding | ||
| bound between ``<=`` (default) and ``<``. | ||
| """ | ||
|
|
||
| score: int | ||
| low: float | None = None | ||
| high: float | None = None | ||
| low_strict: bool = False | ||
| high_strict: bool = False | ||
|
|
||
|
|
||
| def score_from_bands(value: float, bands: tuple[ScoreBand, ...]) -> int: | ||
| """Return the score for the first matching band, defaulting to 1.""" | ||
| for band in bands: | ||
| if band.low is not None: | ||
| if band.low_strict: | ||
| if not band.low < value: | ||
| continue | ||
| elif not band.low <= value: | ||
| continue | ||
| if band.high is not None: | ||
| if band.high_strict: | ||
| if not value < band.high: | ||
| continue | ||
| elif not value <= band.high: | ||
| continue | ||
| return band.score | ||
| return 1 | ||
|
|
||
|
|
||
| _BANDS_UK: Final[dict[str, tuple[ScoreBand, ...]]] = { | ||
| CONF_TEMPERATURE: ( | ||
| ScoreBand(5, low=18, high=21), | ||
| ScoreBand(4, low=16, high=23, low_strict=True, high_strict=True), | ||
| ScoreBand(3, low=15, high=24, low_strict=True, high_strict=True), | ||
| ScoreBand(2, low=14, high=25, low_strict=True, high_strict=True), | ||
| ), | ||
| CONF_HUMIDITY: ( | ||
| ScoreBand(5, low=40, high=60), | ||
| ScoreBand(4, low=30, high=70), | ||
| ScoreBand(3, low=20, high=80), | ||
| ScoreBand(2, low=10, high=90), | ||
| ), | ||
| CONF_CO2: ( | ||
| ScoreBand(5, high=600, high_strict=True), | ||
| ScoreBand(4, high=800), | ||
| ScoreBand(3, high=1500), | ||
| ScoreBand(2, high=1800), | ||
| ), | ||
| CONF_TVOC: ( | ||
| ScoreBand(5, high=0.1, high_strict=True), | ||
| ScoreBand(4, high=0.3), | ||
| ScoreBand(3, high=0.5), | ||
| ScoreBand(2, high=1.0), | ||
| ), | ||
| CONF_VOC_INDEX: ( | ||
| ScoreBand(5, high=50), | ||
| ScoreBand(4, high=115), | ||
| ScoreBand(3, high=180), | ||
| ScoreBand(2, high=260), | ||
| ), | ||
| CONF_PM: ( | ||
| ScoreBand(5, high=23), | ||
| ScoreBand(4, high=41), | ||
| ScoreBand(3, high=53), | ||
| ScoreBand(2, high=64), | ||
| ), | ||
| CONF_NO2: ( | ||
| ScoreBand(5, high=200, high_strict=True), | ||
| ScoreBand(3, high=400), | ||
| ), | ||
| CONF_CO: ( | ||
| ScoreBand(5, low=0, high=0), | ||
| ScoreBand(3, high=7), | ||
| ), | ||
| CONF_HCHO: ( | ||
| ScoreBand(5, high=20, high_strict=True), | ||
| ScoreBand(4, high=50), | ||
| ScoreBand(3, high=100), | ||
| ScoreBand(2, high=200), | ||
| ), | ||
| CONF_RADON: ( | ||
| ScoreBand(5, low=0, high=0), | ||
| ScoreBand(3, high=20, high_strict=True), | ||
| ScoreBand(2, high=100), | ||
| ), | ||
| } | ||
|
|
||
| # IAQ score (5 per source, normalized to 0-65) -> human level. | ||
|
liudger marked this conversation as resolved.
Outdated
|
||
| _LEVEL_BANDS_UK: Final[tuple[tuple[int, str], ...]] = ( | ||
| (25, LEVEL_INADEQUATE), | ||
| (38, LEVEL_POOR), | ||
| (51, LEVEL_FAIR), | ||
| (60, LEVEL_GOOD), | ||
| ) | ||
|
|
||
| BANDS: Final[dict[str, dict[str, tuple[ScoreBand, ...]]]] = { | ||
| STANDARD_UK: _BANDS_UK, | ||
| } | ||
|
|
||
| LEVEL_BANDS: Final[dict[str, tuple[tuple[int, str], ...]]] = { | ||
| STANDARD_UK: _LEVEL_BANDS_UK, | ||
| } | ||
|
|
||
| LEVEL_TOP: Final[dict[str, str]] = { | ||
| STANDARD_UK: LEVEL_EXCELLENT, | ||
| } | ||
|
|
||
|
|
||
| def level_for_index(standard: str, iaq_index: int) -> str: | ||
| """Return the human readable level for an IAQ index value.""" | ||
| for upper, level in LEVEL_BANDS[standard]: | ||
| if iaq_index <= upper: | ||
| return level | ||
| return LEVEL_TOP[standard] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.