From 6a4473afb1a4556a92edbaf815b965e861ad67ff Mon Sep 17 00:00:00 2001 From: fiva Date: Sat, 21 Nov 2020 20:51:37 +0100 Subject: [PATCH] check if password is valid on adding integration --- custom_components/nhc2/__init__.py | 2 +- custom_components/nhc2/config_flow.py | 56 ++++++++++++--------- custom_components/nhc2/manifest.json | 2 +- custom_components/nhc2/strings.json | 6 ++- custom_components/nhc2/translations/en.json | 6 ++- 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/custom_components/nhc2/__init__.py b/custom_components/nhc2/__init__.py index b3faf466..05b510b2 100644 --- a/custom_components/nhc2/__init__.py +++ b/custom_components/nhc2/__init__.py @@ -12,7 +12,7 @@ from .const import DOMAIN, KEY_GATEWAY, CONF_SWITCHES_AS_LIGHTS from .helpers import extract_versions -REQUIREMENTS = ['nhc2-coco==0.2.4'] +REQUIREMENTS = ['nhc2-coco==0.2.5'] _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/nhc2/config_flow.py b/custom_components/nhc2/config_flow.py index 12d11e3f..45b4d619 100644 --- a/custom_components/nhc2/config_flow.py +++ b/custom_components/nhc2/config_flow.py @@ -6,6 +6,7 @@ from homeassistant.const import CONF_HOST, CONF_USERNAME, \ CONF_PASSWORD, CONF_ADDRESS, CONF_PORT from nhc2_coco.coco_discover_profiles import CoCoDiscoverProfiles +from nhc2_coco.coco_login_validation import CoCoLoginValidation from .const import DOMAIN, CONF_SWITCHES_AS_LIGHTS @@ -21,9 +22,9 @@ class Nhc2FlowHandler(config_entries.ConfigFlow): def __init__(self): """Init NHC2FlowHandler.""" - self._errors = {} self._all_cocos = [] self._selected_coco = None + self._errors = {} async def async_step_import(self, user_input): """Import a config entry.""" @@ -39,26 +40,38 @@ async def async_step_user(self, user_input=None): self._errors = {} if user_input is not None: + # TODO We might want to check this before showing dialogs in the future matches = list(filter(lambda x: ((x.data[CONF_ADDRESS] == self._selected_coco[1]) and ( x.data[CONF_USERNAME] == user_input[CONF_USERNAME])), self.hass.config_entries.async_entries(DOMAIN))) - _LOGGER.debug('found %d matches' % len(matches)) + + if len(matches) > 0: + return self.async_abort(reason="single_instance_allowed") + user_name = list(filter(lambda x: (x.get('Uuid') == user_input[CONF_USERNAME]), self._selected_coco[2]))[ 0].get('Name') - if len(matches) < 1: - return self.async_create_entry( - title=user_name + ' (' + self._selected_coco[1].replace(':', '') + ')', - data={ - CONF_HOST: self._selected_coco[0] if self._selected_coco[3] is None else self._selected_coco[3], - CONF_ADDRESS: self._selected_coco[1], - CONF_PORT: 8884 if user_input[CONF_USERNAME] == 'hobby' else 8883, - CONF_USERNAME: user_input[CONF_USERNAME], - CONF_PASSWORD: user_input[CONF_PASSWORD], - CONF_SWITCHES_AS_LIGHTS: user_input[CONF_SWITCHES_AS_LIGHTS] - } - ) - - return self.async_abort(reason="single_instance_allowed") + host = self._selected_coco[0] if self._selected_coco[3] is None else self._selected_coco[3] + username = user_input[CONF_USERNAME] + password = user_input[CONF_PASSWORD] + port = 8884 if user_input[CONF_USERNAME] == 'hobby' else 8883 + validator = CoCoLoginValidation(host, username, password, port) + check = await validator.check_connection() + + if check > 0: + self._errors["base"] = ("login_check_fail_%d" % check) + return await self._show_user_config_form(selected_coco=self._selected_coco) + + return self.async_create_entry( + title=user_name + ' (' + self._selected_coco[1].replace(':', '') + ')', + data={ + CONF_HOST: host, + CONF_ADDRESS: self._selected_coco[1], + CONF_PORT: port, + CONF_USERNAME: username, + CONF_PASSWORD: password, + CONF_SWITCHES_AS_LIGHTS: user_input[CONF_SWITCHES_AS_LIGHTS] + } + ) disc = CoCoDiscoverProfiles() @@ -77,14 +90,11 @@ async def async_step_user(self, user_input=None): self._all_cocos) async def async_step_host(self, user_input=None): - # - # users - # for profile in self.all_cocos: - # profile[0] + self._errors = {} _LOGGER.debug(self.hass.config_entries.async_entries(DOMAIN)) self._selected_coco = \ - list(filter(lambda x: x[0] == user_input[CONF_HOST] or x[3] == user_input[CONF_HOST], self._all_cocos))[0] + list(filter(lambda x: x[0] == user_input[CONF_HOST] or x[3] == user_input[CONF_HOST], self._all_cocos))[0] return await self._show_user_config_form(self._selected_coco) async def _show_host_config_form(self, all_cocos): @@ -98,10 +108,10 @@ async def _show_host_config_form(self, all_cocos): first = dkey return self.async_show_form( step_id='host', + errors=self._errors, data_schema=vol.Schema({ vol.Required(CONF_HOST, default=first): vol.In(host_listing) }), - errors=self._errors, ) async def _show_user_config_form(self, selected_coco=None): @@ -116,10 +126,10 @@ async def _show_user_config_form(self, selected_coco=None): first = dkey return self.async_show_form( step_id='user', + errors=self._errors, data_schema=vol.Schema({ vol.Required(CONF_USERNAME, default=first): vol.In(profile_listing), vol.Required(CONF_PASSWORD, default=None): str, vol.Optional(CONF_SWITCHES_AS_LIGHTS, default=False): bool }), - errors=self._errors, ) diff --git a/custom_components/nhc2/manifest.json b/custom_components/nhc2/manifest.json index ca411492..989899e3 100644 --- a/custom_components/nhc2/manifest.json +++ b/custom_components/nhc2/manifest.json @@ -1,7 +1,7 @@ { "domain": "nhc2", "name": "Niko Home Control II", - "requirements": ["nhc2-coco==0.2.4"], + "requirements": ["nhc2-coco==0.2.5"], "config_flow": true, "issue_tracker": "https://github.com/filipvh/hass-nhc2/issues", "documentation": "https://github.com/filipvh/hass-nhc2/blob/master/README.md", diff --git a/custom_components/nhc2/strings.json b/custom_components/nhc2/strings.json index 293fede3..ee16c594 100644 --- a/custom_components/nhc2/strings.json +++ b/custom_components/nhc2/strings.json @@ -22,7 +22,11 @@ "single_instance_allowed": "Controller and profile already added." }, "error": { - "cannot_connect": "Unable to connect to the Connected Controller." + "login_check_fail_1": "Connection refused - incorrect protocol version", + "login_check_fail_2": "Connection refused - invalid client identifier", + "login_check_fail_3": "Connection refused - server unavailable", + "login_check_fail_4": "Connection refused - bad username or password", + "login_check_fail_5": "Connection refused - not authorised" } } } \ No newline at end of file diff --git a/custom_components/nhc2/translations/en.json b/custom_components/nhc2/translations/en.json index 131d9c0c..98999614 100644 --- a/custom_components/nhc2/translations/en.json +++ b/custom_components/nhc2/translations/en.json @@ -22,7 +22,11 @@ "single_instance_allowed": "Controller and profile already added." }, "error": { - "cannot_connect": "Unable to connect to the Connected Controller." + "login_check_fail_1": "Connection refused - incorrect protocol version", + "login_check_fail_2": "Connection refused - invalid client identifier", + "login_check_fail_3": "Connection refused - server unavailable", + "login_check_fail_4": "Connection refused - bad username or password", + "login_check_fail_5": "Connection refused - not authorised" } } } \ No newline at end of file