Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions elro/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import json

import trio
from valideer import accepts
import valideer
import re

from elro.command import Command
from elro.device import create_device_from_data
Expand All @@ -19,16 +18,20 @@ class Hub:
CTRL_KEY = '0'
BIND_KEY = '0'

@accepts(ip=valideer.Pattern(f"^(mqtt://)?({ip_address})|({hostname})$"),
port="integer",
device_id=valideer.Pattern("^ST_([0-9A-Fa-f]{12})$"))
def __init__(self, ip, port, device_id):
"""
Constructor
:param ip: The ip of the K1
:param port: The port of the K1 (usually 1025)
:param device_id: The device id of the K1 (starts with ST_ followed by its MAC address without colons)
"""
if (re.search(f"^({ip_address})|({hostname})$", ip) is None):
raise TypeError(f"Invalid ip ({ip})")
if (not isinstance(port, int)):
raise TypeError(f"Port should be an integer ({port})")
if (re.search("^ST_([0-9A-Fa-f]{12})$", device_id) is None):
raise TypeError(f"Invalid device id ({device_id})")

self.ip = ip
self.port = port
self.id = device_id
Expand Down
13 changes: 8 additions & 5 deletions elro/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
import json
import re

import trio
from distmqtt.client import open_mqttclient
from distmqtt.mqtt.constants import QOS_1
from valideer import accepts, Pattern

from elro.validation import ip_address, hostname

Expand All @@ -13,8 +13,6 @@ class MQTTPublisher:
"""
A MQTTPublisher listens to all hub events and publishes messages to an MQTT broker accordingly
"""
@accepts(broker_host=Pattern(f"({ip_address}|{hostname})"),
base_topic=Pattern("^[/_\\-a-zA-Z0-9]*$"))
def __init__(self, broker_host, ha_autodiscover, base_topic=None):
"""
Constructor
Expand All @@ -23,6 +21,12 @@ def __init__(self, broker_host, ha_autodiscover, base_topic=None):
:param base_topic: The base topic to publish under, i.e., the publisher publishes messages under
<base topic>/elro/<device name or id>
"""
if (re.search(f"({ip_address}|{hostname})", broker_host) is None):
raise TypeError(f"Invalid broker host ({broker_host})")

if (re.search("^[/_\\-a-zA-Z0-9]*$", base_topic) is None):
raise TypeError(f"Invalid base topic ({base_topic})")

self.broker_host = broker_host
if not self.broker_host.startswith("mqtt://"):
self.broker_host = f"mqtt://{self.broker_host}"
Expand Down Expand Up @@ -110,8 +114,7 @@ async def handle_device_discovery(self, device):
"json_attributes_topic": f"{self.topic_name(device)}",
"unique_id": f"elro_k1_device_{device.id}"
}).encode('utf8'),
QOS_1,
retain=True
QOS_1
)

async def device_message_task(self, hub):
Expand Down
8 changes: 4 additions & 4 deletions elro/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging
import collections

from valideer import accepts, Pattern

import re

# From the ByteUtil class, needed by CRC_maker
auchCRCHi = [
Expand Down Expand Up @@ -81,7 +79,6 @@ def get_string_from_ascii(input):

return name

@accepts(input=Pattern("^[_\-a-zA-Z0-9 ]*$")) # Not fully supporting a wide range of characters due to partial implementation
def get_ascii(input):
"""
This function is partially reversed engineered and translated to python
Expand All @@ -90,6 +87,9 @@ def get_ascii(input):
:param input: A string
:return: A hex string
"""
if (re.search("^[_\-a-zA-Z0-9 ]*$", input) is None):
raise TypeError("Not fully supporting a wide range of characters due to partial implementation")

countf = 0

try:
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ scripts =
bin/elro
install_requires =
trio
valideer
distmqtt
getmac
[options.extras_require]
Expand Down