-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add config flow to linky #26076
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
MartinHjelmare
merged 14 commits into
home-assistant:dev
from
Quentame:linky/config-flow
Sep 4, 2019
Merged
Add config flow to linky #26076
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
60f5292
Linky: setup ConfigFlow
Quentame d047f4d
async_track_time_interval
Quentame 75b7680
Review from @MartinHjelmare 1
Quentame 90bc0b8
Review from @MartinHjelmare 2
Quentame f133202
Review from @MartinHjelmare 3
Quentame 3a0cfa4
Review from @MartinHjelmare 4
Quentame 17d3856
black --fast homeassistant tests
Quentame aa650fb
Bump pylinky to 0.4.0 and add error user feedback
Quentame 15a5789
Fix .coveragerc
Quentame f1990c2
Linky platform moved to integration in config.yml and with multiple a…
Quentame b83f70d
Remove useless logs
Quentame 1c444f5
Review from @MartinHjelmare 5
Quentame 7649dbb
Add config flow tests
Quentame 2033104
Add config flow tests : login + fetch on failed
Quentame 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
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,25 @@ | ||
| { | ||
| "config": { | ||
| "abort": { | ||
| "username_exists": "Account already configured" | ||
| }, | ||
| "error": { | ||
| "access": "Could not access to Enedis.fr, please check your internet connection", | ||
| "enedis": "Enedis.fr answered with an error: please retry later (usually not between 11PM and 2AM)", | ||
| "unknown": "Unknown error: please retry later (usually not between 11PM and 2AM)", | ||
| "username_exists": "Account already configured", | ||
| "wrong_login": "Login error: please check your email & password" | ||
| }, | ||
| "step": { | ||
| "user": { | ||
| "data": { | ||
| "password": "Password", | ||
| "username": "Email" | ||
| }, | ||
| "description": "Enter your credentials", | ||
| "title": "Linky" | ||
| } | ||
| }, | ||
| "title": "Linky" | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1 +1,55 @@ | ||
| """The linky component.""" | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry | ||
| from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME | ||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.helpers.typing import HomeAssistantType | ||
|
|
||
| from .const import DEFAULT_TIMEOUT, DOMAIN | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| ACCOUNT_SCHEMA = vol.Schema( | ||
| { | ||
| vol.Required(CONF_USERNAME): cv.string, | ||
| vol.Required(CONF_PASSWORD): cv.string, | ||
| vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int, | ||
| } | ||
| ) | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema( | ||
| {DOMAIN: vol.Schema(vol.All(cv.ensure_list, [ACCOUNT_SCHEMA]))}, | ||
| extra=vol.ALLOW_EXTRA, | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup(hass, config): | ||
| """Set up Linky sensors from legacy config file.""" | ||
|
|
||
| conf = config.get(DOMAIN) | ||
| if conf is None: | ||
| return True | ||
|
|
||
| for linky_account_conf in conf: | ||
| hass.async_create_task( | ||
| hass.config_entries.flow.async_init( | ||
| DOMAIN, | ||
| context={"source": SOURCE_IMPORT}, | ||
| data=linky_account_conf.copy(), | ||
| ) | ||
| ) | ||
|
|
||
| return True | ||
|
Quentame marked this conversation as resolved.
|
||
|
|
||
|
|
||
| async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): | ||
| """Set up Linky sensors.""" | ||
|
|
||
| hass.async_create_task( | ||
| hass.config_entries.async_forward_entry_setup(entry, "sensor") | ||
| ) | ||
|
|
||
| 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,118 @@ | ||
| """Config flow to configure the Linky integration.""" | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
| from pylinky.client import LinkyClient | ||
| from pylinky.exceptions import ( | ||
| PyLinkyAccessException, | ||
| PyLinkyEnedisException, | ||
| PyLinkyException, | ||
| PyLinkyWrongLoginException, | ||
| ) | ||
|
|
||
| from homeassistant import config_entries | ||
| from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME | ||
| from homeassistant.core import callback | ||
|
|
||
| from .const import DEFAULT_TIMEOUT, DOMAIN | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class LinkyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): | ||
| """Handle a config flow.""" | ||
|
|
||
| VERSION = 1 | ||
| CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL | ||
|
|
||
| def __init__(self): | ||
| """Initialize Linky config flow.""" | ||
| self._username = None | ||
| self._password = None | ||
| self._timeout = None | ||
|
|
||
| def _configuration_exists(self, username: str) -> bool: | ||
| """Return True if username exists in configuration.""" | ||
| for entry in self.hass.config_entries.async_entries(DOMAIN): | ||
| if entry.data[CONF_USERNAME] == username: | ||
| return True | ||
| return False | ||
|
|
||
| @callback | ||
| def _show_setup_form(self, user_input=None, errors=None): | ||
| """Show the setup form to the user.""" | ||
|
|
||
| if user_input is None: | ||
| user_input = {} | ||
|
|
||
| return self.async_show_form( | ||
| step_id="user", | ||
| data_schema=vol.Schema( | ||
| { | ||
| vol.Required( | ||
| CONF_USERNAME, default=user_input.get(CONF_USERNAME, "") | ||
| ): str, | ||
| vol.Required( | ||
| CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "") | ||
| ): str, | ||
| } | ||
| ), | ||
| errors=errors or {}, | ||
| ) | ||
|
|
||
| async def async_step_user(self, user_input=None): | ||
| """Handle a flow initiated by the user.""" | ||
| errors = {} | ||
|
|
||
| if user_input is None: | ||
| return self._show_setup_form(user_input, None) | ||
|
|
||
| self._username = user_input[CONF_USERNAME] | ||
| self._password = user_input[CONF_PASSWORD] | ||
| self._timeout = user_input.get(CONF_TIMEOUT, DEFAULT_TIMEOUT) | ||
|
|
||
| if self._configuration_exists(self._username): | ||
| errors[CONF_USERNAME] = "username_exists" | ||
| return self._show_setup_form(user_input, errors) | ||
|
|
||
| client = LinkyClient(self._username, self._password, None, self._timeout) | ||
| try: | ||
| await self.hass.async_add_executor_job(client.login) | ||
| await self.hass.async_add_executor_job(client.fetch_data) | ||
| except PyLinkyAccessException as exp: | ||
| _LOGGER.error(exp) | ||
| errors["base"] = "access" | ||
| return self._show_setup_form(user_input, errors) | ||
| except PyLinkyEnedisException as exp: | ||
| _LOGGER.error(exp) | ||
| errors["base"] = "enedis" | ||
| return self._show_setup_form(user_input, errors) | ||
| except PyLinkyWrongLoginException as exp: | ||
| _LOGGER.error(exp) | ||
| errors["base"] = "wrong_login" | ||
| return self._show_setup_form(user_input, errors) | ||
| except PyLinkyException as exp: | ||
| _LOGGER.error(exp) | ||
| errors["base"] = "unknown" | ||
| return self._show_setup_form(user_input, errors) | ||
| finally: | ||
| client.close_session() | ||
|
|
||
| return self.async_create_entry( | ||
| title=self._username, | ||
| data={ | ||
| CONF_USERNAME: self._username, | ||
| CONF_PASSWORD: self._password, | ||
| CONF_TIMEOUT: self._timeout, | ||
| }, | ||
| ) | ||
|
|
||
| async def async_step_import(self, user_input=None): | ||
| """Import a config entry. | ||
|
|
||
| Only host was required in the yaml file all other fields are optional | ||
| """ | ||
| if self._configuration_exists(user_input[CONF_USERNAME]): | ||
| return self.async_abort(reason="username_exists") | ||
|
|
||
| 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,5 @@ | ||
| """Linky component constants.""" | ||
|
|
||
| DOMAIN = "linky" | ||
|
|
||
| DEFAULT_TIMEOUT = 10 |
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 |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| { | ||
| "domain": "linky", | ||
| "name": "Linky", | ||
| "config_flow": true, | ||
| "documentation": "https://www.home-assistant.io/components/linky", | ||
| "requirements": [ | ||
| "pylinky==0.3.3" | ||
| "pylinky==0.4.0" | ||
| ], | ||
| "dependencies": [], | ||
| "codeowners": [ | ||
| "@tiste", | ||
| "@Quentame" | ||
| ] | ||
| } |
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.