Skip to content

Commit

Permalink
Merge pull request #7 from filipvh/buffer_commands
Browse files Browse the repository at this point in the history
Buffered commands.
  • Loading branch information
filipvh authored Nov 27, 2020
2 parents ea2812f + 13eea40 commit 1b46236
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 12 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.5']
REQUIREMENTS = ['nhc2-coco==1.0.0']

_LOGGER = logging.getLogger(__name__)

Expand Down
1 change: 1 addition & 0 deletions custom_components/nhc2/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
BRAND = 'Niko'
LIGHT = 'Light'
SWITCH = 'Switch'
COVER = 'Cover'
CONF_SWITCHES_AS_LIGHTS = 'switches_as_lights'
DEFAULT_PORT = 8883
134 changes: 134 additions & 0 deletions custom_components/nhc2/cover.py
Original file line number Diff line number Diff line change
@@ -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),
}
18 changes: 9 additions & 9 deletions custom_components/nhc2/light.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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):
Expand Down Expand Up @@ -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):
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==0.2.5"],
"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",
Expand Down
3 changes: 2 additions & 1 deletion custom_components/nhc2/switch.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 1b46236

Please sign in to comment.