Skip to content

Commit

Permalink
Moved helper functions to helpers.py
Browse files Browse the repository at this point in the history
  • Loading branch information
alengwenus committed May 19, 2019
1 parent ea9d992 commit 39da111
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 73 deletions.
4 changes: 2 additions & 2 deletions homeassistant/components/lcn/__init__.py
Expand Up @@ -19,8 +19,8 @@
CONF_OUTPUT, CONF_SETPOINT, CONF_SK_NUM_TRIES, CONF_SOURCE,
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, has_unique_connection_names,
is_address)
SETPOINTS, THRESHOLDS, VAR_UNITS, VARIABLES)
from .helpers import has_unique_connection_names, is_address

_LOGGER = logging.getLogger(__name__)

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/lcn/binary_sensor.py
Expand Up @@ -6,8 +6,8 @@

from . import LcnDevice
from .const import (
BINSENSOR_PORTS, CONF_CONNECTIONS, CONF_SOURCE, DATA_LCN, SETPOINTS,
get_connection)
BINSENSOR_PORTS, CONF_CONNECTIONS, CONF_SOURCE, DATA_LCN, SETPOINTS)
from .helpers import get_connection


async def async_setup_platform(hass, hass_config, async_add_entities,
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/lcn/climate.py
Expand Up @@ -8,7 +8,8 @@
from . import LcnDevice
from .const import (
CONF_CONNECTIONS, CONF_LOCKABLE, CONF_MAX_TEMP, CONF_MIN_TEMP,
CONF_SETPOINT, CONF_SOURCE, DATA_LCN, get_connection)
CONF_SETPOINT, CONF_SOURCE, DATA_LCN)
from .helpers import get_connection


async def async_setup_platform(hass, hass_config, async_add_entities,
Expand Down
64 changes: 1 addition & 63 deletions homeassistant/components/lcn/const.py
@@ -1,20 +1,13 @@
# coding: utf-8
"""Constants for the LCN component."""
from itertools import product
import re

import voluptuous as vol

from homeassistant.const import CONF_NAME, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT

DOMAIN = 'lcn'
DATA_LCN = 'lcn'
DEFAULT_NAME = 'pchk'

# Regex for address validation
PATTERN_ADDRESS = re.compile('^((?P<conn_id>\\w+)\\.)?s?(?P<seg_id>\\d+)'
'\\.(?P<type>m|g)?(?P<id>\\d+)$')

CONF_CONNECTIONS = 'connections'
CONF_SK_NUM_TRIES = 'sk_num_tries'
CONF_OUTPUT = 'output'
Expand Down Expand Up @@ -77,58 +70,3 @@
'VOLT', 'V',
'AMPERE', 'AMP', 'A',
'DEGREE', '°']


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


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.')
3 changes: 2 additions & 1 deletion homeassistant/components/lcn/cover.py
Expand Up @@ -5,7 +5,8 @@
from homeassistant.const import CONF_ADDRESS

from . import LcnDevice
from .const import CONF_CONNECTIONS, CONF_MOTOR, DATA_LCN, get_connection
from .const import CONF_CONNECTIONS, CONF_MOTOR, DATA_LCN
from .helpers import get_connection


async def async_setup_platform(hass, hass_config, async_add_entities,
Expand Down
67 changes: 67 additions & 0 deletions homeassistant/components/lcn/helpers.py
@@ -0,0 +1,67 @@
"""Helpers for LCN component."""
import re

import voluptuous as vol

from homeassistant.const import CONF_NAME

from .const import DEFAULT_NAME

# Regex for address validation
PATTERN_ADDRESS = re.compile('^((?P<conn_id>\\w+)\\.)?s?(?P<seg_id>\\d+)'
'\\.(?P<type>m|g)?(?P<id>\\d+)$')


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


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.')
3 changes: 2 additions & 1 deletion homeassistant/components/lcn/light.py
Expand Up @@ -9,7 +9,8 @@
from . import LcnDevice
from .const import (
CONF_CONNECTIONS, CONF_DIMMABLE, CONF_OUTPUT, CONF_TRANSITION, DATA_LCN,
OUTPUT_PORTS, get_connection)
OUTPUT_PORTS)
from .helpers import get_connection


async def async_setup_platform(
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/lcn/sensor.py
Expand Up @@ -6,7 +6,8 @@
from . import LcnDevice
from .const import (
CONF_CONNECTIONS, CONF_SOURCE, DATA_LCN, LED_PORTS, S0_INPUTS, SETPOINTS,
THRESHOLDS, VARIABLES, get_connection)
THRESHOLDS, VARIABLES)
from .helpers import get_connection


async def async_setup_platform(hass, hass_config, async_add_entities,
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/lcn/switch.py
Expand Up @@ -5,8 +5,8 @@
from homeassistant.const import CONF_ADDRESS

from . import LcnDevice
from .const import (
CONF_CONNECTIONS, CONF_OUTPUT, DATA_LCN, OUTPUT_PORTS, get_connection)
from .const import CONF_CONNECTIONS, CONF_OUTPUT, DATA_LCN, OUTPUT_PORTS
from .helpers import get_connection


async def async_setup_platform(hass, hass_config, async_add_entities,
Expand Down

0 comments on commit 39da111

Please sign in to comment.