2019-02-14 04:35:12 +00:00
|
|
|
"""Support for LCN devices."""
|
2018-12-28 11:39:06 +00:00
|
|
|
import logging
|
|
|
|
|
2019-04-24 02:20:20 +00:00
|
|
|
import pypck
|
2018-12-28 11:39:06 +00:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_BINARY_SENSORS,
|
|
|
|
CONF_COVERS,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_LIGHTS,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_SENSORS,
|
|
|
|
CONF_SWITCHES,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2018-12-28 11:39:06 +00:00
|
|
|
from homeassistant.helpers.discovery import async_load_platform
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CLIMATES,
|
|
|
|
CONF_CONNECTIONS,
|
|
|
|
CONF_DIM_MODE,
|
|
|
|
CONF_SCENES,
|
|
|
|
CONF_SK_NUM_TRIES,
|
|
|
|
DATA_LCN,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2020-12-05 11:57:49 +00:00
|
|
|
from .schemas import CONFIG_SCHEMA # noqa: 401
|
2019-05-29 22:59:38 +00:00
|
|
|
from .services import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DynText,
|
|
|
|
Led,
|
|
|
|
LockKeys,
|
|
|
|
LockRegulator,
|
|
|
|
OutputAbs,
|
|
|
|
OutputRel,
|
|
|
|
OutputToggle,
|
|
|
|
Pck,
|
|
|
|
Relays,
|
|
|
|
SendKeys,
|
|
|
|
VarAbs,
|
|
|
|
VarRel,
|
|
|
|
VarReset,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-12-28 11:39:06 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the LCN component."""
|
|
|
|
hass.data[DATA_LCN] = {}
|
|
|
|
|
|
|
|
conf_connections = config[DOMAIN][CONF_CONNECTIONS]
|
|
|
|
connections = []
|
|
|
|
for conf_connection in conf_connections:
|
|
|
|
connection_name = conf_connection.get(CONF_NAME)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
settings = {
|
|
|
|
"SK_NUM_TRIES": conf_connection[CONF_SK_NUM_TRIES],
|
|
|
|
"DIM_MODE": pypck.lcn_defs.OutputPortDimMode[
|
|
|
|
conf_connection[CONF_DIM_MODE]
|
|
|
|
],
|
|
|
|
}
|
2018-12-28 11:39:06 +00:00
|
|
|
|
2019-05-25 09:40:44 +00:00
|
|
|
connection = pypck.connection.PchkConnectionManager(
|
|
|
|
conf_connection[CONF_HOST],
|
|
|
|
conf_connection[CONF_PORT],
|
|
|
|
conf_connection[CONF_USERNAME],
|
|
|
|
conf_connection[CONF_PASSWORD],
|
|
|
|
settings=settings,
|
2019-07-31 19:25:30 +00:00
|
|
|
connection_id=connection_name,
|
|
|
|
)
|
2018-12-28 11:39:06 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# establish connection to PCHK server
|
|
|
|
await hass.async_create_task(connection.async_connect(timeout=15))
|
|
|
|
connections.append(connection)
|
|
|
|
_LOGGER.info('LCN connected to "%s"', connection_name)
|
|
|
|
except TimeoutError:
|
2020-07-05 21:04:19 +00:00
|
|
|
_LOGGER.error('Connection to PCHK server "%s" failed', connection_name)
|
2018-12-28 11:39:06 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
hass.data[DATA_LCN][CONF_CONNECTIONS] = connections
|
|
|
|
|
2019-02-23 09:13:15 +00:00
|
|
|
# load platforms
|
2019-07-31 19:25:30 +00:00
|
|
|
for component, conf_key in (
|
|
|
|
("binary_sensor", CONF_BINARY_SENSORS),
|
|
|
|
("climate", CONF_CLIMATES),
|
|
|
|
("cover", CONF_COVERS),
|
|
|
|
("light", CONF_LIGHTS),
|
|
|
|
("scene", CONF_SCENES),
|
|
|
|
("sensor", CONF_SENSORS),
|
|
|
|
("switch", CONF_SWITCHES),
|
|
|
|
):
|
2019-02-23 14:58:18 +00:00
|
|
|
if conf_key in config[DOMAIN]:
|
|
|
|
hass.async_create_task(
|
2019-07-31 19:25:30 +00:00
|
|
|
async_load_platform(
|
|
|
|
hass, component, DOMAIN, config[DOMAIN][conf_key], config
|
|
|
|
)
|
|
|
|
)
|
2019-05-29 22:59:38 +00:00
|
|
|
|
|
|
|
# register service calls
|
2019-07-31 19:25:30 +00:00
|
|
|
for service_name, service in (
|
|
|
|
("output_abs", OutputAbs),
|
|
|
|
("output_rel", OutputRel),
|
|
|
|
("output_toggle", OutputToggle),
|
|
|
|
("relays", Relays),
|
|
|
|
("var_abs", VarAbs),
|
|
|
|
("var_reset", VarReset),
|
|
|
|
("var_rel", VarRel),
|
|
|
|
("lock_regulator", LockRegulator),
|
|
|
|
("led", Led),
|
|
|
|
("send_keys", SendKeys),
|
|
|
|
("lock_keys", LockKeys),
|
|
|
|
("dyn_text", DynText),
|
|
|
|
("pck", Pck),
|
|
|
|
):
|
|
|
|
hass.services.async_register(
|
2020-12-07 12:13:41 +00:00
|
|
|
DOMAIN, service_name, service(hass).async_call_service, service.schema
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-05-29 22:59:38 +00:00
|
|
|
|
2018-12-28 11:39:06 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
class LcnEntity(Entity):
|
2018-12-28 11:39:06 +00:00
|
|
|
"""Parent class for all devices associated with the LCN component."""
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
def __init__(self, config, device_connection):
|
2018-12-28 11:39:06 +00:00
|
|
|
"""Initialize the LCN device."""
|
|
|
|
self.config = config
|
2020-12-05 11:57:49 +00:00
|
|
|
self.device_connection = device_connection
|
2018-12-28 11:39:06 +00:00
|
|
|
self._name = config[CONF_NAME]
|
|
|
|
|
|
|
|
@property
|
2019-01-20 13:50:29 +00:00
|
|
|
def should_poll(self):
|
2018-12-28 11:39:06 +00:00
|
|
|
"""Lcn device entity pushes its state to HA."""
|
|
|
|
return False
|
|
|
|
|
2019-01-20 13:50:29 +00:00
|
|
|
async def async_added_to_hass(self):
|
2018-12-28 11:39:06 +00:00
|
|
|
"""Run when entity about to be added to hass."""
|
2021-01-06 15:47:02 +00:00
|
|
|
self.device_connection.register_for_inputs(self.input_received)
|
2018-12-28 11:39:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
def input_received(self, input_obj):
|
|
|
|
"""Set state/value when LCN input object (command) is received."""
|