From 6de24baf95684b97c92a7955e76d749c6ec2e6f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20=C3=98stergaard=20Nielsen?= Date: Sun, 23 Jan 2022 05:33:10 +0100 Subject: [PATCH] Make service functions async (#64705) --- homeassistant/components/ihc/__init__.py | 105 +--------------- homeassistant/components/ihc/const.py | 10 +- .../components/ihc/service_functions.py | 112 ++++++++++++++++++ homeassistant/components/ihc/util.py | 8 ++ 4 files changed, 131 insertions(+), 104 deletions(-) create mode 100644 homeassistant/components/ihc/service_functions.py diff --git a/homeassistant/components/ihc/__init__.py b/homeassistant/components/ihc/__init__.py index 17021b77b48..7c556bf3713 100644 --- a/homeassistant/components/ihc/__init__.py +++ b/homeassistant/components/ihc/__init__.py @@ -19,15 +19,12 @@ from homeassistant.const import ( TEMP_CELSIUS, Platform, ) -from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.core import HomeAssistant from homeassistant.helpers import discovery import homeassistant.helpers.config_validation as cv from homeassistant.helpers.typing import ConfigType from .const import ( - ATTR_CONTROLLER_ID, - ATTR_IHC_ID, - ATTR_VALUE, CONF_AUTOSETUP, CONF_BINARY_SENSOR, CONF_DIMMABLE, @@ -42,20 +39,15 @@ from .const import ( CONF_SENSOR, CONF_SWITCH, CONF_XPATH, - SERVICE_PULSE, - SERVICE_SET_RUNTIME_VALUE_BOOL, - SERVICE_SET_RUNTIME_VALUE_FLOAT, - SERVICE_SET_RUNTIME_VALUE_INT, + DOMAIN, + IHC_CONTROLLER, ) -from .util import async_pulse +from .service_functions import setup_service_functions _LOGGER = logging.getLogger(__name__) AUTO_SETUP_YAML = "ihc_auto_setup.yaml" -DOMAIN = "ihc" - -IHC_CONTROLLER = "controller" IHC_INFO = "info" PLATFORMS = ( Platform.BINARY_SENSOR, @@ -193,37 +185,6 @@ AUTO_SETUP_SCHEMA = vol.Schema( } ) -SET_RUNTIME_VALUE_BOOL_SCHEMA = vol.Schema( - { - vol.Required(ATTR_IHC_ID): cv.positive_int, - vol.Required(ATTR_VALUE): cv.boolean, - vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int, - } -) - -SET_RUNTIME_VALUE_INT_SCHEMA = vol.Schema( - { - vol.Required(ATTR_IHC_ID): cv.positive_int, - vol.Required(ATTR_VALUE): vol.Coerce(int), - vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int, - } -) - -SET_RUNTIME_VALUE_FLOAT_SCHEMA = vol.Schema( - { - vol.Required(ATTR_IHC_ID): cv.positive_int, - vol.Required(ATTR_VALUE): vol.Coerce(float), - vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int, - } -) - -PULSE_SCHEMA = vol.Schema( - { - vol.Required(ATTR_IHC_ID): cv.positive_int, - vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int, - } -) - def setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the IHC integration.""" @@ -345,61 +306,3 @@ def get_discovery_info(platform_setup, groups, controller_id): } discovery_data[name] = device return discovery_data - - -def setup_service_functions(hass: HomeAssistant): - """Set up the IHC service functions.""" - - def _get_controller(call): - controller_id = call.data[ATTR_CONTROLLER_ID] - ihc_key = f"ihc{controller_id}" - return hass.data[ihc_key][IHC_CONTROLLER] - - def set_runtime_value_bool(call: ServiceCall) -> None: - """Set a IHC runtime bool value service function.""" - ihc_id = call.data[ATTR_IHC_ID] - value = call.data[ATTR_VALUE] - ihc_controller = _get_controller(call) - ihc_controller.set_runtime_value_bool(ihc_id, value) - - def set_runtime_value_int(call: ServiceCall) -> None: - """Set a IHC runtime integer value service function.""" - ihc_id = call.data[ATTR_IHC_ID] - value = call.data[ATTR_VALUE] - ihc_controller = _get_controller(call) - ihc_controller.set_runtime_value_int(ihc_id, value) - - def set_runtime_value_float(call: ServiceCall) -> None: - """Set a IHC runtime float value service function.""" - ihc_id = call.data[ATTR_IHC_ID] - value = call.data[ATTR_VALUE] - ihc_controller = _get_controller(call) - ihc_controller.set_runtime_value_float(ihc_id, value) - - async def async_pulse_runtime_input(call: ServiceCall) -> None: - """Pulse a IHC controller input function.""" - ihc_id = call.data[ATTR_IHC_ID] - ihc_controller = _get_controller(call) - await async_pulse(hass, ihc_controller, ihc_id) - - hass.services.register( - DOMAIN, - SERVICE_SET_RUNTIME_VALUE_BOOL, - set_runtime_value_bool, - schema=SET_RUNTIME_VALUE_BOOL_SCHEMA, - ) - hass.services.register( - DOMAIN, - SERVICE_SET_RUNTIME_VALUE_INT, - set_runtime_value_int, - schema=SET_RUNTIME_VALUE_INT_SCHEMA, - ) - 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, async_pulse_runtime_input, schema=PULSE_SCHEMA - ) diff --git a/homeassistant/components/ihc/const.py b/homeassistant/components/ihc/const.py index c751d7990e4..9deaa0dbbc4 100644 --- a/homeassistant/components/ihc/const.py +++ b/homeassistant/components/ihc/const.py @@ -1,5 +1,9 @@ """IHC component constants.""" +ATTR_IHC_ID = "ihc_id" +ATTR_VALUE = "value" +ATTR_CONTROLLER_ID = "controller_id" + CONF_AUTOSETUP = "auto_setup" CONF_BINARY_SENSOR = "binary_sensor" CONF_DIMMABLE = "dimmable" @@ -15,9 +19,9 @@ CONF_SENSOR = "sensor" CONF_SWITCH = "switch" CONF_XPATH = "xpath" -ATTR_IHC_ID = "ihc_id" -ATTR_VALUE = "value" -ATTR_CONTROLLER_ID = "controller_id" +DOMAIN = "ihc" + +IHC_CONTROLLER = "controller" SERVICE_SET_RUNTIME_VALUE_BOOL = "set_runtime_value_bool" SERVICE_SET_RUNTIME_VALUE_FLOAT = "set_runtime_value_float" diff --git a/homeassistant/components/ihc/service_functions.py b/homeassistant/components/ihc/service_functions.py new file mode 100644 index 00000000000..6136c8ccd46 --- /dev/null +++ b/homeassistant/components/ihc/service_functions.py @@ -0,0 +1,112 @@ +"""Support for IHC devices.""" +import logging + +import voluptuous as vol + +from homeassistant.core import HomeAssistant +import homeassistant.helpers.config_validation as cv + +from .const import ( + ATTR_CONTROLLER_ID, + ATTR_IHC_ID, + ATTR_VALUE, + DOMAIN, + IHC_CONTROLLER, + SERVICE_PULSE, + SERVICE_SET_RUNTIME_VALUE_BOOL, + SERVICE_SET_RUNTIME_VALUE_FLOAT, + SERVICE_SET_RUNTIME_VALUE_INT, +) +from .util import async_pulse, async_set_bool, async_set_float, async_set_int + +_LOGGER = logging.getLogger(__name__) + + +SET_RUNTIME_VALUE_BOOL_SCHEMA = vol.Schema( + { + vol.Required(ATTR_IHC_ID): cv.positive_int, + vol.Required(ATTR_VALUE): cv.boolean, + vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int, + } +) + +SET_RUNTIME_VALUE_INT_SCHEMA = vol.Schema( + { + vol.Required(ATTR_IHC_ID): cv.positive_int, + vol.Required(ATTR_VALUE): vol.Coerce(int), + vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int, + } +) + +SET_RUNTIME_VALUE_FLOAT_SCHEMA = vol.Schema( + { + vol.Required(ATTR_IHC_ID): cv.positive_int, + vol.Required(ATTR_VALUE): vol.Coerce(float), + vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int, + } +) + +PULSE_SCHEMA = vol.Schema( + { + vol.Required(ATTR_IHC_ID): cv.positive_int, + vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int, + } +) + + +def setup_service_functions(hass: HomeAssistant): + """Set up the IHC service functions.""" + + def _get_controller(call): + controller_id = call.data[ATTR_CONTROLLER_ID] + ihc_key = f"ihc{controller_id}" + return hass.data[ihc_key][IHC_CONTROLLER] + + async def async_set_runtime_value_bool(call): + """Set a IHC runtime bool value service function.""" + ihc_id = call.data[ATTR_IHC_ID] + value = call.data[ATTR_VALUE] + ihc_controller = _get_controller(call) + await async_set_bool(hass, ihc_controller, ihc_id, value) + + async def async_set_runtime_value_int(call): + """Set a IHC runtime integer value service function.""" + ihc_id = call.data[ATTR_IHC_ID] + value = call.data[ATTR_VALUE] + ihc_controller = _get_controller(call) + await async_set_int(hass, ihc_controller, ihc_id, value) + + async def async_set_runtime_value_float(call): + """Set a IHC runtime float value service function.""" + ihc_id = call.data[ATTR_IHC_ID] + value = call.data[ATTR_VALUE] + ihc_controller = _get_controller(call) + await async_set_float(hass, ihc_controller, ihc_id, value) + + async def async_pulse_runtime_input(call): + """Pulse a IHC controller input function.""" + ihc_id = call.data[ATTR_IHC_ID] + ihc_controller = _get_controller(call) + await async_pulse(hass, ihc_controller, ihc_id) + + hass.services.async_register( + DOMAIN, + SERVICE_SET_RUNTIME_VALUE_BOOL, + async_set_runtime_value_bool, + schema=SET_RUNTIME_VALUE_BOOL_SCHEMA, + ) + hass.services.async_register( + DOMAIN, + SERVICE_SET_RUNTIME_VALUE_INT, + async_set_runtime_value_int, + schema=SET_RUNTIME_VALUE_INT_SCHEMA, + ) + hass.services.async_register( + DOMAIN, + SERVICE_SET_RUNTIME_VALUE_FLOAT, + async_set_runtime_value_float, + schema=SET_RUNTIME_VALUE_FLOAT_SCHEMA, + ) + hass.services.async_register( + DOMAIN, SERVICE_PULSE, async_pulse_runtime_input, schema=PULSE_SCHEMA + ) diff --git a/homeassistant/components/ihc/util.py b/homeassistant/components/ihc/util.py index 1b6b1dbd3e0..8765243e76d 100644 --- a/homeassistant/components/ihc/util.py +++ b/homeassistant/components/ihc/util.py @@ -26,3 +26,11 @@ def async_set_int(hass, ihc_controller, ihc_id: int, value: int): return hass.async_add_executor_job( ihc_controller.set_runtime_value_int, ihc_id, value ) + + +@callback +def async_set_float(hass, ihc_controller, ihc_id: int, value: float): + """Set a float value on an IHC controller resource.""" + return hass.async_add_executor_job( + ihc_controller.set_runtime_value_float, ihc_id, value + )