From e90870e379e0961822c33a0dde1eef483d880837 Mon Sep 17 00:00:00 2001 From: Levi Govaerts Date: Tue, 30 Oct 2018 18:54:53 +0100 Subject: [PATCH 1/4] Add niko-home-control support --- .coveragerc | 1 + .../components/light/niko_home_control.py | 102 ++++++++++++++++++ .../components/sensor/niko_home_control.py | 102 ++++++++++++++++++ requirements_all.txt | 3 + 4 files changed, 208 insertions(+) create mode 100644 homeassistant/components/light/niko_home_control.py create mode 100644 homeassistant/components/sensor/niko_home_control.py diff --git a/.coveragerc b/.coveragerc index dea02d21f567d5..7bfa8e857df5f4 100644 --- a/.coveragerc +++ b/.coveragerc @@ -525,6 +525,7 @@ omit = homeassistant/components/light/lw12wifi.py homeassistant/components/light/mystrom.py homeassistant/components/light/nanoleaf_aurora.py + homeassistant/components/light/niko_home_control.py homeassistant/components/light/opple.py homeassistant/components/light/osramlightify.py homeassistant/components/light/piglow.py diff --git a/homeassistant/components/light/niko_home_control.py b/homeassistant/components/light/niko_home_control.py new file mode 100644 index 00000000000000..edf5804eef0099 --- /dev/null +++ b/homeassistant/components/light/niko_home_control.py @@ -0,0 +1,102 @@ +""" +Support for Niko Home Control. + +For more details about this platform, please refer to the documentation +https://home-assistant.io/components/light.niko_home_control/ +""" +import logging +import socket +import voluptuous as vol + +# Import the device class from the component that you want to support +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, Light, PLATFORM_SCHEMA) +from homeassistant.const import CONF_HOST +from homeassistant.exceptions import PlatformNotReady +import homeassistant.helpers.config_validation as cv + +# Home Assistant depends on 3rd party packages for API specific code. +REQUIREMENTS = ['niko-home-control==0.1.8'] + +_LOGGER = logging.getLogger(__name__) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_HOST): cv.string, +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the NikoHomeControl platform.""" + import nikohomecontrol + + host = config.get(CONF_HOST) + + try: + hub = nikohomecontrol.Hub({ + 'ip': host, + 'port': 8000, + 'timeout': 20000, + 'events': True + }) + except socket.error as err: + _LOGGER.error('Unable to access %s (%s)', host, err) + raise PlatformNotReady + + add_devices( + [NikoHomeControlLight(light, hub) for light in hub.list_actions()], + True + ) + + +class NikoHomeControlLight(Light): + """Representation of an Niko Light.""" + + def __init__(self, light, nhc): + """Set up the Niko Home Control platform.""" + self._nhc = nhc + self._light = light + self._name = light.name + self._state = None + self._brightness = None + + @property + def name(self): + """Return the display name of this light.""" + return self._name + + @property + def brightness(self): + """Return the brightness of the light. + + This method is optional. Removing it indicates to Home Assistant + that brightness is not supported for this light. + """ + return self._brightness + + @property + def is_on(self): + """Return true if light is on.""" + return self._state + + def turn_on(self, **kwargs): + """Instruct the light to turn on. + + You can skip the brightness part if your light does not support + brightness control. + """ + self._light.brightness = kwargs.get(ATTR_BRIGHTNESS, 255) + self._light.turn_on() + self._state = True + + def turn_off(self, **kwargs): + """Instruct the light to turn off.""" + self._light.turn_off() + self._state = False + + def update(self): + """Fetch new state data for this light. + + This is the only method that should fetch new data for Home Assistant. + """ + self._light.update() + self._state = self._light.is_on \ No newline at end of file diff --git a/homeassistant/components/sensor/niko_home_control.py b/homeassistant/components/sensor/niko_home_control.py new file mode 100644 index 00000000000000..10161fc8e80159 --- /dev/null +++ b/homeassistant/components/sensor/niko_home_control.py @@ -0,0 +1,102 @@ +""" +Support for Niko Home Control. + +For more details about this platform, please refer to the documentation +https://home-assistant.io/components/light.niko_home_control/ +""" +import logging +import socket +import voluptuous as vol + +# Import the device class from the component that you want to support +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, Light, PLATFORM_SCHEMA) +from homeassistant.const import CONF_HOST +from homeassistant.exceptions import PlatformNotReady +import homeassistant.helpers.config_validation as cv + +# Home Assistant depends on 3rd party packages for API specific code. +REQUIREMENTS = ['niko-home-control==0.1.8'] + +_LOGGER = logging.getLogger(__name__) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_HOST): cv.string, +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the NikoHomeControl platform.""" + import nikohomecontrol + + host = config.get(CONF_HOST) + + try: + hub = nikohomecontrol.Hub({ + 'ip': host, + 'port': 8000, + 'timeout': 20000, + 'events': True + }) + except socket.error as err: + _LOGGER.error('Unable to access %s (%s)', host, err) + raise PlatformNotReady + + add_devices( + [NikoHomeControlLight(light, hub) for light in hub.list_actions()], + True + ) + + +class NikoHomeControlLight(Light): + """Representation of an Niko Light.""" + + def __init__(self, light, nhc): + """Set up the Niko Home Control platform.""" + self._nhc = nhc + self._light = light + self._name = light.name + self._state = None + self._brightness = None + + @property + def name(self): + """Return the display name of this light.""" + return self._name + + @property + def brightness(self): + """Return the brightness of the light. + + This method is optional. Removing it indicates to Home Assistant + that brightness is not supported for this light. + """ + return self._brightness + + @property + def is_on(self): + """Return true if light is on.""" + return self._state + + def turn_on(self, **kwargs): + """Instruct the light to turn on. + + You can skip the brightness part if your light does not support + brightness control. + """ + self._light.brightness = kwargs.get(ATTR_BRIGHTNESS, 255) + self._light.turn_on() + self._state = True + + def turn_off(self, **kwargs): + """Instruct the light to turn off.""" + self._light.turn_off() + self._state = False + + def update(self): + """Fetch new state data for this light. + + This is the only method that should fetch new data for Home Assistant. + """ + self._light.update() + self._state = self._light.is_on diff --git a/requirements_all.txt b/requirements_all.txt index e46fdeb3a6e015..e4dd0ef1ecb9c7 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -654,6 +654,9 @@ netdisco==2.2.0 # homeassistant.components.sensor.neurio_energy neurio==0.3.1 +# homeassistant.components.light.niko_home_control +niko-home-control==0.1.8 + # homeassistant.components.sensor.nederlandse_spoorwegen nsapi==2.7.4 From b78fc089f0a26ed11bab8f423498e6ed565e8e67 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 12 Nov 2018 11:38:39 +0100 Subject: [PATCH 2/4] Remove the sensor platform --- .../components/sensor/niko_home_control.py | 102 ------------------ 1 file changed, 102 deletions(-) delete mode 100644 homeassistant/components/sensor/niko_home_control.py diff --git a/homeassistant/components/sensor/niko_home_control.py b/homeassistant/components/sensor/niko_home_control.py deleted file mode 100644 index 10161fc8e80159..00000000000000 --- a/homeassistant/components/sensor/niko_home_control.py +++ /dev/null @@ -1,102 +0,0 @@ -""" -Support for Niko Home Control. - -For more details about this platform, please refer to the documentation -https://home-assistant.io/components/light.niko_home_control/ -""" -import logging -import socket -import voluptuous as vol - -# Import the device class from the component that you want to support -from homeassistant.components.light import ( - ATTR_BRIGHTNESS, Light, PLATFORM_SCHEMA) -from homeassistant.const import CONF_HOST -from homeassistant.exceptions import PlatformNotReady -import homeassistant.helpers.config_validation as cv - -# Home Assistant depends on 3rd party packages for API specific code. -REQUIREMENTS = ['niko-home-control==0.1.8'] - -_LOGGER = logging.getLogger(__name__) - -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ - vol.Required(CONF_HOST): cv.string, -}) - - -def setup_platform(hass, config, add_devices, discovery_info=None): - """Setup the NikoHomeControl platform.""" - import nikohomecontrol - - host = config.get(CONF_HOST) - - try: - hub = nikohomecontrol.Hub({ - 'ip': host, - 'port': 8000, - 'timeout': 20000, - 'events': True - }) - except socket.error as err: - _LOGGER.error('Unable to access %s (%s)', host, err) - raise PlatformNotReady - - add_devices( - [NikoHomeControlLight(light, hub) for light in hub.list_actions()], - True - ) - - -class NikoHomeControlLight(Light): - """Representation of an Niko Light.""" - - def __init__(self, light, nhc): - """Set up the Niko Home Control platform.""" - self._nhc = nhc - self._light = light - self._name = light.name - self._state = None - self._brightness = None - - @property - def name(self): - """Return the display name of this light.""" - return self._name - - @property - def brightness(self): - """Return the brightness of the light. - - This method is optional. Removing it indicates to Home Assistant - that brightness is not supported for this light. - """ - return self._brightness - - @property - def is_on(self): - """Return true if light is on.""" - return self._state - - def turn_on(self, **kwargs): - """Instruct the light to turn on. - - You can skip the brightness part if your light does not support - brightness control. - """ - self._light.brightness = kwargs.get(ATTR_BRIGHTNESS, 255) - self._light.turn_on() - self._state = True - - def turn_off(self, **kwargs): - """Instruct the light to turn off.""" - self._light.turn_off() - self._state = False - - def update(self): - """Fetch new state data for this light. - - This is the only method that should fetch new data for Home Assistant. - """ - self._light.update() - self._state = self._light.is_on From 5aeeadac9a0f05c952b54af0009ecacdd3370c5c Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 12 Nov 2018 11:44:24 +0100 Subject: [PATCH 3/4] Minor changes --- .../components/light/niko_home_control.py | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/light/niko_home_control.py b/homeassistant/components/light/niko_home_control.py index edf5804eef0099..d320a6159ccb1c 100644 --- a/homeassistant/components/light/niko_home_control.py +++ b/homeassistant/components/light/niko_home_control.py @@ -6,16 +6,15 @@ """ import logging import socket + import voluptuous as vol -# Import the device class from the component that you want to support from homeassistant.components.light import ( - ATTR_BRIGHTNESS, Light, PLATFORM_SCHEMA) + ATTR_BRIGHTNESS, PLATFORM_SCHEMA, Light) from homeassistant.const import CONF_HOST from homeassistant.exceptions import PlatformNotReady import homeassistant.helpers.config_validation as cv -# Home Assistant depends on 3rd party packages for API specific code. REQUIREMENTS = ['niko-home-control==0.1.8'] _LOGGER = logging.getLogger(__name__) @@ -44,15 +43,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None): add_devices( [NikoHomeControlLight(light, hub) for light in hub.list_actions()], - True - ) + True) class NikoHomeControlLight(Light): """Representation of an Niko Light.""" def __init__(self, light, nhc): - """Set up the Niko Home Control platform.""" + """Set up the Niko Home Control light platform.""" self._nhc = nhc self._light = light self._name = light.name @@ -66,11 +64,7 @@ def name(self): @property def brightness(self): - """Return the brightness of the light. - - This method is optional. Removing it indicates to Home Assistant - that brightness is not supported for this light. - """ + """Return the brightness of the light.""" return self._brightness @property @@ -79,11 +73,7 @@ def is_on(self): return self._state def turn_on(self, **kwargs): - """Instruct the light to turn on. - - You can skip the brightness part if your light does not support - brightness control. - """ + """Instruct the light to turn on.""" self._light.brightness = kwargs.get(ATTR_BRIGHTNESS, 255) self._light.turn_on() self._state = True @@ -94,9 +84,6 @@ def turn_off(self, **kwargs): self._state = False def update(self): - """Fetch new state data for this light. - - This is the only method that should fetch new data for Home Assistant. - """ + """Fetch new state data for this light.""" self._light.update() - self._state = self._light.is_on \ No newline at end of file + self._state = self._light.is_on From ecaaca70f257ccbe921aae78c6a0ce8f24f0321e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 12 Nov 2018 13:14:59 +0100 Subject: [PATCH 4/4] Fix docstring --- homeassistant/components/light/niko_home_control.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/light/niko_home_control.py b/homeassistant/components/light/niko_home_control.py index d320a6159ccb1c..3146954ed628e9 100644 --- a/homeassistant/components/light/niko_home_control.py +++ b/homeassistant/components/light/niko_home_control.py @@ -25,7 +25,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): - """Setup the NikoHomeControl platform.""" + """Set up the Niko Home Control light platform.""" import nikohomecontrol host = config.get(CONF_HOST) @@ -38,7 +38,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): 'events': True }) except socket.error as err: - _LOGGER.error('Unable to access %s (%s)', host, err) + _LOGGER.error("Unable to access %s (%s)", host, err) raise PlatformNotReady add_devices(