-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Add Garmin Connect integration #30792
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
Merged
Merged
Changes from 32 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
1c8dda2
Added code files
cyberjunky 54a86b8
Correctly name init file
cyberjunky 88c8039
Update codeowners
cyberjunky f8d6f14
Update requirements
cyberjunky 0a6d74a
Added code files
cyberjunky 51b1d5a
Correctly name init file
cyberjunky d688f80
Update codeowners
cyberjunky 74059c9
Update requirements
cyberjunky 774fe24
Black changes, added to coveragerc
cyberjunky c2793f9
Merge branch 'garmin-connect' of https://github.com/cyberjunky/home-a…
cyberjunky 72350dc
Removed documentation location for now
cyberjunky 03a7b75
Added documentation url
cyberjunky 3c166ec
Fixed merge
cyberjunky 48783bf
Fixed flake8 syntax
cyberjunky 2a90f7c
Merge branch 'garmin-connect' of https://github.com/cyberjunky/home-a…
cyberjunky 30ada90
Fixed isort
cyberjunky 4aeaa0d
Removed false check and double throttle, applied time format change
cyberjunky 9043a5e
Renamed email to username, used dict, deleted unused type, changed at…
cyberjunky 2880a96
Async and ConfigFlow code
cyberjunky cd9b34f
Fixes
cyberjunky 42857f6
Added device_class and misc fixes
cyberjunky ebfa841
isort and pylint fixes
cyberjunky 3dea786
Removed from test requirements
cyberjunky cad6f49
Merge pull request #1 from cyberjunky/garmin-connect-configflow
cyberjunky 92b46eb
Fixed isort checkblack
cyberjunky b4357b4
Removed host field
cyberjunky f7735f4
Fixed coveragerc
cyberjunky db3f856
Start working test file
cyberjunky d021103
Added more config_flow tests
cyberjunky 60f5c11
Enable only most used sensors by default
cyberjunky a2f11ab
Added more default enabled sensors, fixed tests
cyberjunky cae9406
Fixed isort
cyberjunky 08d0a6d
Test config_flow improvements
cyberjunky 9a6ce6f
Remove unused import
cyberjunky 53b53e2
Removed redundant patch calls
cyberjunky 9a17cc1
Fixed mock return value
cyberjunky 39df719
Updated to garmin_connect 0.1.8 fixed exceptions
cyberjunky 37ee95a
Quick fix test patch to see if rest is error free
cyberjunky fb90fc0
Fixed mock routine
cyberjunky 282fa4c
Code improvements from PR feedback
cyberjunky 4325416
Fix entity indentifier
cyberjunky a4bf720
Reverted device identifier
cyberjunky d8c0839
Fixed abort message
cyberjunky d91f2de
Test fix
cyberjunky 14453e9
Fixed unique_id MockConfigEntry
cyberjunky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
24 changes: 24 additions & 0 deletions
24
homeassistant/components/garmin_connect/.translations/en.json
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,24 @@ | ||
| { | ||
| "config": { | ||
| "abort": { | ||
| "already_setup": "This account is already configured." | ||
| }, | ||
| "error": { | ||
| "cannot_connect": "Failed to connect, please try again.", | ||
| "invalid_auth": "Invalid authentication.", | ||
| "too_many_requests": "Too many requests, retry later.", | ||
| "unknown": "Unexpected error." | ||
| }, | ||
| "step": { | ||
| "user": { | ||
| "data": { | ||
| "password": "Password", | ||
| "username": "Username" | ||
| }, | ||
| "description": "Enter your credentials.", | ||
| "title": "Garmin Connect" | ||
| } | ||
| }, | ||
| "title": "Garmin Connect" | ||
| } | ||
| } |
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,104 @@ | ||
| """The Garmin Connect integration.""" | ||
| import asyncio | ||
| from datetime import date, timedelta | ||
| import logging | ||
|
|
||
| from garminconnect import ( | ||
| Garmin, | ||
| GarminConnectAuthenticationError, | ||
| GarminConnectConnectionError, | ||
| GarminConnectTooManyRequestsError, | ||
| ) | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import CONF_PASSWORD, CONF_USERNAME | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.exceptions import PlatformNotReady | ||
| from homeassistant.util import Throttle | ||
|
|
||
| from .const import DOMAIN | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| PLATFORMS = ["sensor"] | ||
| MIN_SCAN_INTERVAL = timedelta(minutes=10) | ||
|
|
||
|
|
||
| async def async_setup(hass: HomeAssistant, config: dict): | ||
| """Set up the Garmin Connect component.""" | ||
| hass.data[DOMAIN] = {} | ||
| return True | ||
|
|
||
|
|
||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
| """Set up Garmin Connect from a config entry.""" | ||
| username = entry.data[CONF_USERNAME] | ||
| password = entry.data[CONF_PASSWORD] | ||
|
|
||
| try: | ||
| garmin_client = Garmin(username, password) | ||
| except ( | ||
| GarminConnectAuthenticationError, | ||
| GarminConnectTooManyRequestsError, | ||
| ) as err: | ||
| _LOGGER.error("Error occured during Garmin Connect setup: %s", err) | ||
| return False | ||
| except (GarminConnectConnectionError) as err: | ||
| _LOGGER.error("Error occured during Garmin Connect setup: %s", err) | ||
| raise PlatformNotReady | ||
|
MartinHjelmare marked this conversation as resolved.
Outdated
|
||
| except Exception: # pylint: disable=broad-except | ||
| _LOGGER.error("Unknown error occured during Garmin Connect setup") | ||
| return False | ||
|
|
||
| garmin_data = GarminConnectData(hass, garmin_client) | ||
| hass.data[DOMAIN][entry.entry_id] = garmin_data | ||
|
|
||
| for component in PLATFORMS: | ||
| hass.async_create_task( | ||
| hass.config_entries.async_forward_entry_setup(entry, component) | ||
| ) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
| """Unload a config entry.""" | ||
| unload_ok = all( | ||
| await asyncio.gather( | ||
| *[ | ||
| hass.config_entries.async_forward_entry_unload(entry, component) | ||
| for component in PLATFORMS | ||
| ] | ||
| ) | ||
| ) | ||
| if unload_ok: | ||
| hass.data[DOMAIN].pop(entry.entry_id) | ||
|
|
||
| return unload_ok | ||
|
|
||
|
|
||
| class GarminConnectData: | ||
| """Define an object to hold sensor data.""" | ||
|
|
||
| def __init__(self, hass, client): | ||
| """Initialize.""" | ||
| self.client = client | ||
| self.data = None | ||
|
|
||
| @Throttle(MIN_SCAN_INTERVAL) | ||
| async def async_update(self): | ||
| """Update data via library.""" | ||
| today = date.today() | ||
|
|
||
| try: | ||
| self.data = self.client.get_stats(today.isoformat()) | ||
| except ( | ||
| GarminConnectAuthenticationError, | ||
| GarminConnectTooManyRequestsError, | ||
| GarminConnectConnectionError, | ||
| ) as err: | ||
| _LOGGER.error("Error occured during Garmin Connect stats update: %s", err) | ||
| raise PlatformNotReady | ||
|
MartinHjelmare marked this conversation as resolved.
Outdated
|
||
| except Exception: # pylint: disable=broad-except | ||
| _LOGGER.error("Unknown error occured during Garmin Connect stats update") | ||
| return | ||
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,74 @@ | ||
| """Config flow for Garmin Connect integration.""" | ||
| import logging | ||
|
|
||
| from garminconnect import ( | ||
| Garmin, | ||
| GarminConnectAuthenticationError, | ||
| GarminConnectConnectionError, | ||
| GarminConnectTooManyRequestsError, | ||
| ) | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant import config_entries | ||
| from homeassistant.const import CONF_ID, CONF_PASSWORD, CONF_USERNAME | ||
|
|
||
| from .const import DOMAIN | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @config_entries.HANDLERS.register(DOMAIN) | ||
|
MartinHjelmare marked this conversation as resolved.
Outdated
|
||
| class GarminConnectConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): | ||
| """Handle a config flow for Garmin Connect.""" | ||
|
|
||
| VERSION = 1 | ||
| CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL | ||
|
|
||
| async def _show_setup_form(self, errors=None): | ||
| """Show the setup form to the user.""" | ||
| return self.async_show_form( | ||
| step_id="user", | ||
| data_schema=vol.Schema( | ||
| {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str} | ||
| ), | ||
| errors=errors or {}, | ||
| ) | ||
|
|
||
| async def async_step_user(self, user_input=None): | ||
| """Handle the initial step.""" | ||
| if user_input is None: | ||
| return await self._show_setup_form(user_input) | ||
|
MartinHjelmare marked this conversation as resolved.
Outdated
|
||
|
|
||
| errors = {} | ||
| try: | ||
| unique_id = Garmin( | ||
| user_input[CONF_USERNAME], user_input[CONF_PASSWORD] | ||
| ).get_full_name() | ||
|
|
||
| except GarminConnectConnectionError: | ||
| errors["base"] = "cannot_connect" | ||
| return await self._show_setup_form(errors) | ||
| except GarminConnectAuthenticationError: | ||
| errors["base"] = "invalid_auth" | ||
| return await self._show_setup_form(errors) | ||
| except GarminConnectTooManyRequestsError: | ||
| errors["base"] = "too_many_requests" | ||
| return await self._show_setup_form(errors) | ||
| except Exception: # pylint: disable=broad-except | ||
| _LOGGER.exception("Unexpected exception") | ||
| errors["base"] = "unknown" | ||
| return await self._show_setup_form(errors) | ||
|
|
||
| entries = self._async_current_entries() | ||
| for entry in entries: | ||
| if entry.data[CONF_ID] == unique_id: | ||
|
MartinHjelmare marked this conversation as resolved.
Outdated
|
||
| return self.async_abort(reason="already_setup") | ||
|
|
||
| return self.async_create_entry( | ||
| title=unique_id, | ||
| data={ | ||
| CONF_ID: unique_id, | ||
| CONF_USERNAME: user_input[CONF_USERNAME], | ||
| CONF_PASSWORD: user_input[CONF_PASSWORD], | ||
| }, | ||
| ) | ||
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.