hass-core/homeassistant/components/airq/__init__.py
Renat Sibgatulin be5d6425dc
Add options flow to the airq integration (#109337)
* Add support for options to airq integration

Expose to the user the following configuration:
1. A choice between fetching from the device either:
    a. the averaged (previous and the new default behaviour) or
    b. noisy momentary sensor reading
2. A toggle to clip (spuriously) negative sensor values (default
    functionality, previously unexposed)

To those ends:
- Introduce an `OptionsFlowHandler` alongside with a listener
  `AirQCoordinator.async_set_options`
- Introduce constants to handle represent options
- Add tests and strings

* Drop OptionsFlowHandler in favour of SchemaOptionsFlowHandler

Modify `AirQCoordinator.__init__` to accommodate the change in option
handling, and drop `async_set_options` which slipped through the
previous commit.

* Ruff formatting
2024-05-15 09:13:26 +02:00

45 lines
1.4 KiB
Python

"""The air-Q integration."""
from __future__ import annotations
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from .const import CONF_CLIP_NEGATIVE, CONF_RETURN_AVERAGE
from .coordinator import AirQCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR]
AirQConfigEntry = ConfigEntry[AirQCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: AirQConfigEntry) -> bool:
"""Set up air-Q from a config entry."""
coordinator = AirQCoordinator(
hass,
entry,
clip_negative=entry.options.get(CONF_CLIP_NEGATIVE, True),
return_average=entry.options.get(CONF_RETURN_AVERAGE, True),
)
# Query the device for the first time and initialise coordinator.data
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))
return True
async def async_unload_entry(hass: HomeAssistant, entry: AirQConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)