Skip to content

Commit

Permalink
Manual host and dimmers (#8)
Browse files Browse the repository at this point in the history
Allow adding coco manually and enable to change brightness on dimmers
  • Loading branch information
filipvh authored Dec 1, 2020
1 parent 940d519 commit 2a62bb0
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 41 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added custom_components/.DS_Store
Binary file not shown.
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==1.0.1']
REQUIREMENTS = ['nhc2-coco==1.1.3']

_LOGGER = logging.getLogger(__name__)

Expand Down
60 changes: 42 additions & 18 deletions custom_components/nhc2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from nhc2_coco.coco_discover_profiles import CoCoDiscoverProfiles
from nhc2_coco.coco_login_validation import CoCoLoginValidation

from .const import DOMAIN, CONF_SWITCHES_AS_LIGHTS
from .const import DOMAIN, CONF_SWITCHES_AS_LIGHTS, KEY_MANUAL

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -62,7 +62,7 @@ async def async_step_user(self, user_input=None):
return await self._show_user_config_form()

return self.async_create_entry(
title=user_name + ' (' + self._selected_coco[1].replace(':', '') + ')',
title=user_name + ' (' + host + ')',
data={
CONF_HOST: host,
CONF_ADDRESS: self._selected_coco[1],
Expand All @@ -84,25 +84,37 @@ async def async_step_user(self, user_input=None):
'Type': 'hobby'
})

found_coco_count = len(self._all_cocos) if self._all_cocos else 0
# If there is only one controller, we continue to the next step
if found_coco_count == 1:
self._selected_coco = self._all_cocos[0]
return await self._show_user_config_form()

if found_coco_count > 1:
return await self._show_host_config_form()

return self.async_abort(reason="no_controller_found")
return await self._show_host_config_form()

async def async_step_host(self, user_input=None):
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]
if user_input[CONF_HOST] == KEY_MANUAL:
return await self._show_manual_host_config_form()
else:
self._selected_coco = \
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()

async def async_step_manual_host(self, user_input=None):
self._errors = {}

disc = CoCoDiscoverProfiles(user_input[CONF_HOST])
self._all_cocos = await disc.get_all_profiles()
if self._all_cocos is not None and len(self._all_cocos) == 1:
self._selected_coco = self._all_cocos[0]
_LOGGER.debug(str(self._all_cocos))
for coco in self._all_cocos:
if coco[2] is not None:
coco[2].insert(0, {
'Uuid': 'hobby',
'Name': 'hobby',
'Type': 'hobby'
})

return await self._show_user_config_form()
else:
return self.async_abort(reason="no_controller_found")

async def _show_host_config_form(self):
"""Show the configuration form to edit NHC2 data."""
host_listing = {}
Expand All @@ -112,6 +124,9 @@ async def _show_host_config_form(self):
host_listing[dkey] = [x[3] + ' (' + x[0] + ')']
if i == 0:
first = dkey
host_listing[KEY_MANUAL] = 'Manual Input'
if first is None:
first = KEY_MANUAL
return self.async_show_form(
step_id='host',
errors=self._errors,
Expand All @@ -134,12 +149,21 @@ async def _show_user_config_form(self):
step_id='user',
errors=self._errors,
description_placeholders={
"host": self._selected_coco[3] if self._selected_coco[3] is not None else self._selected_coco[0],
"address": self._selected_coco[1]
"host": self._selected_coco[3] if self._selected_coco[3] is not None else self._selected_coco[0]
},
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
}),
)

async def _show_manual_host_config_form(self):
"""Show the configuration form to edit NHC2 data."""
return self.async_show_form(
step_id='manual_host',
errors=self._errors,
data_schema=vol.Schema({
vol.Required(CONF_HOST, default=None): str
}),
)
1 change: 1 addition & 0 deletions custom_components/nhc2/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
COVER = 'Cover'
CONF_SWITCHES_AS_LIGHTS = 'switches_as_lights'
DEFAULT_PORT = 8883
KEY_MANUAL = 'MANUAL_IP_HOST'
8 changes: 3 additions & 5 deletions custom_components/nhc2/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ def __init__(self, nhc2light: CoCoLight, optimistic=True):
self._optimistic = optimistic
self._is_on = nhc2light.is_on
if self._nhc2light.support_brightness:
self._brightness = int((self._nhc2light.brightness + 1) * 2.54)
self._brightness = round(self._nhc2light.brightness * 2.55)
else:
self._brightness = None
nhc2light.on_change = self._on_change

def _on_change(self):
self._is_on = self._nhc2light.is_on
if self._nhc2light.support_brightness:
self._brightness = int((self._nhc2light.brightness + 1) * 2.54)
self._brightness = round(self._nhc2light.brightness * 2.55)
self.schedule_update_ha_state()

def turn_off(self, **kwargs) -> None:
Expand All @@ -59,12 +59,10 @@ async def async_turn_on(self, **kwargs):
brightness = kwargs.get(ATTR_BRIGHTNESS)

if self._nhc2light.support_brightness and brightness is not None:
self._nhc2light.brightness(int((brightness / 2.54) - 1))
self._nhc2light.set_brightness(round((brightness) / 2.55))

if self._optimistic:
self._is_on = True
if self._nhc2light.support_brightness and brightness is not None:
self._brightness = int((int((brightness / 2.54) - 1) + 1) * 2.54)
self.schedule_update_ha_state()

async def async_turn_off(self, **kwargs):
Expand Down
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==1.0.1"],
"requirements": ["nhc2-coco==1.1.3"],
"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
34 changes: 20 additions & 14 deletions custom_components/nhc2/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"config": {
"step": {
"user": {
"description": "Configuring controller {host} ({address}).\n One option is to activate the 'Niko Hobby API' on the Connected Services page at: https://mynikohomecontrol.niko.eu/nlbe/ConnectedServices/Overview or you can use a touchscreen profile. Touchscreen profiles can be create in the 'Niko Home Control programming software' under Dashboard > Touchscreens.",
"description": "Configuring controller {host}.\n One option is to activate the 'Niko Hobby API' on the Connected Services page at: https://mynikohomecontrol.niko.eu/nlbe/ConnectedServices/Overview or you can use a touchscreen profile. Touchscreen profiles can be create in the 'Niko Home Control programming software' under Dashboard > Touchscreens.",
"title": "Connect to your Niko Connected Controller II",
"data": {
"username": "Profile",
Expand All @@ -16,18 +16,24 @@
"data": {
"host": "Controller"
}
},
"manual_host": {
"description": "Provide a host or IP address",
"title": "Niko Connected Controller II",
"data": {
"host": "Address/IP"
}
},
"abort": {
"single_instance_allowed": "Controller and profile already added.",
"no_controller_found": "No controller was found."
},
"error": {
"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"
}
},
"abort": {
"single_instance_allowed": "Controller and profile already added.",
"no_controller_found": "No controller was found."
},
"error": {
"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"
}
}
}
}
9 changes: 8 additions & 1 deletion custom_components/nhc2/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"config": {
"step": {
"user": {
"description": "Configuring controller {host} ({address}).\n One option is to activate the 'Niko Hobby API' on the Connected Services page at: https://mynikohomecontrol.niko.eu/nlbe/ConnectedServices/Overview or you can use a touchscreen profile. Touchscreen profiles can be create in the 'Niko Home Control programming software' under Dashboard > Touchscreens.",
"description": "Configuring controller {host}.\n One option is to activate the 'Niko Hobby API' on the Connected Services page at: https://mynikohomecontrol.niko.eu/nlbe/ConnectedServices/Overview or you can use a touchscreen profile. Touchscreen profiles can be create in the 'Niko Home Control programming software' under Dashboard > Touchscreens.",
"title": "Connect to your Niko Connected Controller II",
"data": {
"username": "Profile",
Expand All @@ -15,6 +15,13 @@
"title": "Choose a Niko Connected Controller II",
"data": {
"host": "Controller"
},
"manual_host": {
"description": "Provide a host or IP address",
"title": "Niko Connected Controller II",
"data": {
"host": "Address/IP"
}
}
}
},
Expand Down
9 changes: 8 additions & 1 deletion custom_components/nhc2/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"config": {
"step": {
"user": {
"description": "Configureren controller {host} ({address}).\n Een optie is het activeren van de 'Niko Hobby API' op de Geconnecteerde services pagina: https://mynikohomecontrol.niko.eu/nlbe/ConnectedServices/Overview of je gebruikt een touchscreen profiel. Touchsceen profielen kunnen aangemaakt worden in de 'Niko Home Control programming software' onder Dashboard > Touchscreens",
"description": "Configureren controller {host}.\n Een optie is het activeren van de 'Niko Hobby API' op de Geconnecteerde services pagina: https://mynikohomecontrol.niko.eu/nlbe/ConnectedServices/Overview of je gebruikt een touchscreen profiel. Touchsceen profielen kunnen aangemaakt worden in de 'Niko Home Control programming software' onder Dashboard > Touchscreens",
"title": "Verbinden met je Niko Connected Controller II",
"data": {
"username": "Profiel",
Expand All @@ -16,6 +16,13 @@
"data": {
"host": "Controller"
}
},
"manual_host": {
"description": "Geef een host- of IP-adres op",
"title": "Niko Connected Controller II",
"data": {
"host": "Adres/IP"
}
}
},
"abort": {
Expand Down

0 comments on commit 2a62bb0

Please sign in to comment.