From 3880d10b75d74e0050b951c06e41137cab079dc6 Mon Sep 17 00:00:00 2001 From: fiva Date: Wed, 25 Nov 2020 13:47:36 +0100 Subject: [PATCH 1/3] test using nhc2-coco version 0.3.0-alpha1 with experimental buffered commands --- custom_components/nhc2/__init__.py | 2 +- custom_components/nhc2/manifest.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/nhc2/__init__.py b/custom_components/nhc2/__init__.py index 05b510b2..efb362b8 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.5'] +REQUIREMENTS = ['nhc2-coco==0.3.0-alpha1'] _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/nhc2/manifest.json b/custom_components/nhc2/manifest.json index 989899e3..a1e40a67 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.5"], + "requirements": ["nhc2-coco==0.3.0-alpha1"], "config_flow": true, "issue_tracker": "https://github.com/filipvh/hass-nhc2/issues", "documentation": "https://github.com/filipvh/hass-nhc2/blob/master/README.md", From 5637492ccc3610e49d71d7d19f87a90c6edb45a5 Mon Sep 17 00:00:00 2001 From: fiva Date: Wed, 25 Nov 2020 23:19:59 +0100 Subject: [PATCH 2/3] Bump to latest alpha release of nhc2-coco and initial trial for covers/shutters --- custom_components/nhc2/__init__.py | 2 +- custom_components/nhc2/const.py | 1 + custom_components/nhc2/cover.py | 134 +++++++++++++++++++++++++++ custom_components/nhc2/light.py | 18 ++-- custom_components/nhc2/manifest.json | 2 +- custom_components/nhc2/switch.py | 3 +- 6 files changed, 148 insertions(+), 12 deletions(-) create mode 100644 custom_components/nhc2/cover.py diff --git a/custom_components/nhc2/__init__.py b/custom_components/nhc2/__init__.py index efb362b8..404944c7 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.3.0-alpha1'] +REQUIREMENTS = ['nhc2-coco==0.3.0-alpha4'] _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/nhc2/const.py b/custom_components/nhc2/const.py index 6db3a1e4..ababe9ed 100644 --- a/custom_components/nhc2/const.py +++ b/custom_components/nhc2/const.py @@ -6,5 +6,6 @@ BRAND = 'Niko' LIGHT = 'Light' SWITCH = 'Switch' +COVER = 'Cover' CONF_SWITCHES_AS_LIGHTS = 'switches_as_lights' DEFAULT_PORT = 8883 diff --git a/custom_components/nhc2/cover.py b/custom_components/nhc2/cover.py new file mode 100644 index 00000000..adc0740d --- /dev/null +++ b/custom_components/nhc2/cover.py @@ -0,0 +1,134 @@ +"""Support for NHC2 switches.""" +import logging + +from homeassistant.components.cover import CoverEntity, SUPPORT_OPEN, SUPPORT_CLOSE, SUPPORT_STOP, SUPPORT_SET_POSITION, \ + ATTR_POSITION +from nhc2_coco import CoCo +from nhc2_coco.coco_device_class import CoCoDeviceClass +from nhc2_coco.coco_shutter import CoCoShutter + +from .const import DOMAIN, KEY_GATEWAY, BRAND, COVER +from .helpers import nhc2_entity_processor + +KEY_GATEWAY = KEY_GATEWAY +KEY_ENTITY = 'nhc2_covers' + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Load NHC2 covers based on a config entry.""" + hass.data.setdefault(KEY_ENTITY, {})[config_entry.entry_id] = [] + gateway: CoCo = hass.data[KEY_GATEWAY][config_entry.entry_id] + _LOGGER.debug('Platform is starting') + gateway.get_devices(CoCoDeviceClass.SHUTTERS, + nhc2_entity_processor(hass, + config_entry, + async_add_entities, + KEY_ENTITY, + lambda x: NHC2HassCover(x)) + ) + + +class NHC2HassCover(CoverEntity): + """Representation of an NHC2 Switch.""" + + def __init__(self, nhc2shutter: CoCoShutter): + """Initialize a switch.""" + self._nhc2shutter = nhc2shutter + self._is_closed = (nhc2shutter.position == 0) + nhc2shutter.on_change = self._on_change + + def current_cover_position(self): + """Return current position of cover. 0 is closed, 100 is open.""" + return self._nhc2shutter.position + + @property + def supported_features(self) -> int: + """Flag supported features.""" + return SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP | SUPPORT_SET_POSITION + + def _on_change(self): + self._is_closed = (self._nhc2shutter == 0) + self.schedule_update_ha_state() + + def open_cover(self, **kwargs) -> None: + """Pass - not in use.""" + pass + + def close_cover(self, **kwargs) -> None: + """Pass - not in use.""" + pass + + def stop_cover(self, **kwargs) -> None: + """Pass - not in use.""" + pass + + def set_cover_position(self, **kwargs) -> None: + """Pass - not in use.""" + pass + + async def async_open_cover(self, **kwargs): + """Instruct the cover to open.""" + self._nhc2shutter.open() + + async def async_close_cover(self, **kwargs): + """Instruct the cover to close.""" + self._nhc2shutter.close() + + async def async_stop_cover(self, **kwargs): + """Instruct the cover to stop.""" + self._nhc2shutter.stop() + + async def async_set_cover_position(self, **kwargs): + """Instruct the cover to stop.""" + self._nhc2shutter.set_position(kwargs[ATTR_POSITION]) + + def nhc2_update(self, nhc2shutter: CoCoShutter): + """Update the NHC2 switch with a new object.""" + self._nhc2shutter = nhc2shutter + nhc2shutter.on_change = self._on_change + self.schedule_update_ha_state() + + @property + def unique_id(self): + """Return the lights UUID.""" + return self._nhc2shutter.uuid + + @property + def uuid(self): + """Return the lights UUID.""" + return self._nhc2shutter.uuid + + @property + def should_poll(self): + """Return false, since the cover will push state.""" + return False + + @property + def name(self): + """Return the lights name.""" + return self._nhc2shutter.name + + @property + def available(self): + """Return true if the light is online.""" + return self._nhc2shutter.online + + @property + def is_closed(self): + """Return true if the light is on.""" + return self._is_closed + + @property + def device_info(self): + """Return the device info.""" + return { + 'identifiers': { + (DOMAIN, self.unique_id) + }, + 'name': self.name, + 'manufacturer': BRAND, + 'model': COVER, + 'via_hub': (DOMAIN, self._nhc2shutter.profile_creation_id), + } diff --git a/custom_components/nhc2/light.py b/custom_components/nhc2/light.py index 0fbb1dca..2718cde9 100644 --- a/custom_components/nhc2/light.py +++ b/custom_components/nhc2/light.py @@ -1,12 +1,12 @@ """Support for NHC2 lights.""" import logging -from typing import List -from homeassistant.components.light import LightEntity, SUPPORT_BRIGHTNESS, ATTR_BRIGHTNESS -from .helpers import nhc2_entity_processor +from homeassistant.components.light import LightEntity, SUPPORT_BRIGHTNESS, ATTR_BRIGHTNESS from nhc2_coco import CoCoLight, CoCo +from nhc2_coco.coco_device_class import CoCoDeviceClass from .const import DOMAIN, KEY_GATEWAY, BRAND, LIGHT +from .helpers import nhc2_entity_processor KEY_GATEWAY = KEY_GATEWAY KEY_ENTITY = 'nhc2_lights' @@ -19,10 +19,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities): hass.data.setdefault(KEY_ENTITY, {})[config_entry.entry_id] = [] gateway: CoCo = hass.data[KEY_GATEWAY][config_entry.entry_id] _LOGGER.debug('Platform is starting') - gateway.get_lights( - nhc2_entity_processor(hass, config_entry, async_add_entities, - KEY_ENTITY, lambda x: NHC2HassLight(x)) - ) + gateway.get_devices(CoCoDeviceClass.LIGHTS, + nhc2_entity_processor(hass, config_entry, async_add_entities, + KEY_ENTITY, lambda x: NHC2HassLight(x)) + ) class NHC2HassLight(LightEntity): @@ -59,12 +59,12 @@ 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.brightness(int((brightness / 2.54) - 1)) 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._brightness = int((int((brightness / 2.54) - 1) + 1) * 2.54) self.schedule_update_ha_state() async def async_turn_off(self, **kwargs): diff --git a/custom_components/nhc2/manifest.json b/custom_components/nhc2/manifest.json index a1e40a67..5e101178 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.3.0-alpha1"], + "requirements": ["nhc2-coco==0.3.0-alpha4"], "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/switch.py b/custom_components/nhc2/switch.py index 7c9d7c5f..e27d2059 100644 --- a/custom_components/nhc2/switch.py +++ b/custom_components/nhc2/switch.py @@ -1,6 +1,7 @@ """Support for NHC2 switches.""" import logging from homeassistant.components.switch import SwitchEntity +from nhc2_coco.coco_device_class import CoCoDeviceClass from .helpers import nhc2_entity_processor from nhc2_coco import CoCo, CoCoSwitch @@ -18,7 +19,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): hass.data.setdefault(KEY_ENTITY, {})[config_entry.entry_id] = [] gateway: CoCo = hass.data[KEY_GATEWAY][config_entry.entry_id] _LOGGER.debug('Platform is starting') - gateway.get_switches( + gateway.get_devices(CoCoDeviceClass.SWITCHES, nhc2_entity_processor(hass, config_entry, async_add_entities, From 13eea408cf347e17cc26086dd3c406081066dc03 Mon Sep 17 00:00:00 2001 From: fiva Date: Fri, 27 Nov 2020 20:45:46 +0100 Subject: [PATCH 3/3] release 0.3.0 --- custom_components/nhc2/__init__.py | 2 +- custom_components/nhc2/manifest.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/nhc2/__init__.py b/custom_components/nhc2/__init__.py index 404944c7..259e66b1 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.3.0-alpha4'] +REQUIREMENTS = ['nhc2-coco==1.0.0'] _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/nhc2/manifest.json b/custom_components/nhc2/manifest.json index 5e101178..ac7a7f5f 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.3.0-alpha4"], + "requirements": ["nhc2-coco==1.0.0"], "config_flow": true, "issue_tracker": "https://github.com/filipvh/hass-nhc2/issues", "documentation": "https://github.com/filipvh/hass-nhc2/blob/master/README.md",