Skip to content

Commit

Permalink
Update service_functions.py
Browse files Browse the repository at this point in the history
  • Loading branch information
duddi authored Mar 10, 2023
1 parent 552fbce commit c9691d2
Showing 1 changed file with 51 additions and 60 deletions.
111 changes: 51 additions & 60 deletions custom_components/ihc/service_functions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Support for IHC devices."""
import voluptuous as vol

from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
import voluptuous as vol

from .const import (
ATTR_CONTROLLER_ID,
Expand All @@ -15,7 +15,12 @@
SERVICE_SET_RUNTIME_VALUE_FLOAT,
SERVICE_SET_RUNTIME_VALUE_INT,
)
from .util import async_pulse, async_set_bool, async_set_float, async_set_int
from .util import (
async_pulse,
async_set_bool,
async_set_float,
async_set_int,
)

SET_RUNTIME_VALUE_BOOL_SCHEMA = vol.Schema(
{
Expand Down Expand Up @@ -49,61 +54,47 @@
)


def setup_service_functions(hass: HomeAssistant) -> None:
"""Set up the IHC service functions."""

def _get_controller(call):
controller_id = call.data[ATTR_CONTROLLER_ID]
# if no controller id is specified use the first one
if controller_id == "":
controller_id = next(iter(hass.data[DOMAIN]))
return hass.data[DOMAIN][controller_id][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
)
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]
controller_id = call.data.get(ATTR_CONTROLLER_ID)
if not controller_id:
controller_id = next(iter(call.hass.data[DOMAIN]))
ihc_controller = call.hass.data[DOMAIN][controller_id][IHC_CONTROLLER]
await async_set_bool(call.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]
controller_id = call.data.get(ATTR_CONTROLLER_ID)
if not controller_id:
controller_id = next(iter(call.hass.data[DOMAIN]))
ihc_controller = call.hass.data[DOMAIN][controller_id][IHC_CONTROLLER]
await async_set_int(call.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]
controller_id = call.data.get(ATTR_CONTROLLER_ID)
if not controller_id:
controller_id = next(iter(call.hass.data[DOMAIN]))
ihc_controller = call.hass.data[DOMAIN][controller_id][IHC_CONTROLLER]
await async_set_float(call.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]
controller_id = call.data.get(ATTR_CONTROLLER_ID)
if not controller_id:
controller_id = next(iter(call.hass.data[DOMAIN]))
ihc_controller = call.hass.data[DOMAIN][controller_id][IHC_CONTROLLER]
await async_pulse(call.hass, ihc_controller, ihc_id)


def setup_service_functions(hass: HomeAssistant) -> None

0 comments on commit c9691d2

Please sign in to comment.