Make service functions async (#64705)
parent
cd34beb832
commit
6de24baf95
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
)
|
|
@ -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
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue