-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add Config Flow for sense #32160
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
bachya
merged 3 commits into
home-assistant:dev
from
bdraco:config_flow_for_sense_fix_and_migrate_unique_ids
Feb 25, 2020
Merged
Add Config Flow for sense #32160
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "config": { | ||
| "title": "Sense", | ||
| "step": { | ||
| "user": { | ||
| "title": "Connect to your Sense Energy Monitor", | ||
| "data": { | ||
| "email": "Email Address", | ||
| "password": "Password" | ||
| } | ||
| } | ||
| }, | ||
| "error": { | ||
| "cannot_connect": "Failed to connect, please try again", | ||
| "invalid_auth": "Invalid authentication", | ||
| "unknown": "Unexpected error" | ||
| }, | ||
| "abort": { | ||
| "already_configured": "Device is already configured" | ||
| } | ||
| } | ||
| } |
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
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,75 @@ | ||
| """Config flow for Sense integration.""" | ||
| import logging | ||
|
|
||
| from sense_energy import ( | ||
| ASyncSenseable, | ||
| SenseAPITimeoutException, | ||
| SenseAuthenticationException, | ||
| ) | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant import config_entries, core | ||
| from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT | ||
|
|
||
| from .const import ACTIVE_UPDATE_RATE, DEFAULT_TIMEOUT | ||
|
|
||
| from .const import DOMAIN # pylint:disable=unused-import; pylint:disable=unused-import | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DATA_SCHEMA = vol.Schema( | ||
| { | ||
| vol.Required(CONF_EMAIL): str, | ||
| vol.Required(CONF_PASSWORD): str, | ||
| vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): vol.Coerce(int), | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| async def validate_input(hass: core.HomeAssistant, data): | ||
| """Validate the user input allows us to connect. | ||
|
|
||
| Data has the keys from DATA_SCHEMA with values provided by the user. | ||
| """ | ||
| timeout = data[CONF_TIMEOUT] | ||
|
|
||
| gateway = ASyncSenseable(api_timeout=timeout, wss_timeout=timeout) | ||
| gateway.rate_limit = ACTIVE_UPDATE_RATE | ||
| await gateway.authenticate(data[CONF_EMAIL], data[CONF_PASSWORD]) | ||
|
|
||
| # Return info that you want to store in the config entry. | ||
| return {"title": data[CONF_EMAIL]} | ||
|
|
||
|
|
||
| class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): | ||
| """Handle a config flow for Sense.""" | ||
|
|
||
| VERSION = 1 | ||
| CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL | ||
|
|
||
| async def async_step_user(self, user_input=None): | ||
| """Handle the initial step.""" | ||
| errors = {} | ||
| if user_input is not None: | ||
| try: | ||
| info = await validate_input(self.hass, user_input) | ||
| await self.async_set_unique_id(user_input[CONF_EMAIL]) | ||
| return self.async_create_entry(title=info["title"], data=user_input) | ||
| except SenseAPITimeoutException: | ||
| errors["base"] = "cannot_connect" | ||
| except SenseAuthenticationException: | ||
| errors["base"] = "invalid_auth" | ||
| except Exception: # pylint: disable=broad-except | ||
| _LOGGER.exception("Unexpected exception") | ||
| errors["base"] = "unknown" | ||
|
|
||
| return self.async_show_form( | ||
| step_id="user", data_schema=DATA_SCHEMA, errors=errors | ||
| ) | ||
|
|
||
| async def async_step_import(self, user_input): | ||
| """Handle import.""" | ||
| await self.async_set_unique_id(user_input[CONF_EMAIL]) | ||
| self._abort_if_unique_id_configured() | ||
|
|
||
| return await self.async_step_user(user_input) |
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,7 @@ | ||
| """Constants for monitoring a Sense energy sensor.""" | ||
| DOMAIN = "sense" | ||
| DEFAULT_TIMEOUT = 10 | ||
| ACTIVE_UPDATE_RATE = 60 | ||
| DEFAULT_NAME = "Sense" | ||
| SENSE_DATA = "sense_data" | ||
| SENSE_DEVICE_UPDATE = "sense_devices_update" |
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
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.