Add separate on/off ids on manual configured IHC lights (#20253)

* Add support for separate on/off ids on manual configured IHC lights.
This makes it easier to support IHC code units thats relies on being
turned on and off through specific inputs.
Also adds a pulse service (ihc.pulse) that supports sending a short on/off pulse to an IHC input.

* Fix

* Lint fix

* Add on/off id support in switch

* Make pulse async

* Code review fixes
This commit is contained in:
msvinth authored and Martin Hjelmare committed 2019-03-01 08:17:59 +01:00
1 parent ee4be13bda
commit 0aba49adce
6 files changed
+109 -23

No files matched your search

+31 -12
View File
@@ -2,7 +2,10 @@
import logging
from homeassistant.components.ihc import IHC_CONTROLLER, IHC_DATA, IHC_INFO
from homeassistant.components.ihc.const import CONF_DIMMABLE
from homeassistant.components.ihc.const import (
CONF_DIMMABLE, CONF_OFF_ID, CONF_ON_ID)
from homeassistant.components.ihc.util import (
async_pulse, async_set_bool, async_set_int)
from homeassistant.components.ihc.ihcdevice import IHCDevice
from homeassistant.components.light import (
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light)
@@ -26,9 +29,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
ihc_key = IHC_DATA.format(ctrl_id)
info = hass.data[ihc_key][IHC_INFO]
ihc_controller = hass.data[ihc_key][IHC_CONTROLLER]
ihc_off_id = product_cfg.get(CONF_OFF_ID)
ihc_on_id = product_cfg.get(CONF_ON_ID)
dimmable = product_cfg[CONF_DIMMABLE]
light = IhcLight(ihc_controller, name, ihc_id, info,
dimmable, product)
light = IhcLight(ihc_controller, name, ihc_id, ihc_off_id, ihc_on_id,
info, dimmable, product)
devices.append(light)
add_entities(devices)
@@ -41,10 +46,13 @@ class IhcLight(IHCDevice, Light):
an on/off (boolean) resource
"""
def __init__(self, ihc_controller, name, ihc_id: int, info: bool,
dimmable=False, product=None) -> None:
def __init__(self, ihc_controller, name, ihc_id: int, ihc_off_id: int,
ihc_on_id: int, info: bool, dimmable=False,
product=None) -> None:
"""Initialize the light."""
super().__init__(ihc_controller, name, ihc_id, info, product)
self._ihc_off_id = ihc_off_id
self._ihc_on_id = ihc_on_id
self._brightness = 0
self._dimmable = dimmable
self._state = None
@@ -66,7 +74,7 @@ class IhcLight(IHCDevice, Light):
return SUPPORT_BRIGHTNESS
return 0
def turn_on(self, **kwargs) -> None:
async def async_turn_on(self, **kwargs):
"""Turn the light on."""
if ATTR_BRIGHTNESS in kwargs:
brightness = kwargs[ATTR_BRIGHTNESS]
@@ -76,17 +84,28 @@ class IhcLight(IHCDevice, Light):
brightness = 255
if self._dimmable:
self.ihc_controller.set_runtime_value_int(
self.ihc_id, int(brightness * 100 / 255))
await async_set_int(self.hass, self.ihc_controller,
self.ihc_id, int(brightness * 100 / 255))
else:
self.ihc_controller.set_runtime_value_bool(self.ihc_id, True)
if self._ihc_on_id:
await async_pulse(self.hass, self.ihc_controller,
self._ihc_on_id)
else:
await async_set_bool(self.hass, self.ihc_controller,
self.ihc_id, True)
def turn_off(self, **kwargs) -> None:
async def async_turn_off(self, **kwargs):
"""Turn the light off."""
if self._dimmable:
self.ihc_controller.set_runtime_value_int(self.ihc_id, 0)
await async_set_int(self.hass, self.ihc_controller,
self.ihc_id, 0)
else:
self.ihc_controller.set_runtime_value_bool(self.ihc_id, False)
if self._ihc_off_id:
await async_pulse(self.hass, self.ihc_controller,
self._ihc_off_id)
else:
await async_set_bool(self.hass, self.ihc_controller,
self.ihc_id, False)
def on_ihc_change(self, ihc_id, value):
"""Handle IHC notifications."""