Skip to content

Commit

Permalink
Fix niko_home_control integration
Browse files Browse the repository at this point in the history
Update base library and only pull for state once.
  • Loading branch information
NoUseFreak committed Apr 14, 2019
1 parent 7d46ed0 commit 5b14f05
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
71 changes: 50 additions & 21 deletions homeassistant/components/niko_home_control/light.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
"""Support for Niko Home Control."""
import logging

import voluptuous as vol

from homeassistant.components.light import (
ATTR_BRIGHTNESS, PLATFORM_SCHEMA, Light)
from homeassistant.const import CONF_HOST
from datetime import timedelta
from homeassistant.util import Throttle

# Import the device class from the component that you want to support
from homeassistant.components.light \
import ATTR_BRIGHTNESS, Light, PLATFORM_SCHEMA
from homeassistant.exceptions import PlatformNotReady
from homeassistant.const import CONF_HOST
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['niko-home-control==0.1.8']

_LOGGER = logging.getLogger(__name__)

# Validation of the user's configuration
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
})

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=20)

def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Niko Home Control light platform."""
import nikohomecontrol

async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
"""Setup the Niko Light platform."""
import nikohomecontrol
host = config[CONF_HOST]

try:
hub = nikohomecontrol.Hub({
nhc = nikohomecontrol.NikoHomeControl({
'ip': host,
'port': 8000,
'timeout': 20000,
'events': True
'timeout': 20000
})
nikoData = NikoHomeControlData(nhc)
await nikoData.async_update()
except OSError as err:
_LOGGER.error("Unable to access %s (%s)", host, err)
raise PlatformNotReady

add_entities(
[NikoHomeControlLight(light, hub) for light in hub.list_actions()],
async_add_entities(
[Niko2Light(light, nikoData) for light in nhc.list_actions()],
True)


class NikoHomeControlLight(Light):
class Niko2Light(Light):
"""Representation of an Niko Light."""

def __init__(self, light, nhc):
def __init__(self, light, data):
"""Set up the Niko Home Control light platform."""
self._nhc = nhc
self._data = data
self._light = light
self._name = light.name
self._state = None
Expand Down Expand Up @@ -75,7 +80,31 @@ def turn_off(self, **kwargs):
"""Instruct the light to turn off."""
self._light.turn_off()

def update(self):
"""Fetch new state data for this light."""
self._light.update()
self._state = self._light.is_on
async def async_update(self):
"""Get the latest data from NikoHomeControl API."""
await self._data.async_update()
self._state = self._data.getState(self._light.id)


class NikoHomeControlData:
"""The class for handling data retrieval."""

def __init__(self, nhc):
"""Set up Niko Home Control Data object"""
self._nhc = nhc
self.available = True
self.data = {}

@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self):
"""Get the latest data from the NikoHomeControl API."""
try:
self.data = self._nhc.list_actions_raw()
self.available = True
except Exception as e:
_LOGGER.error("Unable to retrieve data from Niko, %s" % e)
self.available = False

def getState(self, aid):
"""Find and filter state based on action id."""
return next(filter(lambda a: a['id'] == aid, self.data))['value1'] != 0
2 changes: 1 addition & 1 deletion homeassistant/components/niko_home_control/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Niko home control",
"documentation": "https://www.home-assistant.io/components/niko_home_control",
"requirements": [
"niko-home-control==0.1.8"
"niko-home-control==0.2.0"
],
"dependencies": [],
"codeowners": []
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ netdisco==2.6.0
neurio==0.3.1

# homeassistant.components.niko_home_control
niko-home-control==0.1.8
niko-home-control==0.2.0

# homeassistant.components.nilu
niluclient==0.1.2
Expand Down

0 comments on commit 5b14f05

Please sign in to comment.