From bc7c2f3df05cae1ac34c01346b96ba981dc2e0e0 Mon Sep 17 00:00:00 2001 From: Michael Svinth Date: Sat, 19 Jan 2019 19:48:09 +0100 Subject: [PATCH 1/6] 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. --- homeassistant/components/ihc/__init__.py | 24 +++++++++++++++--- homeassistant/components/ihc/const.py | 3 +++ homeassistant/components/ihc/light.py | 29 ++++++++++++++++------ homeassistant/components/ihc/services.yaml | 5 ++++ homeassistant/components/ihc/util.py | 9 +++++++ 5 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 homeassistant/components/ihc/util.py diff --git a/homeassistant/components/ihc/__init__.py b/homeassistant/components/ihc/__init__.py index bd45a52944cc8..15e4cc51ba6c8 100644 --- a/homeassistant/components/ihc/__init__.py +++ b/homeassistant/components/ihc/__init__.py @@ -7,9 +7,11 @@ from homeassistant.components.binary_sensor import DEVICE_CLASSES_SCHEMA from homeassistant.components.ihc.const import ( ATTR_IHC_ID, ATTR_VALUE, CONF_AUTOSETUP, CONF_BINARY_SENSOR, CONF_DIMMABLE, - CONF_INFO, CONF_INVERTING, CONF_LIGHT, CONF_NODE, CONF_NOTE, CONF_POSITION, - CONF_SENSOR, CONF_SWITCH, CONF_XPATH, SERVICE_SET_RUNTIME_VALUE_BOOL, - SERVICE_SET_RUNTIME_VALUE_FLOAT, SERVICE_SET_RUNTIME_VALUE_INT) + CONF_INFO, CONF_INVERTING, CONF_LIGHT, CONF_NODE, CONF_NOTE, CONF_OFF_ID, + CONF_ON_ID, CONF_POSITION, CONF_SENSOR, CONF_SWITCH, CONF_XPATH, + SERVICE_SET_RUNTIME_VALUE_BOOL, SERVICE_SET_RUNTIME_VALUE_FLOAT, + SERVICE_SET_RUNTIME_VALUE_INT, SERVICE_PULSE) +from homeassistant.components.ihc.util import pulse from homeassistant.config import load_yaml_config_file from homeassistant.const import ( CONF_ID, CONF_NAME, CONF_PASSWORD, CONF_TYPE, CONF_UNIT_OF_MEASUREMENT, @@ -59,6 +61,8 @@ def validate_name(config): LIGHT_SCHEMA = DEVICE_SCHEMA.extend({ vol.Optional(CONF_DIMMABLE, default=False): cv.boolean, + vol.Optional(CONF_OFF_ID, default=0): cv.positive_int, + vol.Optional(CONF_ON_ID, default=0): cv.positive_int, }) SENSOR_SCHEMA = DEVICE_SCHEMA.extend({ @@ -138,6 +142,10 @@ def validate_name(config): vol.Required(ATTR_VALUE): vol.Coerce(float), }) +PULSE_SCHEMA = vol.Schema({ + vol.Required(ATTR_IHC_ID): cv.positive_int, +}) + def setup(hass, config): """Set up the IHC platform.""" @@ -197,6 +205,8 @@ def get_manual_configuration( 'product_cfg': { 'type': sensor_cfg.get(CONF_TYPE), 'inverting': sensor_cfg.get(CONF_INVERTING), + 'off_id': sensor_cfg.get(CONF_OFF_ID), + 'on_id': sensor_cfg.get(CONF_ON_ID), 'dimmable': sensor_cfg.get(CONF_DIMMABLE), 'unit_of_measurement': sensor_cfg.get( CONF_UNIT_OF_MEASUREMENT) @@ -287,6 +297,11 @@ def set_runtime_value_float(call): value = call.data[ATTR_VALUE] ihc_controller.set_runtime_value_float(ihc_id, value) + def pulse_runtime_input(call): + """Pulse a IHC controller input function.""" + ihc_id = call.data[ATTR_IHC_ID] + pulse(ihc_controller, ihc_id) + hass.services.register(DOMAIN, SERVICE_SET_RUNTIME_VALUE_BOOL, set_runtime_value_bool, schema=SET_RUNTIME_VALUE_BOOL_SCHEMA) @@ -296,3 +311,6 @@ def set_runtime_value_float(call): hass.services.register(DOMAIN, SERVICE_SET_RUNTIME_VALUE_FLOAT, set_runtime_value_float, schema=SET_RUNTIME_VALUE_FLOAT_SCHEMA) + hass.services.register(DOMAIN, SERVICE_PULSE, + pulse_runtime_input, + schema=PULSE_SCHEMA) diff --git a/homeassistant/components/ihc/const.py b/homeassistant/components/ihc/const.py index 69342c944ba7a..2199a8a156e31 100644 --- a/homeassistant/components/ihc/const.py +++ b/homeassistant/components/ihc/const.py @@ -9,6 +9,8 @@ CONF_NAME = 'name' CONF_NODE = 'node' CONF_NOTE = 'note' +CONF_OFF_ID = 'off_id' +CONF_ON_ID = 'on_id' CONF_POSITION = 'position' CONF_SENSOR = 'sensor' CONF_SWITCH = 'switch' @@ -20,3 +22,4 @@ SERVICE_SET_RUNTIME_VALUE_BOOL = 'set_runtime_value_bool' SERVICE_SET_RUNTIME_VALUE_FLOAT = 'set_runtime_value_float' SERVICE_SET_RUNTIME_VALUE_INT = 'set_runtime_value_int' +SERVICE_PULSE = 'pulse' diff --git a/homeassistant/components/ihc/light.py b/homeassistant/components/ihc/light.py index 2590ea832221e..e6de1b8f1dfe0 100644 --- a/homeassistant/components/ihc/light.py +++ b/homeassistant/components/ihc/light.py @@ -2,7 +2,9 @@ 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 pulse from homeassistant.components.ihc.ihcdevice import IHCDevice from homeassistant.components.light import ( ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light) @@ -26,9 +28,13 @@ 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] + if CONF_OFF_ID in product_cfg: + ihc_off_id = product_cfg[CONF_OFF_ID] + if CONF_ON_ID in product_cfg: + ihc_on_id = product_cfg[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 +47,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 @@ -79,14 +88,20 @@ def turn_on(self, **kwargs) -> None: self.ihc_controller.set_runtime_value_int( self.ihc_id, int(brightness * 100 / 255)) else: - self.ihc_controller.set_runtime_value_bool(self.ihc_id, True) + if self._ihc_on_id > 0: + pulse(self.ihc_controller, self._ihc_on_id) + else: + self.ihc_controller.set_runtime_value_bool(self.ihc_id, True) def turn_off(self, **kwargs) -> None: """Turn the light off.""" if self._dimmable: self.ihc_controller.set_runtime_value_int(self.ihc_id, 0) else: - self.ihc_controller.set_runtime_value_bool(self.ihc_id, False) + if self._ihc_off_id > 0: + pulse(self.ihc_controller, self._ihc_off_id) + else: + self.ihc_controller.set_runtime_value_bool(self.ihc_id, False) def on_ihc_change(self, ihc_id, value): """Handle IHC notifications.""" diff --git a/homeassistant/components/ihc/services.yaml b/homeassistant/components/ihc/services.yaml index 0c63b32a618f7..0a78c45d7b23a 100644 --- a/homeassistant/components/ihc/services.yaml +++ b/homeassistant/components/ihc/services.yaml @@ -24,3 +24,8 @@ set_runtime_value_float: value: description: The float value to set. +pulse: + description: Pulses an input on the IHC controller. + fields: + ihc_id: + description: The integer IHC resource ID. \ No newline at end of file diff --git a/homeassistant/components/ihc/util.py b/homeassistant/components/ihc/util.py new file mode 100644 index 0000000000000..6fef9a6626141 --- /dev/null +++ b/homeassistant/components/ihc/util.py @@ -0,0 +1,9 @@ +"""Utility methods for IHC""" + +import time + + +def pulse(ihc_controller, ihc_id: int): + ihc_controller.set_runtime_value_bool(ihc_id, True) + time.sleep(0.1) + ihc_controller.set_runtime_value_bool(ihc_id, False) From 9ad1594cfcdaecd938d1c85904cb1737a0f27cca Mon Sep 17 00:00:00 2001 From: Michael Svinth Date: Sat, 19 Jan 2019 19:54:41 +0100 Subject: [PATCH 2/6] Fix --- homeassistant/components/ihc/light.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/homeassistant/components/ihc/light.py b/homeassistant/components/ihc/light.py index e6de1b8f1dfe0..c2bb4e77fa9e0 100644 --- a/homeassistant/components/ihc/light.py +++ b/homeassistant/components/ihc/light.py @@ -30,8 +30,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None): ihc_controller = hass.data[ihc_key][IHC_CONTROLLER] if CONF_OFF_ID in product_cfg: ihc_off_id = product_cfg[CONF_OFF_ID] + else: + ihc_off_id = 0 if CONF_ON_ID in product_cfg: ihc_on_id = product_cfg[CONF_ON_ID] + else: + ihc_on_id = 0 dimmable = product_cfg[CONF_DIMMABLE] light = IhcLight(ihc_controller, name, ihc_id, ihc_off_id, ihc_on_id, info, dimmable, product) From a46cb56ffda65cd71c3c986b014da564ad7bcee7 Mon Sep 17 00:00:00 2001 From: Michael Svinth Date: Sun, 20 Jan 2019 08:33:06 +0100 Subject: [PATCH 3/6] Lint fix --- homeassistant/components/ihc/util.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/ihc/util.py b/homeassistant/components/ihc/util.py index 6fef9a6626141..8af5152d5b9a6 100644 --- a/homeassistant/components/ihc/util.py +++ b/homeassistant/components/ihc/util.py @@ -1,9 +1,10 @@ -"""Utility methods for IHC""" +"""Useful functions for the IHC component.""" import time def pulse(ihc_controller, ihc_id: int): + """Send a short on/off pulse to an IHC controller resource.""" ihc_controller.set_runtime_value_bool(ihc_id, True) time.sleep(0.1) ihc_controller.set_runtime_value_bool(ihc_id, False) From 14a548da792a67e2e3e3c0fa494bf368eb3d401f Mon Sep 17 00:00:00 2001 From: Michael Svinth Date: Tue, 26 Feb 2019 18:35:08 +0100 Subject: [PATCH 4/6] Add on/off id support in switch --- homeassistant/components/ihc/__init__.py | 5 ++++- homeassistant/components/ihc/light.py | 14 ++++---------- homeassistant/components/ihc/switch.py | 24 +++++++++++++++++++----- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/ihc/__init__.py b/homeassistant/components/ihc/__init__.py index 15e4cc51ba6c8..be05ab1780ef8 100644 --- a/homeassistant/components/ihc/__init__.py +++ b/homeassistant/components/ihc/__init__.py @@ -52,7 +52,10 @@ def validate_name(config): }) -SWITCH_SCHEMA = DEVICE_SCHEMA.extend({}) +SWITCH_SCHEMA = DEVICE_SCHEMA.extend({ + vol.Optional(CONF_OFF_ID, default=0): cv.positive_int, + vol.Optional(CONF_ON_ID, default=0): cv.positive_int, +}) BINARY_SENSOR_SCHEMA = DEVICE_SCHEMA.extend({ vol.Optional(CONF_INVERTING, default=False): cv.boolean, diff --git a/homeassistant/components/ihc/light.py b/homeassistant/components/ihc/light.py index c2bb4e77fa9e0..b560da7e847f0 100644 --- a/homeassistant/components/ihc/light.py +++ b/homeassistant/components/ihc/light.py @@ -28,14 +28,8 @@ 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] - if CONF_OFF_ID in product_cfg: - ihc_off_id = product_cfg[CONF_OFF_ID] - else: - ihc_off_id = 0 - if CONF_ON_ID in product_cfg: - ihc_on_id = product_cfg[CONF_ON_ID] - else: - ihc_on_id = 0 + 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, ihc_off_id, ihc_on_id, info, dimmable, product) @@ -92,7 +86,7 @@ def turn_on(self, **kwargs) -> None: self.ihc_controller.set_runtime_value_int( self.ihc_id, int(brightness * 100 / 255)) else: - if self._ihc_on_id > 0: + if self._ihc_on_id: pulse(self.ihc_controller, self._ihc_on_id) else: self.ihc_controller.set_runtime_value_bool(self.ihc_id, True) @@ -102,7 +96,7 @@ def turn_off(self, **kwargs) -> None: if self._dimmable: self.ihc_controller.set_runtime_value_int(self.ihc_id, 0) else: - if self._ihc_off_id > 0: + if self._ihc_off_id: pulse(self.ihc_controller, self._ihc_off_id) else: self.ihc_controller.set_runtime_value_bool(self.ihc_id, False) diff --git a/homeassistant/components/ihc/switch.py b/homeassistant/components/ihc/switch.py index bbab9d3e68c0c..38383423ce3fe 100644 --- a/homeassistant/components/ihc/switch.py +++ b/homeassistant/components/ihc/switch.py @@ -1,5 +1,7 @@ """Support for IHC switches.""" from homeassistant.components.ihc import IHC_CONTROLLER, IHC_DATA, IHC_INFO +from homeassistant.components.ihc.const import CONF_OFF_ID, CONF_ON_ID +from homeassistant.components.ihc.util import pulse from homeassistant.components.ihc.ihcdevice import IHCDevice from homeassistant.components.switch import SwitchDevice @@ -13,14 +15,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None): devices = [] for name, device in discovery_info.items(): ihc_id = device['ihc_id'] + product_cfg = device['product_cfg'] product = device['product'] # Find controller that corresponds with device id ctrl_id = device['ctrl_id'] 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) - switch = IHCSwitch(ihc_controller, name, ihc_id, info, product) + switch = IHCSwitch(ihc_controller, name, ihc_id, ihc_off_id, ihc_on_id, + info, product) devices.append(switch) add_entities(devices) @@ -28,10 +34,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None): class IHCSwitch(IHCDevice, SwitchDevice): """Representation of an IHC switch.""" - def __init__(self, ihc_controller, name: str, ihc_id: int, - info: bool, product=None) -> None: + def __init__(self, ihc_controller, name: str, ihc_id: int, ihc_off_id: int, + ihc_on_id: int, info: bool, product=None) -> None: """Initialize the IHC switch.""" super().__init__(ihc_controller, name, ihc_id, product) + self._ihc_off_id = ihc_off_id + self._ihc_on_id = ihc_on_id self._state = False @property @@ -41,11 +49,17 @@ def is_on(self): def turn_on(self, **kwargs): """Turn the switch on.""" - self.ihc_controller.set_runtime_value_bool(self.ihc_id, True) + if self._ihc_on_id: + pulse(self.ihc_controller, self._ihc_on_id) + else: + self.ihc_controller.set_runtime_value_bool(self.ihc_id, True) def turn_off(self, **kwargs): """Turn the device off.""" - self.ihc_controller.set_runtime_value_bool(self.ihc_id, False) + if self._ihc_off_id: + pulse(self.ihc_controller, self._ihc_off_id) + else: + self.ihc_controller.set_runtime_value_bool(self.ihc_id, False) def on_ihc_change(self, ihc_id, value): """Handle IHC resource change.""" From 3814be03b420b3b2a9898db1a28040788dab04c7 Mon Sep 17 00:00:00 2001 From: Michael Svinth Date: Tue, 26 Feb 2019 20:22:53 +0100 Subject: [PATCH 5/6] Make pulse async --- homeassistant/components/ihc/__init__.py | 12 ++++++------ homeassistant/components/ihc/light.py | 8 ++++---- homeassistant/components/ihc/switch.py | 8 ++++---- homeassistant/components/ihc/util.py | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/ihc/__init__.py b/homeassistant/components/ihc/__init__.py index be05ab1780ef8..252fda6ef7796 100644 --- a/homeassistant/components/ihc/__init__.py +++ b/homeassistant/components/ihc/__init__.py @@ -54,7 +54,7 @@ def validate_name(config): SWITCH_SCHEMA = DEVICE_SCHEMA.extend({ vol.Optional(CONF_OFF_ID, default=0): cv.positive_int, - vol.Optional(CONF_ON_ID, default=0): cv.positive_int, + vol.Optional(CONF_ON_ID, default=0): cv.positive_int, }) BINARY_SENSOR_SCHEMA = DEVICE_SCHEMA.extend({ @@ -300,10 +300,10 @@ def set_runtime_value_float(call): value = call.data[ATTR_VALUE] ihc_controller.set_runtime_value_float(ihc_id, value) - def pulse_runtime_input(call): + async def pulse_runtime_input(call): """Pulse a IHC controller input function.""" ihc_id = call.data[ATTR_IHC_ID] - pulse(ihc_controller, ihc_id) + await pulse(ihc_controller, ihc_id) hass.services.register(DOMAIN, SERVICE_SET_RUNTIME_VALUE_BOOL, set_runtime_value_bool, @@ -314,6 +314,6 @@ def pulse_runtime_input(call): hass.services.register(DOMAIN, SERVICE_SET_RUNTIME_VALUE_FLOAT, set_runtime_value_float, schema=SET_RUNTIME_VALUE_FLOAT_SCHEMA) - hass.services.register(DOMAIN, SERVICE_PULSE, - pulse_runtime_input, - schema=PULSE_SCHEMA) + hass.services.async_register(DOMAIN, SERVICE_PULSE, + pulse_runtime_input, + schema=PULSE_SCHEMA) diff --git a/homeassistant/components/ihc/light.py b/homeassistant/components/ihc/light.py index b560da7e847f0..9f2ca1d5f7b97 100644 --- a/homeassistant/components/ihc/light.py +++ b/homeassistant/components/ihc/light.py @@ -73,7 +73,7 @@ def supported_features(self): 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] @@ -87,17 +87,17 @@ def turn_on(self, **kwargs) -> None: self.ihc_id, int(brightness * 100 / 255)) else: if self._ihc_on_id: - pulse(self.ihc_controller, self._ihc_on_id) + await pulse(self.ihc_controller, self._ihc_on_id) else: self.ihc_controller.set_runtime_value_bool(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) else: if self._ihc_off_id: - pulse(self.ihc_controller, self._ihc_off_id) + await pulse(self.ihc_controller, self._ihc_off_id) else: self.ihc_controller.set_runtime_value_bool(self.ihc_id, False) diff --git a/homeassistant/components/ihc/switch.py b/homeassistant/components/ihc/switch.py index 38383423ce3fe..addc0326cf20b 100644 --- a/homeassistant/components/ihc/switch.py +++ b/homeassistant/components/ihc/switch.py @@ -47,17 +47,17 @@ def is_on(self): """Return true if switch is on.""" return self._state - def turn_on(self, **kwargs): + async def async_turn_on(self, **kwargs): """Turn the switch on.""" if self._ihc_on_id: - pulse(self.ihc_controller, self._ihc_on_id) + await pulse(self.ihc_controller, self._ihc_on_id) else: self.ihc_controller.set_runtime_value_bool(self.ihc_id, True) - def turn_off(self, **kwargs): + async def async_turn_off(self, **kwargs): """Turn the device off.""" if self._ihc_off_id: - pulse(self.ihc_controller, self._ihc_off_id) + await pulse(self.ihc_controller, self._ihc_off_id) else: self.ihc_controller.set_runtime_value_bool(self.ihc_id, False) diff --git a/homeassistant/components/ihc/util.py b/homeassistant/components/ihc/util.py index 8af5152d5b9a6..94d06b5383c93 100644 --- a/homeassistant/components/ihc/util.py +++ b/homeassistant/components/ihc/util.py @@ -1,10 +1,10 @@ """Useful functions for the IHC component.""" -import time +import asyncio -def pulse(ihc_controller, ihc_id: int): +async def pulse(ihc_controller, ihc_id: int): """Send a short on/off pulse to an IHC controller resource.""" ihc_controller.set_runtime_value_bool(ihc_id, True) - time.sleep(0.1) + await asyncio.sleep(0.1) ihc_controller.set_runtime_value_bool(ihc_id, False) From c6d599ea7cc37ed6aa3e3a6ef74891482fa5057f Mon Sep 17 00:00:00 2001 From: Michael Svinth Date: Wed, 27 Feb 2019 18:32:53 +0100 Subject: [PATCH 6/6] Code review fixes --- homeassistant/components/ihc/__init__.py | 12 ++++++------ homeassistant/components/ihc/light.py | 22 ++++++++++++++-------- homeassistant/components/ihc/switch.py | 12 +++++++----- homeassistant/components/ihc/util.py | 18 +++++++++++++++--- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/ihc/__init__.py b/homeassistant/components/ihc/__init__.py index 252fda6ef7796..daaf471e3182d 100644 --- a/homeassistant/components/ihc/__init__.py +++ b/homeassistant/components/ihc/__init__.py @@ -11,7 +11,7 @@ CONF_ON_ID, CONF_POSITION, CONF_SENSOR, CONF_SWITCH, CONF_XPATH, SERVICE_SET_RUNTIME_VALUE_BOOL, SERVICE_SET_RUNTIME_VALUE_FLOAT, SERVICE_SET_RUNTIME_VALUE_INT, SERVICE_PULSE) -from homeassistant.components.ihc.util import pulse +from homeassistant.components.ihc.util import async_pulse from homeassistant.config import load_yaml_config_file from homeassistant.const import ( CONF_ID, CONF_NAME, CONF_PASSWORD, CONF_TYPE, CONF_UNIT_OF_MEASUREMENT, @@ -300,10 +300,10 @@ def set_runtime_value_float(call): value = call.data[ATTR_VALUE] ihc_controller.set_runtime_value_float(ihc_id, value) - async def pulse_runtime_input(call): + async def async_pulse_runtime_input(call): """Pulse a IHC controller input function.""" ihc_id = call.data[ATTR_IHC_ID] - await pulse(ihc_controller, ihc_id) + await async_pulse(hass, ihc_controller, ihc_id) hass.services.register(DOMAIN, SERVICE_SET_RUNTIME_VALUE_BOOL, set_runtime_value_bool, @@ -314,6 +314,6 @@ async def pulse_runtime_input(call): hass.services.register(DOMAIN, SERVICE_SET_RUNTIME_VALUE_FLOAT, set_runtime_value_float, schema=SET_RUNTIME_VALUE_FLOAT_SCHEMA) - hass.services.async_register(DOMAIN, SERVICE_PULSE, - pulse_runtime_input, - schema=PULSE_SCHEMA) + hass.services.register(DOMAIN, SERVICE_PULSE, + async_pulse_runtime_input, + schema=PULSE_SCHEMA) diff --git a/homeassistant/components/ihc/light.py b/homeassistant/components/ihc/light.py index 9f2ca1d5f7b97..646be7597d057 100644 --- a/homeassistant/components/ihc/light.py +++ b/homeassistant/components/ihc/light.py @@ -4,7 +4,8 @@ from homeassistant.components.ihc import IHC_CONTROLLER, IHC_DATA, IHC_INFO from homeassistant.components.ihc.const import ( CONF_DIMMABLE, CONF_OFF_ID, CONF_ON_ID) -from homeassistant.components.ihc.util import pulse +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) @@ -83,23 +84,28 @@ async def async_turn_on(self, **kwargs): 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: if self._ihc_on_id: - await pulse(self.ihc_controller, self._ihc_on_id) + await async_pulse(self.hass, self.ihc_controller, + self._ihc_on_id) else: - self.ihc_controller.set_runtime_value_bool(self.ihc_id, True) + await async_set_bool(self.hass, self.ihc_controller, + self.ihc_id, True) 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: if self._ihc_off_id: - await pulse(self.ihc_controller, self._ihc_off_id) + await async_pulse(self.hass, self.ihc_controller, + self._ihc_off_id) else: - self.ihc_controller.set_runtime_value_bool(self.ihc_id, False) + await async_set_bool(self.hass, self.ihc_controller, + self.ihc_id, False) def on_ihc_change(self, ihc_id, value): """Handle IHC notifications.""" diff --git a/homeassistant/components/ihc/switch.py b/homeassistant/components/ihc/switch.py index addc0326cf20b..d25b343446dd0 100644 --- a/homeassistant/components/ihc/switch.py +++ b/homeassistant/components/ihc/switch.py @@ -1,7 +1,7 @@ """Support for IHC switches.""" from homeassistant.components.ihc import IHC_CONTROLLER, IHC_DATA, IHC_INFO from homeassistant.components.ihc.const import CONF_OFF_ID, CONF_ON_ID -from homeassistant.components.ihc.util import pulse +from homeassistant.components.ihc.util import async_pulse, async_set_bool from homeassistant.components.ihc.ihcdevice import IHCDevice from homeassistant.components.switch import SwitchDevice @@ -50,16 +50,18 @@ def is_on(self): async def async_turn_on(self, **kwargs): """Turn the switch on.""" if self._ihc_on_id: - await pulse(self.ihc_controller, self._ihc_on_id) + await async_pulse(self.hass, self.ihc_controller, self._ihc_on_id) else: - self.ihc_controller.set_runtime_value_bool(self.ihc_id, True) + await async_set_bool(self.hass, self.ihc_controller, + self.ihc_id, True) async def async_turn_off(self, **kwargs): """Turn the device off.""" if self._ihc_off_id: - await pulse(self.ihc_controller, self._ihc_off_id) + await async_pulse(self.hass, self.ihc_controller, self._ihc_off_id) else: - self.ihc_controller.set_runtime_value_bool(self.ihc_id, False) + await async_set_bool(self.hass, self.ihc_controller, + self.ihc_id, False) def on_ihc_change(self, ihc_id, value): """Handle IHC resource change.""" diff --git a/homeassistant/components/ihc/util.py b/homeassistant/components/ihc/util.py index 94d06b5383c93..a6780262f5eac 100644 --- a/homeassistant/components/ihc/util.py +++ b/homeassistant/components/ihc/util.py @@ -3,8 +3,20 @@ import asyncio -async def pulse(ihc_controller, ihc_id: int): +async def async_pulse(hass, ihc_controller, ihc_id: int): """Send a short on/off pulse to an IHC controller resource.""" - ihc_controller.set_runtime_value_bool(ihc_id, True) + await async_set_bool(hass, ihc_controller, ihc_id, True) await asyncio.sleep(0.1) - ihc_controller.set_runtime_value_bool(ihc_id, False) + await async_set_bool(hass, ihc_controller, ihc_id, False) + + +def async_set_bool(hass, ihc_controller, ihc_id: int, value: bool): + """Set a bool value on an IHC controller resource.""" + return hass.async_add_executor_job(ihc_controller.set_runtime_value_bool, + ihc_id, value) + + +def async_set_int(hass, ihc_controller, ihc_id: int, value: int): + """Set a int value on an IHC controller resource.""" + return hass.async_add_executor_job(ihc_controller.set_runtime_value_int, + ihc_id, value)