Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved helper functions to helpers.py
- Loading branch information
1 parent
ea9d992
commit 39da111
Showing
9 changed files
with
82 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters