core/homeassistant/components/awair/__init__.py

155 lines
5.5 KiB
Python
Raw Normal View History

Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
"""The awair component."""
2021-03-17 22:34:25 +00:00
from __future__ import annotations
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
from asyncio import gather
from datetime import timedelta
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
from aiohttp import ClientSession
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
from async_timeout import timeout
2022-08-11 13:01:35 +00:00
from python_awair import Awair, AwairLocal
from python_awair.devices import AwairBaseDevice, AwairLocalDevice
from python_awair.exceptions import AuthError, AwairError
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
from homeassistant.config_entries import ConfigEntry
2022-08-11 13:01:35 +00:00
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
2022-08-11 13:01:35 +00:00
from .const import (
API_TIMEOUT,
DOMAIN,
LOGGER,
UPDATE_INTERVAL_CLOUD,
UPDATE_INTERVAL_LOCAL,
AwairResult,
)
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
PLATFORMS = [Platform.SENSOR]
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
"""Set up Awair integration from a config entry."""
session = async_get_clientsession(hass)
2022-08-11 13:01:35 +00:00
coordinator: AwairDataUpdateCoordinator
if CONF_HOST in config_entry.data:
coordinator = AwairLocalDataUpdateCoordinator(hass, config_entry, session)
config_entry.async_on_unload(
config_entry.add_update_listener(_async_update_listener)
)
2022-08-11 13:01:35 +00:00
else:
coordinator = AwairCloudDataUpdateCoordinator(hass, config_entry, session)
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
await coordinator.async_config_entry_first_refresh()
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][config_entry.entry_id] = coordinator
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
return True
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
coordinator: AwairLocalDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
if entry.title != coordinator.title:
await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
"""Unload Awair configuration."""
unload_ok = await hass.config_entries.async_unload_platforms(
config_entry, PLATFORMS
)
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
if unload_ok:
hass.data[DOMAIN].pop(config_entry.entry_id)
return unload_ok
class AwairDataUpdateCoordinator(DataUpdateCoordinator[dict[str, AwairResult]]):
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
"""Define a wrapper class to update Awair data."""
def __init__(
self,
hass: HomeAssistant,
config_entry: ConfigEntry,
update_interval: timedelta | None,
) -> None:
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
"""Set up the AwairDataUpdateCoordinator class."""
2022-08-11 13:01:35 +00:00
self._config_entry = config_entry
self.title = config_entry.title
2022-08-11 13:01:35 +00:00
super().__init__(hass, LOGGER, name=DOMAIN, update_interval=update_interval)
async def _fetch_air_data(self, device: AwairBaseDevice) -> AwairResult:
2022-08-11 13:01:35 +00:00
"""Fetch latest air quality data."""
LOGGER.debug("Fetching data for %s", device.uuid)
air_data = await device.air_data_latest()
LOGGER.debug(air_data)
return AwairResult(device=device, air_data=air_data)
class AwairCloudDataUpdateCoordinator(AwairDataUpdateCoordinator):
"""Define a wrapper class to update Awair data from Cloud API."""
def __init__(
self, hass: HomeAssistant, config_entry: ConfigEntry, session: ClientSession
) -> None:
2022-08-11 13:01:35 +00:00
"""Set up the AwairCloudDataUpdateCoordinator class."""
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
access_token = config_entry.data[CONF_ACCESS_TOKEN]
self._awair = Awair(access_token=access_token, session=session)
2022-08-11 13:01:35 +00:00
super().__init__(hass, config_entry, UPDATE_INTERVAL_CLOUD)
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
async def _async_update_data(self) -> dict[str, AwairResult]:
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
"""Update data via Awair client library."""
2021-11-04 15:07:50 +00:00
async with timeout(API_TIMEOUT):
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
try:
LOGGER.debug("Fetching users and devices")
user = await self._awair.user()
devices = await user.devices()
results = await gather(
*(self._fetch_air_data(device) for device in devices)
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
)
return {result.device.uuid: result for result in results}
except AuthError as err:
raise ConfigEntryAuthFailed from err
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
except Exception as err:
raise UpdateFailed(err) from err
Refactor / update Awair integration (#34394) * 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>
2020-06-21 19:46:07 +00:00
2022-08-11 13:01:35 +00:00
class AwairLocalDataUpdateCoordinator(AwairDataUpdateCoordinator):
"""Define a wrapper class to update Awair data from the local API."""
_device: AwairLocalDevice | None = None
def __init__(
self, hass: HomeAssistant, config_entry: ConfigEntry, session: ClientSession
) -> None:
2022-08-11 13:01:35 +00:00
"""Set up the AwairLocalDataUpdateCoordinator class."""
self._awair = AwairLocal(
session=session, device_addrs=[config_entry.data[CONF_HOST]]
)
super().__init__(hass, config_entry, UPDATE_INTERVAL_LOCAL)
async def _async_update_data(self) -> dict[str, AwairResult]:
2022-08-11 13:01:35 +00:00
"""Update data via Awair client library."""
async with timeout(API_TIMEOUT):
try:
if self._device is None:
LOGGER.debug("Fetching devices")
devices = await self._awair.devices()
self._device = devices[0]
result = await self._fetch_air_data(self._device)
return {result.device.uuid: result}
except AwairError as err:
LOGGER.error("Unexpected API error: %s", err)
raise UpdateFailed(err) from err