-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
Uhoo integration #158887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
joshsmonta
wants to merge
20
commits into
home-assistant:dev
Choose a base branch
from
joshsmonta:uhoo-integration
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Uhoo integration #158887
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
86192d8
feat: add uhoo integration
joshsmonta 8cda9d7
feat: uhoo integration
joshsmonta c89f100
Merge branch 'dev' into uhoo-integration
joshsmonta ff8653f
Merge branch 'dev' into uhoo-integration
joshsmonta b399958
refactor: integrate uhooapi as library
joshsmonta 36681b4
Merge branch 'dev' into uhoo-integration
joshsmonta 9557303
Merge branch 'dev' into uhoo-integration
joshsmonta 8d06397
Merge branch 'dev' into uhoo-integration
joshsmonta c4be9ce
refactor: comply with requested changes
joshsmonta a9f31d0
Merge branch 'dev' into uhoo-integration
joshsmonta ee8146d
Merge branch 'dev' into uhoo-integration
joshsmonta d58a071
refactor: implement requested changes
joshsmonta c4cb7d5
Merge branch 'dev' into uhoo-integration
joshsmonta 71b8817
refactor: apply requested changes
joshsmonta 530c1a3
Merge branch 'dev' into uhoo-integration
joshsmonta f88e8d4
refactor: apply changes requested
joshsmonta 753d564
refactor: add entry to update coordinator
joshsmonta e7b068d
Merge branch 'dev' into uhoo-integration
joshsmonta 047852b
refactor: tests
joshsmonta 3d05917
Merge branch 'dev' into uhoo-integration
joshsmonta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """Initializes the uhoo api client and setup needed for the devices.""" | ||
|
|
||
| from aiodns.error import DNSError | ||
| from aiohttp.client_exceptions import ClientConnectionError | ||
| from uhooapi import Client | ||
| from uhooapi.errors import UhooError, UnauthorizedError | ||
|
|
||
| from homeassistant.const import CONF_API_KEY | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady | ||
| from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
|
|
||
| from .const import PLATFORMS | ||
| from .coordinator import UhooConfigEntry, UhooDataUpdateCoordinator | ||
|
|
||
|
|
||
| async def async_setup_entry(hass: HomeAssistant, config_entry: UhooConfigEntry) -> bool: | ||
| """Set up uHoo integration from a config entry.""" | ||
|
|
||
| # get api key and session from configuration | ||
| api_key = config_entry.data[CONF_API_KEY] | ||
| session = async_get_clientsession(hass) | ||
| client = Client(api_key, session, debug=False) | ||
| coordinator = UhooDataUpdateCoordinator(hass, client=client, entry=config_entry) | ||
|
|
||
| try: | ||
| await client.login() | ||
| await client.setup_devices() | ||
| except (ClientConnectionError, DNSError) as err: | ||
| raise ConfigEntryNotReady(f"Cannot connect to uHoo servers: {err}") from err | ||
| except UnauthorizedError as err: | ||
| raise ConfigEntryError(f"Invalid API credentials: {err}") from err | ||
| except UhooError as err: | ||
| raise ConfigEntryNotReady(err) from err | ||
|
|
||
| await coordinator.async_config_entry_first_refresh() | ||
| config_entry.runtime_data = coordinator | ||
| await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) | ||
| return True | ||
|
|
||
|
|
||
| async def async_unload_entry( | ||
| hass: HomeAssistant, config_entry: UhooConfigEntry | ||
| ) -> bool: | ||
| """Handle removal of an entry.""" | ||
| return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """Custom uhoo config flow setup.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from aiodns.error import DNSError | ||
| from aiohttp.client_exceptions import ClientConnectorDNSError | ||
| from uhooapi import Client | ||
| from uhooapi.errors import UnauthorizedError | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.config_entries import ConfigFlow, ConfigFlowResult | ||
| from homeassistant.const import CONF_API_KEY | ||
| from homeassistant.helpers.aiohttp_client import async_create_clientsession | ||
| from homeassistant.helpers.selector import ( | ||
| TextSelector, | ||
| TextSelectorConfig, | ||
| TextSelectorType, | ||
| ) | ||
|
|
||
| from .const import DOMAIN, LOGGER | ||
|
|
||
| USER_DATA_SCHEMA = vol.Schema( | ||
| { | ||
| vol.Required(CONF_API_KEY): TextSelector( | ||
| TextSelectorConfig( | ||
| type=TextSelectorType.PASSWORD, | ||
| autocomplete="current-password", | ||
| ) | ||
| ), | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| class UhooFlowHandler(ConfigFlow, domain=DOMAIN): | ||
| """Setup Uhoo flow handlers.""" | ||
|
|
||
| VERSION = 1 | ||
|
|
||
| def __init__(self) -> None: | ||
| """Initialize the config flow.""" | ||
| self._errors: dict = {} | ||
|
|
||
| async def async_step_user( | ||
| self, user_input: dict[str, Any] | None = None | ||
| ) -> ConfigFlowResult: | ||
| """Handle the start of the config flow.""" | ||
| self._errors = {} | ||
|
|
||
| if user_input is None: | ||
| return await self._show_config_form(user_input) | ||
|
|
||
| # Set the unique ID for the config flow. | ||
| api_key = user_input[CONF_API_KEY] | ||
| if not api_key: | ||
| self._errors["base"] = "invalid_auth" | ||
| return await self._show_config_form(user_input) | ||
|
|
||
| await self.async_set_unique_id(api_key) | ||
| self._async_abort_entries_match() | ||
|
|
||
| valid = await self._test_credentials(api_key) | ||
| if not valid: | ||
| self._errors["base"] = "invalid_auth" | ||
| return await self._show_config_form(user_input) | ||
|
|
||
| key_snippet = api_key[-5:] | ||
| return self.async_create_entry(title=f"uHoo ({key_snippet})", data=user_input) | ||
|
|
||
| async def _show_config_form( | ||
| self, user_input: dict[str, Any] | None = None | ||
| ) -> ConfigFlowResult: | ||
| if user_input is None: | ||
| user_input = {} | ||
| user_input[CONF_API_KEY] = "" | ||
| return await self._show_config_form(user_input) | ||
| return self.async_show_form( | ||
| step_id="user", | ||
| data_schema=self.add_suggested_values_to_schema( | ||
| USER_DATA_SCHEMA, user_input or {} | ||
| ), | ||
| errors=self._errors, | ||
| ) | ||
|
|
||
| async def _test_credentials(self, api_key): | ||
| """Return true if credentials is valid.""" | ||
| try: | ||
| session = async_create_clientsession(self.hass) | ||
| client = Client(api_key, session, debug=True) | ||
| await client.login() | ||
| except UnauthorizedError as err: | ||
| LOGGER.error( | ||
| f"Error: received a 401 Unauthorized error attempting to login:\n{err}" | ||
| ) | ||
| return False | ||
| except ConnectionError: | ||
| LOGGER.error("ConnectionError: cannot connect to uhoo server") | ||
| return False | ||
| except (ClientConnectorDNSError, DNSError): | ||
| LOGGER.error("ClientConnectorDNSError: cannot connect to uhoo server") | ||
| return False | ||
| else: | ||
| return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """Static consts for uhoo integration.""" | ||
|
|
||
| from datetime import timedelta | ||
| import logging | ||
|
|
||
| DOMAIN = "uhoo" | ||
| PLATFORMS = ["sensor"] | ||
| LOGGER = logging.getLogger(__package__) | ||
|
|
||
| NAME = "uHoo Integration" | ||
| MODEL = "uHoo Indoor Air Monitor" | ||
| MANUFACTURER = "uHoo Pte. Ltd." | ||
|
|
||
| UPDATE_INTERVAL = timedelta(seconds=300) | ||
|
|
||
| API_VIRUS = "virus_index" | ||
| API_MOLD = "mold_index" | ||
| API_TEMP = "temperature" | ||
| API_HUMIDITY = "humidity" | ||
| API_PM25 = "pm25" | ||
| API_TVOC = "tvoc" | ||
| API_CO2 = "co2" | ||
| API_CO = "co" | ||
| API_PRESSURE = "air_pressure" | ||
| API_OZONE = "ozone" | ||
| API_NO2 = "no2" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """Custom uhoo data update coordinator.""" | ||
|
|
||
| import asyncio | ||
|
|
||
| from aiohttp.client_exceptions import ClientConnectorDNSError | ||
| from uhooapi import Client, Device | ||
| from uhooapi.errors import UhooError | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
|
||
| from .const import DOMAIN, LOGGER, UPDATE_INTERVAL | ||
|
|
||
| type UhooConfigEntry = ConfigEntry["UhooDataUpdateCoordinator"] | ||
|
|
||
|
|
||
| class UhooDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]): | ||
| """Class to manage fetching data from the uHoo API.""" | ||
|
|
||
| def __init__( | ||
| self, hass: HomeAssistant, client: Client, entry: UhooConfigEntry | ||
| ) -> None: | ||
| """Initialize DataUpdateCoordinator.""" | ||
| self.client = client | ||
| self.entry = entry | ||
| super().__init__(hass, LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL) | ||
|
|
||
| async def _async_update_data(self) -> dict[str, Device]: | ||
| try: | ||
| await self.client.login() | ||
| if self.client.devices: | ||
| await asyncio.gather( | ||
| *[ | ||
| self.client.get_latest_data(device_id) | ||
| for device_id in self.client.devices | ||
| ] | ||
| ) | ||
| except TimeoutError as error: | ||
| raise UpdateFailed from error | ||
| except ClientConnectorDNSError as error: | ||
| raise UpdateFailed from error | ||
| except UhooError as error: | ||
| raise UpdateFailed(f"The device is unavailable: {error}") from error | ||
| else: | ||
| return self.client.devices | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "domain": "uhoo", | ||
| "name": "uHoo", | ||
| "codeowners": ["@getuhoo", "@joshsmonta"], | ||
| "config_flow": true, | ||
| "documentation": "https://www.home-assistant.io/integrations/uhooair", | ||
| "iot_class": "cloud_polling", | ||
| "quality_scale": "bronze", | ||
| "requirements": ["uhooapi==1.2.3"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| rules: | ||
| # Bronze | ||
| action-setup: done | ||
| appropriate-polling: done | ||
| brands: done | ||
| common-modules: done | ||
| config-flow-test-coverage: done | ||
| config-flow: done | ||
| dependency-transparency: done | ||
| docs-actions: done | ||
| docs-high-level-description: done | ||
| docs-installation-instructions: done | ||
| docs-removal-instructions: done | ||
| entity-event-setup: done | ||
| entity-unique-id: done | ||
| has-entity-name: done | ||
| runtime-data: done | ||
| test-before-configure: done | ||
| test-before-setup: done | ||
| unique-config-entry: done | ||
|
|
||
| # Silver | ||
| action-exceptions: done | ||
| config-entry-unloading: done | ||
| docs-configuration-parameters: done | ||
| docs-installation-parameters: done | ||
| entity-unavailable: done | ||
| integration-owner: done | ||
| log-when-unavailable: todo | ||
| parallel-updates: done | ||
| reauthentication-flow: todo | ||
| test-coverage: done | ||
|
|
||
| # Gold | ||
| devices: done | ||
| diagnostics: todo | ||
| discovery-update-info: todo | ||
| discovery: todo | ||
| docs-data-update: done | ||
| docs-examples: todo | ||
| docs-known-limitations: todo | ||
| docs-supported-devices: done | ||
| docs-supported-functions: todo | ||
| docs-troubleshooting: done | ||
| docs-use-cases: done | ||
| dynamic-devices: todo | ||
| entity-category: todo | ||
| entity-device-class: done | ||
| entity-disabled-by-default: todo | ||
| entity-translations: done | ||
| exception-translations: todo | ||
| icon-translations: todo | ||
| reconfiguration-flow: todo | ||
| repair-issues: todo | ||
| stale-devices: todo | ||
|
|
||
| # Platinum | ||
| async-dependency: done | ||
| inject-websession: done | ||
| strict-typing: todo |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.