Refactoring of LCN component (#23824)

* Moved helper functions to const.py

* Removed pypck attribute from LcnDevice

* Bump to pypck==0.6.0

* Added myself as a codeowner

* Moved helper functions to helpers.py
This commit is contained in:
Andre Lengwenus 2019-05-25 11:40:44 +02:00 committed by Martin Hjelmare
parent 9d7aa8f05d
commit c928f82cbf
12 changed files with 144 additions and 129 deletions

View file

@ -2,7 +2,6 @@
import logging
import pypck
from pypck.connection import PchkConnectionManager
import voluptuous as vol
from homeassistant.components.climate import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP
@ -18,55 +17,13 @@ from .const import (
BINSENSOR_PORTS, CONF_CLIMATES, CONF_CONNECTIONS, CONF_DIM_MODE,
CONF_DIMMABLE, CONF_LOCKABLE, CONF_MAX_TEMP, CONF_MIN_TEMP, CONF_MOTOR,
CONF_OUTPUT, CONF_SETPOINT, CONF_SK_NUM_TRIES, CONF_SOURCE,
CONF_TRANSITION, DATA_LCN, DEFAULT_NAME, DIM_MODES, DOMAIN, KEYS,
LED_PORTS, LOGICOP_PORTS, MOTOR_PORTS, OUTPUT_PORTS, PATTERN_ADDRESS,
RELAY_PORTS, S0_INPUTS, SETPOINTS, THRESHOLDS, VAR_UNITS, VARIABLES)
CONF_TRANSITION, DATA_LCN, DIM_MODES, DOMAIN, KEYS, LED_PORTS,
LOGICOP_PORTS, MOTOR_PORTS, OUTPUT_PORTS, RELAY_PORTS, S0_INPUTS,
SETPOINTS, THRESHOLDS, VAR_UNITS, VARIABLES)
from .helpers import has_unique_connection_names, is_address
_LOGGER = logging.getLogger(__name__)
def has_unique_connection_names(connections):
"""Validate that all connection names are unique.
Use 'pchk' as default connection_name (or add a numeric suffix if
pchk' is already in use.
"""
for suffix, connection in enumerate(connections):
connection_name = connection.get(CONF_NAME)
if connection_name is None:
if suffix == 0:
connection[CONF_NAME] = DEFAULT_NAME
else:
connection[CONF_NAME] = '{}{:d}'.format(DEFAULT_NAME, suffix)
schema = vol.Schema(vol.Unique())
schema([connection.get(CONF_NAME) for connection in connections])
return connections
def is_address(value):
"""Validate the given address string.
Examples for S000M005 at myhome:
myhome.s000.m005
myhome.s0.m5
myhome.0.5 ("m" is implicit if missing)
Examples for s000g011
myhome.0.g11
myhome.s0.g11
"""
matcher = PATTERN_ADDRESS.match(value)
if matcher:
is_group = (matcher.group('type') == 'g')
addr = (int(matcher.group('seg_id')),
int(matcher.group('id')),
is_group)
conn_id = matcher.group('conn_id')
return addr, conn_id
raise vol.error.Invalid('Not a valid address string.')
BINARY_SENSORS_SCHEMA = vol.Schema({
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_ADDRESS): is_address,
@ -153,19 +110,6 @@ CONFIG_SCHEMA = vol.Schema({
}, extra=vol.ALLOW_EXTRA)
def get_connection(connections, connection_id=None):
"""Return the connection object from list."""
if connection_id is None:
connection = connections[0]
else:
for connection in connections:
if connection.connection_id == connection_id:
break
else:
raise ValueError('Unknown connection_id.')
return connection
async def async_setup(hass, config):
"""Set up the LCN component."""
hass.data[DATA_LCN] = {}
@ -179,13 +123,14 @@ async def async_setup(hass, config):
'DIM_MODE': pypck.lcn_defs.OutputPortDimMode[
conf_connection[CONF_DIM_MODE]]}
connection = PchkConnectionManager(hass.loop,
conf_connection[CONF_HOST],
conf_connection[CONF_PORT],
conf_connection[CONF_USERNAME],
conf_connection[CONF_PASSWORD],
settings=settings,
connection_id=connection_name)
connection = pypck.connection.PchkConnectionManager(
hass.loop,
conf_connection[CONF_HOST],
conf_connection[CONF_PORT],
conf_connection[CONF_USERNAME],
conf_connection[CONF_PASSWORD],
settings=settings,
connection_id=connection_name)
try:
# establish connection to PCHK server
@ -218,7 +163,6 @@ class LcnDevice(Entity):
def __init__(self, config, address_connection):
"""Initialize the LCN device."""
self.pypck = pypck
self.config = config
self.address_connection = address_connection
self._name = config[CONF_NAME]