1 """Service calls related dependencies for LCN component."""
3 from enum
import StrEnum, auto
6 import voluptuous
as vol
13 CONF_UNIT_OF_MEASUREMENT,
45 from .helpers
import (
47 get_device_connection,
54 """Parent class for all LCN service calls."""
56 schema = vol.Schema({vol.Required(CONF_ADDRESS): is_address})
58 def __init__(self, hass: HomeAssistant) ->
None:
59 """Initialize service call."""
63 """Get address connection object."""
64 address, host_name = service.data[CONF_ADDRESS]
66 for config_entry
in self.
hasshass.config_entries.async_entries(DOMAIN):
67 if config_entry.data[CONF_HOST] == host_name:
69 self.
hasshass, address, config_entry
71 if device_connection
is None:
72 raise ValueError(
"Wrong address.")
73 return device_connection
74 raise ValueError(
"Invalid host name.")
77 """Execute service call."""
78 raise NotImplementedError
82 """Set absolute brightness of output port in percent."""
84 schema = LcnServiceCall.schema.extend(
86 vol.Required(CONF_OUTPUT): vol.All(vol.Upper, vol.In(OUTPUT_PORTS)),
87 vol.Required(CONF_BRIGHTNESS): vol.All(
88 vol.Coerce(int), vol.Range(min=0, max=100)
90 vol.Optional(CONF_TRANSITION, default=0): vol.All(
91 vol.Coerce(float), vol.Range(min=0.0, max=486.0)
97 """Execute service call."""
98 output = pypck.lcn_defs.OutputPort[service.data[CONF_OUTPUT]]
99 brightness = service.data[CONF_BRIGHTNESS]
100 transition = pypck.lcn_defs.time_to_ramp_value(
101 service.data[CONF_TRANSITION] * 1000
105 await device_connection.dim_output(output.value, brightness, transition)
109 """Set relative brightness of output port in percent."""
111 schema = LcnServiceCall.schema.extend(
113 vol.Required(CONF_OUTPUT): vol.All(vol.Upper, vol.In(OUTPUT_PORTS)),
114 vol.Required(CONF_BRIGHTNESS): vol.All(
115 vol.Coerce(int), vol.Range(min=-100, max=100)
121 """Execute service call."""
122 output = pypck.lcn_defs.OutputPort[service.data[CONF_OUTPUT]]
123 brightness = service.data[CONF_BRIGHTNESS]
126 await device_connection.rel_output(output.value, brightness)
130 """Toggle output port."""
132 schema = LcnServiceCall.schema.extend(
134 vol.Required(CONF_OUTPUT): vol.All(vol.Upper, vol.In(OUTPUT_PORTS)),
135 vol.Optional(CONF_TRANSITION, default=0): vol.All(
136 vol.Coerce(float), vol.Range(min=0.0, max=486.0)
142 """Execute service call."""
143 output = pypck.lcn_defs.OutputPort[service.data[CONF_OUTPUT]]
144 transition = pypck.lcn_defs.time_to_ramp_value(
145 service.data[CONF_TRANSITION] * 1000
149 await device_connection.toggle_output(output.value, transition)
153 """Set the relays status."""
155 schema = LcnServiceCall.schema.extend({vol.Required(CONF_STATE): is_states_string})
158 """Execute service call."""
160 pypck.lcn_defs.RelayStateModifier[state]
161 for state
in service.data[CONF_STATE]
165 await device_connection.control_relays(states)
169 """Set the led state."""
171 schema = LcnServiceCall.schema.extend(
173 vol.Required(CONF_LED): vol.All(vol.Upper, vol.In(LED_PORTS)),
174 vol.Required(CONF_STATE): vol.All(vol.Upper, vol.In(LED_STATUS)),
179 """Execute service call."""
180 led = pypck.lcn_defs.LedPort[service.data[CONF_LED]]
181 led_state = pypck.lcn_defs.LedStatus[service.data[CONF_STATE]]
184 await device_connection.control_led(led, led_state)
188 """Set absolute value of a variable or setpoint.
190 Variable has to be set as counter!
191 Regulator setpoints can also be set using R1VARSETPOINT, R2VARSETPOINT.
194 schema = LcnServiceCall.schema.extend(
196 vol.Required(CONF_VARIABLE): vol.All(
197 vol.Upper, vol.In(VARIABLES + SETPOINTS)
199 vol.Optional(CONF_VALUE, default=0): vol.Coerce(float),
200 vol.Optional(CONF_UNIT_OF_MEASUREMENT, default=
"native"): vol.All(
201 vol.Upper, vol.In(VAR_UNITS)
207 """Execute service call."""
208 var = pypck.lcn_defs.Var[service.data[CONF_VARIABLE]]
209 value = service.data[CONF_VALUE]
210 unit = pypck.lcn_defs.VarUnit.parse(service.data[CONF_UNIT_OF_MEASUREMENT])
213 await device_connection.var_abs(var, value, unit)
217 """Reset value of variable or setpoint."""
219 schema = LcnServiceCall.schema.extend(
220 {vol.Required(CONF_VARIABLE): vol.All(vol.Upper, vol.In(VARIABLES + SETPOINTS))}
224 """Execute service call."""
225 var = pypck.lcn_defs.Var[service.data[CONF_VARIABLE]]
228 await device_connection.var_reset(var)
232 """Shift value of a variable, setpoint or threshold."""
234 schema = LcnServiceCall.schema.extend(
236 vol.Required(CONF_VARIABLE): vol.All(
237 vol.Upper, vol.In(VARIABLES + SETPOINTS + THRESHOLDS)
239 vol.Optional(CONF_VALUE, default=0): vol.Coerce(float),
240 vol.Optional(CONF_UNIT_OF_MEASUREMENT, default=
"native"): vol.All(
241 vol.Upper, vol.In(VAR_UNITS)
243 vol.Optional(CONF_RELVARREF, default=
"current"): vol.All(
244 vol.Upper, vol.In(RELVARREF)
250 """Execute service call."""
251 var = pypck.lcn_defs.Var[service.data[CONF_VARIABLE]]
252 value = service.data[CONF_VALUE]
253 unit = pypck.lcn_defs.VarUnit.parse(service.data[CONF_UNIT_OF_MEASUREMENT])
254 value_ref = pypck.lcn_defs.RelVarRef[service.data[CONF_RELVARREF]]
257 await device_connection.var_rel(var, value, unit, value_ref)
261 """Locks a regulator setpoint."""
263 schema = LcnServiceCall.schema.extend(
265 vol.Required(CONF_SETPOINT): vol.All(vol.Upper, vol.In(SETPOINTS)),
266 vol.Optional(CONF_STATE, default=
False): bool,
271 """Execute service call."""
272 setpoint = pypck.lcn_defs.Var[service.data[CONF_SETPOINT]]
273 state = service.data[CONF_STATE]
275 reg_id = pypck.lcn_defs.Var.to_set_point_id(setpoint)
277 await device_connection.lock_regulator(reg_id, state)
281 """Sends keys (which executes bound commands)."""
283 schema = LcnServiceCall.schema.extend(
285 vol.Required(CONF_KEYS): vol.All(
286 vol.Upper, cv.matches_regex(
r"^([A-D][1-8])+$")
288 vol.Optional(CONF_STATE, default=
"hit"): vol.All(
289 vol.Upper, vol.In(SENDKEYCOMMANDS)
291 vol.Optional(CONF_TIME, default=0): cv.positive_int,
292 vol.Optional(CONF_TIME_UNIT, default=
"S"): vol.All(
293 vol.Upper, vol.In(TIME_UNITS)
299 """Execute service call."""
302 keys = [[
False] * 8
for i
in range(4)]
305 service.data[CONF_KEYS][::2], service.data[CONF_KEYS][1::2], strict=
False
308 for table, key
in key_strings:
309 table_id = ord(table) - 65
310 key_id =
int(key) - 1
311 keys[table_id][key_id] =
True
313 if (delay_time := service.data[CONF_TIME]) != 0:
314 hit = pypck.lcn_defs.SendKeyCommand.HIT
315 if pypck.lcn_defs.SendKeyCommand[service.data[CONF_STATE]] != hit:
317 "Only hit command is allowed when sending deferred keys."
319 delay_unit = pypck.lcn_defs.TimeUnit.parse(service.data[CONF_TIME_UNIT])
320 await device_connection.send_keys_hit_deferred(keys, delay_time, delay_unit)
322 state = pypck.lcn_defs.SendKeyCommand[service.data[CONF_STATE]]
323 await device_connection.send_keys(keys, state)
329 schema = LcnServiceCall.schema.extend(
331 vol.Optional(CONF_TABLE, default=
"a"): vol.All(
332 vol.Upper, cv.matches_regex(
r"^[A-D]$")
334 vol.Required(CONF_STATE): is_states_string,
335 vol.Optional(CONF_TIME, default=0): cv.positive_int,
336 vol.Optional(CONF_TIME_UNIT, default=
"S"): vol.All(
337 vol.Upper, vol.In(TIME_UNITS)
343 """Execute service call."""
347 pypck.lcn_defs.KeyLockStateModifier[state]
348 for state
in service.data[CONF_STATE]
350 table_id = ord(service.data[CONF_TABLE]) - 65
352 if (delay_time := service.data[CONF_TIME]) != 0:
355 "Only table A is allowed when locking keys for a specific time."
357 delay_unit = pypck.lcn_defs.TimeUnit.parse(service.data[CONF_TIME_UNIT])
358 await device_connection.lock_keys_tab_a_temporary(
359 delay_time, delay_unit, states
362 await device_connection.lock_keys(table_id, states)
364 handler = device_connection.status_requests_handler
365 await handler.request_status_locked_keys_timeout()
369 """Send dynamic text to LCN-GTxD displays."""
371 schema = LcnServiceCall.schema.extend(
373 vol.Required(CONF_ROW): vol.All(int, vol.Range(min=1, max=4)),
374 vol.Required(CONF_TEXT): vol.All(str, vol.Length(max=60)),
379 """Execute service call."""
380 row_id = service.data[CONF_ROW] - 1
381 text = service.data[CONF_TEXT]
384 await device_connection.dyn_text(row_id, text)
388 """Send arbitrary PCK command."""
390 schema = LcnServiceCall.schema.extend({vol.Required(CONF_PCK): str})
393 """Execute service call."""
394 pck = service.data[CONF_PCK]
396 await device_connection.pck(pck)
400 """LCN service names."""
404 OUTPUT_TOGGLE = auto()
409 LOCK_REGULATOR = auto()
418 (LcnService.OUTPUT_ABS, OutputAbs),
419 (LcnService.OUTPUT_REL, OutputRel),
420 (LcnService.OUTPUT_TOGGLE, OutputToggle),
421 (LcnService.RELAYS, Relays),
422 (LcnService.VAR_ABS, VarAbs),
423 (LcnService.VAR_RESET, VarReset),
424 (LcnService.VAR_REL, VarRel),
425 (LcnService.LOCK_REGULATOR, LockRegulator),
426 (LcnService.LED, Led),
427 (LcnService.SEND_KEYS, SendKeys),
428 (LcnService.LOCK_KEYS, LockKeys),
429 (LcnService.DYN_TEXT, DynText),
430 (LcnService.PCK, Pck),
435 """Register services for LCN."""
436 for service_name, service
in SERVICES:
437 hass.services.async_register(
438 DOMAIN, service_name,
service(hass).async_call_service, service.schema
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
DeviceConnectionType get_device_connection(self, ServiceCall service)
None __init__(self, HomeAssistant hass)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None async_call_service(self, ServiceCall service)
None register_services(HomeAssistant hass)