Skip to content

Commit

Permalink
check if password is valid on adding integration
Browse files Browse the repository at this point in the history
  • Loading branch information
fiva authored and fiva committed Nov 21, 2020
1 parent 2d2f071 commit 6a4473a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion custom_components/nhc2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
56 changes: 33 additions & 23 deletions custom_components/nhc2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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."""
Expand All @@ -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()

Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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,
)
2 changes: 1 addition & 1 deletion custom_components/nhc2/manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 5 additions & 1 deletion custom_components/nhc2/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
6 changes: 5 additions & 1 deletion custom_components/nhc2/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}

0 comments on commit 6a4473a

Please sign in to comment.