* Refactor / update Awair integration This commit does a few things, all in service of making the Awair integration more modern and reliable. Specifically we do the following: - Update to python_awair 0.1.1 - Begin using config entries / flow for setting up the integration. - YAML support is completely removed. - The integration now allows adding multiple Awair accounts, should a user wish to do so (I found it _very_ useful in development). - Group various Awair sensors into devices, using the device registry. - Renames various sensors and treats the "dust" sensor as a particulate sensor. - Device update rate-limits are no longer dynamically calculated; the Awair API now separates rate-limits on a per-device basis. - Supports sound pressure and illuminance sensors found on some Awair devices. - We report the "awair index" for certain sensors as part of device_state_attributes. The "index" is a subjective measure of whether or not a sensor reading is "good" or "bad" (and to what extent). It's a component of the Awair score, and it is useful on its own as an input for those who wish to do things like "display this color if the value is 'bad'". This is a breaking change, and requires updates to documentation and a warning in the README. The breaking changes in detail, are: - Support for all YAML configuration is removed, and users will need to re-add the integration via the UI. - We no longer support overriding device discovery via manual configuration of device UUIDs. This was previously supported because the Awair API had severe limits on the device list endpoints; however those have since been removed. - Gen 1 devices no longer show a "dust" sensor; rather we create a PM2.5 sensor and a PM10 sensor and just keep the values in sync. This better reflects the sensor capabilities: it can detect particles in a range from 2.5 -> 10, but cannot differentiate between sizes. - Sensors are renamed as follows: - "sensor.devicename_co2" -> "sensor.devicename_carbon_dioxide" - "sensor.devicename_voc" -> "sensor.devicename_volatile_organic_compounds" - "sensor.devicename_score" -> "sensor.devicename_air_quality_index" - I've chosen to call the "Awair Score" an "air quality index" sensor, because fundamentally the "Awair Score" and other air quality indices (such as CAQI) do the same thing: they calculate a value based on a variety of other inputs. Under the hood, the integration has seen some improvements: - We use the DataUpdateCoordinator class to handle updates, rather than rolling our own update class. - The code no longer tracks availability based on a timestamp returned from the Awair service; we assert that if we have received a response and the response has data for our device, then we are available (and otherwise, not available). We don't need to test the actual Awair API so heavily. - Test coverage has been expanded to handle a variety of products that Awair produces, not just the one I happen to own. - Test coverage no longer concerns itself with testing behavior that is now handled by the DataUpdateCoordinator; nor is it concerned with ensuring that the overall component sets up and registers properly. These are assumed to be well-tested functionaity of the core and not things we need to re-test ourselves. Finally - between library updates and integration updates, this integration is well-positioned to support future updates. I have a proof-of-concept patch for device automations, and the underlying library now supports subclassing authentication (which clears the way for us to use OAuth authentication for Awair). * Wrap test fixture in mock_coro Truthfully I'm not sure why this was passing on my local dev environment, but I was developing with python 3.8 before. After installing python 3.7, I was able to reproduce the CI failures and fix them. * Fix broken tests after #34901 and/or #34989 * Do not rename sensors so broadly We're going to keep the sensors named as they were before, pending the outcome of any decisions around the air_quality component and what names should be standardized for air-quality-like devices. If standardized names are selected (which does seem likely), then we will update this integration to match them - at which point, it would be a breaking change. But for now, we'll keep names mostly identical to what users had before. Notable in this commit is that we generate the entity_id ourselves, rather than just allowing it to be auto-generated from the name attribute. This allows us to provide more human friendly names, while keeping the old format for entity ids. For example, given an Awair device called "Living Room", we'll generate an entity id of "sensor.living_room_voc" but show set the name of the device to "Living Room Volatile organic compounds". * Support import from config.yaml We'll create a config entry from config.yaml the first time we're loaded, and then defer to it from then on. We ignore all keys other than the access_token, since we no longer need to deal with per-account rate-limits (rather, everything is per-device now). * Add myself to CODEOWNERS Since I wrote the initial integration, and now this re-write, it feels appropriate for me to take care of the integration along with `danielsjf`. * Remove name mangling * Update homeassistant/components/awair/manifest.json Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/awair/config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/awair/sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/awair/sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Address some review feedback * Set up reauth flow in a job, rather than awaiting * Remove unnecessary title string * Remove unnecessary config schema checking As pointed out in review, because this comes in via import from `configuration.yaml`, we can rely on the `PLATFORM_SCHEMA` validation instead. * Fix tests * Set unique_id appropriately for legacy devices For users who have had this integration already installed (and who have updated their home assistant installation sometime in recent history), we want to ensure that unique_id's are set to the same thing as before, to facilitate the upgrade process. To do that, we add an additional property to the `SENSOR_TYPES` dict (`ATTR_UNIQUE_ID`) which allows us to map modern sensor names from python_awair to what older versions called them - ie: `humidity` -> `HUMID`. We then use that value when constructing the unique ID. This should allow users to upgrade and not lose configuration even if entity IDs would otherwise change (because we have changed the name format that generates entity IDs). One note is that for the gen1 `DUST` sensor, we have to treat it differently. This integration used to call that a "PM2.5" sensor, but the unique_id generated would be something like `awair_12345_DUST`. So we special-case that sensor, and do the same thing. We do not need to special-case the PM10 sensor for gen1 devices, because we didn't create a PM10 sensor in the past (we do now, because the "DUST" sensor is really a hybrid PM2.5/PM10 sensor). * Patch async_setup_entry for two tests * Update awair config_flow to require / use an email address for unique_id Also, only start one re-auth flow. * Add a few more tests, try to get coverage up. * Add another test * Move attribution to device_state_attributes * Don't require email * Switch from Union[dict, None] to Optional[dict] * Use a mock where requested * Fix missing constant rename * Use async_create_task * Bump test coverage a bit for config_flow * s/CONF_UNIQUE_ID/UNIQUE_ID/g * Add warning about deprecated platform config Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
245 lines
8.3 KiB
Python
245 lines
8.3 KiB
Python
"""Support for Awair sensors."""
|
|
|
|
from typing import Callable, List, Optional
|
|
|
|
from python_awair.devices import AwairDevice
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components.awair import AwairDataUpdateCoordinator, AwairResult
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
from homeassistant.config_entries import SOURCE_IMPORT
|
|
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_DEVICE_CLASS, CONF_ACCESS_TOKEN
|
|
from homeassistant.helpers import device_registry as dr
|
|
import homeassistant.helpers.config_validation as cv
|
|
from homeassistant.helpers.entity import Entity
|
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
|
|
|
from .const import (
|
|
API_DUST,
|
|
API_PM25,
|
|
API_SCORE,
|
|
API_TEMP,
|
|
API_VOC,
|
|
ATTR_ICON,
|
|
ATTR_LABEL,
|
|
ATTR_UNIQUE_ID,
|
|
ATTR_UNIT,
|
|
ATTRIBUTION,
|
|
DOMAIN,
|
|
DUST_ALIASES,
|
|
LOGGER,
|
|
SENSOR_TYPES,
|
|
)
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
{vol.Required(CONF_ACCESS_TOKEN): cv.string}, extra=vol.ALLOW_EXTRA,
|
|
)
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
"""Import Awair configuration from YAML."""
|
|
LOGGER.warning(
|
|
"Loading Awair via platform setup is deprecated. Please remove it from your configuration."
|
|
)
|
|
hass.async_create_task(
|
|
hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=config,
|
|
)
|
|
)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistantType,
|
|
config_entry: ConfigType,
|
|
async_add_entities: Callable[[List[Entity], bool], None],
|
|
):
|
|
"""Set up Awair sensor entity based on a config entry."""
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
sensors = []
|
|
|
|
data: List[AwairResult] = coordinator.data.values()
|
|
for result in data:
|
|
if result.air_data:
|
|
sensors.append(AwairSensor(API_SCORE, result.device, coordinator))
|
|
device_sensors = result.air_data.sensors.keys()
|
|
for sensor in device_sensors:
|
|
if sensor in SENSOR_TYPES:
|
|
sensors.append(AwairSensor(sensor, result.device, coordinator))
|
|
|
|
# The "DUST" sensor for Awair is a combo pm2.5/pm10 sensor only
|
|
# present on first-gen devices in lieu of separate pm2.5/pm10 sensors.
|
|
# We handle that by creating fake pm2.5/pm10 sensors that will always
|
|
# report identical values, and we let users decide how they want to use
|
|
# that data - because we can't really tell what kind of particles the
|
|
# "DUST" sensor actually detected. However, it's still useful data.
|
|
if API_DUST in device_sensors:
|
|
for alias_kind in DUST_ALIASES:
|
|
sensors.append(AwairSensor(alias_kind, result.device, coordinator))
|
|
|
|
async_add_entities(sensors)
|
|
|
|
|
|
class AwairSensor(Entity):
|
|
"""Defines an Awair sensor entity."""
|
|
|
|
def __init__(
|
|
self, kind: str, device: AwairDevice, coordinator: AwairDataUpdateCoordinator,
|
|
) -> None:
|
|
"""Set up an individual AwairSensor."""
|
|
self._kind = kind
|
|
self._device = device
|
|
self._coordinator = coordinator
|
|
|
|
@property
|
|
def should_poll(self) -> bool:
|
|
"""Return the polling requirement of the entity."""
|
|
return False
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
"""Return the name of the sensor."""
|
|
name = SENSOR_TYPES[self._kind][ATTR_LABEL]
|
|
if self._device.name:
|
|
name = f"{self._device.name} {name}"
|
|
|
|
return name
|
|
|
|
@property
|
|
def unique_id(self) -> str:
|
|
"""Return the uuid as the unique_id."""
|
|
unique_id_tag = SENSOR_TYPES[self._kind][ATTR_UNIQUE_ID]
|
|
|
|
# This integration used to create a sensor that was labelled as a "PM2.5"
|
|
# sensor for first-gen Awair devices, but its unique_id reflected the truth:
|
|
# under the hood, it was a "DUST" sensor. So we preserve that specific unique_id
|
|
# for users with first-gen devices that are upgrading.
|
|
if self._kind == API_PM25 and API_DUST in self._air_data.sensors:
|
|
unique_id_tag = "DUST"
|
|
|
|
return f"{self._device.uuid}_{unique_id_tag}"
|
|
|
|
@property
|
|
def available(self) -> bool:
|
|
"""Determine if the sensor is available based on API results."""
|
|
# If the last update was successful...
|
|
if self._coordinator.last_update_success and self._air_data:
|
|
# and the results included our sensor type...
|
|
if self._kind in self._air_data.sensors:
|
|
# then we are available.
|
|
return True
|
|
|
|
# or, we're a dust alias
|
|
if self._kind in DUST_ALIASES and API_DUST in self._air_data.sensors:
|
|
return True
|
|
|
|
# or we are API_SCORE
|
|
if self._kind == API_SCORE:
|
|
# then we are available.
|
|
return True
|
|
|
|
# Otherwise, we are not.
|
|
return False
|
|
|
|
@property
|
|
def state(self) -> float:
|
|
"""Return the state, rounding off to reasonable values."""
|
|
state: float
|
|
|
|
# Special-case for "SCORE", which we treat as the AQI
|
|
if self._kind == API_SCORE:
|
|
state = self._air_data.score
|
|
elif self._kind in DUST_ALIASES and API_DUST in self._air_data.sensors:
|
|
state = self._air_data.sensors.dust
|
|
else:
|
|
state = self._air_data.sensors[self._kind]
|
|
|
|
if self._kind == API_VOC or self._kind == API_SCORE:
|
|
return round(state)
|
|
|
|
if self._kind == API_TEMP:
|
|
return round(state, 1)
|
|
|
|
return round(state, 2)
|
|
|
|
@property
|
|
def icon(self) -> str:
|
|
"""Return the icon."""
|
|
return SENSOR_TYPES[self._kind][ATTR_ICON]
|
|
|
|
@property
|
|
def device_class(self) -> str:
|
|
"""Return the device_class."""
|
|
return SENSOR_TYPES[self._kind][ATTR_DEVICE_CLASS]
|
|
|
|
@property
|
|
def unit_of_measurement(self) -> str:
|
|
"""Return the unit the value is expressed in."""
|
|
return SENSOR_TYPES[self._kind][ATTR_UNIT]
|
|
|
|
@property
|
|
def device_state_attributes(self) -> dict:
|
|
"""Return the Awair Index alongside state attributes.
|
|
|
|
The Awair Index is a subjective score ranging from 0-4 (inclusive) that
|
|
is is used by the Awair app when displaying the relative "safety" of a
|
|
given measurement. Each value is mapped to a color indicating the safety:
|
|
|
|
0: green
|
|
1: yellow
|
|
2: light-orange
|
|
3: orange
|
|
4: red
|
|
|
|
The API indicates that both positive and negative values may be returned,
|
|
but the negative values are mapped to identical colors as the positive values.
|
|
Knowing that, we just return the absolute value of a given index so that
|
|
users don't have to handle positive/negative values that ultimately "mean"
|
|
the same thing.
|
|
|
|
https://docs.developer.getawair.com/?version=latest#awair-score-and-index
|
|
"""
|
|
attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
if self._kind in self._air_data.indices:
|
|
attrs["awair_index"] = abs(self._air_data.indices[self._kind])
|
|
elif self._kind in DUST_ALIASES and API_DUST in self._air_data.indices:
|
|
attrs["awair_index"] = abs(self._air_data.indices.dust)
|
|
|
|
return attrs
|
|
|
|
@property
|
|
def device_info(self) -> dict:
|
|
"""Device information."""
|
|
info = {
|
|
"identifiers": {(DOMAIN, self._device.uuid)},
|
|
"manufacturer": "Awair",
|
|
"model": self._device.model,
|
|
}
|
|
|
|
if self._device.name:
|
|
info["name"] = self._device.name
|
|
|
|
if self._device.mac_address:
|
|
info["connections"] = {
|
|
(dr.CONNECTION_NETWORK_MAC, self._device.mac_address)
|
|
}
|
|
|
|
return info
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
"""Connect to dispatcher listening for entity data notifications."""
|
|
self.async_on_remove(
|
|
self._coordinator.async_add_listener(self.async_write_ha_state)
|
|
)
|
|
|
|
async def async_update(self) -> None:
|
|
"""Update Awair entity."""
|
|
await self._coordinator.async_request_refresh()
|
|
|
|
@property
|
|
def _air_data(self) -> Optional[AwairResult]:
|
|
"""Return the latest data for our device, or None."""
|
|
result: Optional[AwairResult] = self._coordinator.data.get(self._device.uuid)
|
|
if result:
|
|
return result.air_data
|
|
|
|
return None
|