This commit is contained in:
Paulus Schoutsen 2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View file

@ -5,8 +5,7 @@ import logging
import voluptuous as vol
# Import the device class from the component that you want to support
from homeassistant.components.light import (
ATTR_BRIGHTNESS, PLATFORM_SCHEMA, Light)
from homeassistant.components.light import ATTR_BRIGHTNESS, PLATFORM_SCHEMA, Light
from homeassistant.const import CONF_HOST
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv
@ -16,32 +15,28 @@ _LOGGER = logging.getLogger(__name__)
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=1)
SCAN_INTERVAL = timedelta(seconds=30)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_HOST): cv.string})
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Niko Home Control light platform."""
import nikohomecontrol
host = config[CONF_HOST]
try:
nhc = nikohomecontrol.NikoHomeControl({
'ip': host,
'port': 8000,
'timeout': 20000
})
nhc = nikohomecontrol.NikoHomeControl(
{"ip": host, "port": 8000, "timeout": 20000}
)
niko_data = NikoHomeControlData(hass, nhc)
await niko_data.async_update()
except OSError as err:
_LOGGER.error("Unable to access %s (%s)", host, err)
raise PlatformNotReady
async_add_entities([
NikoHomeControlLight(light, niko_data) for light in nhc.list_actions()
], True)
async_add_entities(
[NikoHomeControlLight(light, niko_data) for light in nhc.list_actions()], True
)
class NikoHomeControlLight(Light):
@ -79,12 +74,12 @@ class NikoHomeControlLight(Light):
def turn_on(self, **kwargs):
"""Instruct the light to turn on."""
self._light.brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
_LOGGER.debug('Turn on: %s', self.name)
_LOGGER.debug("Turn on: %s", self.name)
self._light.turn_on()
def turn_off(self, **kwargs):
"""Instruct the light to turn off."""
_LOGGER.debug('Turn off: %s', self.name)
_LOGGER.debug("Turn off: %s", self.name)
self._light.turn_off()
async def async_update(self):
@ -107,10 +102,11 @@ class NikoHomeControlData:
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self):
"""Get the latest data from the NikoHomeControl API."""
_LOGGER.debug('Fetching async state in bulk')
_LOGGER.debug("Fetching async state in bulk")
try:
self.data = await self.hass.async_add_executor_job(
self._nhc.list_actions_raw)
self._nhc.list_actions_raw
)
self.available = True
except OSError as ex:
_LOGGER.error("Unable to retrieve data from Niko, %s", str(ex))
@ -119,6 +115,6 @@ class NikoHomeControlData:
def get_state(self, aid):
"""Find and filter state based on action id."""
for state in self.data:
if state['id'] == aid:
return state['value1'] != 0
if state["id"] == aid:
return state["value1"] != 0
_LOGGER.error("Failed to retrieve state off unknown light")