Skip to content

Commit

Permalink
Merge pull request #5 from kobbejager/master
Browse files Browse the repository at this point in the history
python 3 compatibility
  • Loading branch information
martenjacobs authored Aug 10, 2018
2 parents 219a37d + 19f156a commit 21a4a6e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
23 changes: 15 additions & 8 deletions __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# Values used to parse boolean values of incoming messages
true_values=('True', 'true', '1', 'y', 'yes')
false_values=('False', 'false', '0', 'n', 'no')

# Default settings
settings = {
Expand Down Expand Up @@ -57,28 +58,28 @@ def on_mqtt_connect(client, userdata, flags, rc):
def on_mqtt_message(client, userdata, msg):
# Handle incoming messages
log.info("Received message on topic {} with payload {}".format(
msg.topic, str(msg.payload)))
msg.topic, str(msg.payload.decode('ascii', 'ignore'))))
namespace = settings['mqtt']['sub_topic_namespace']
command_generators={
"{}/room_setpoint/temporary".format(namespace): \
lambda _ :"TT={:.2f}".format(float(_)),
lambda _ :"TT={:.2f}".format(float(_) if is_float(_) else 0),
"{}/room_setpoint/constant".format(namespace): \
lambda _ :"TC={:.2f}".format(float(_)),
lambda _ :"TC={:.2f}".format(float(_) if is_float(_) else 0),
"{}/outside_temperature".format(namespace): \
lambda _ :"OT={:.2f}".format(float(_)),
lambda _ :"OT={:.2f}".format(float(_) if is_float(_) else 99),
"{}/hot_water/enable".format(namespace): \
lambda _ :"HW={}".format('1' if _ in true_values else '0'),
lambda _ :"HW={}".format('1' if _ in true_values else '0' if _ in false_values else 'T'),
"{}/hot_water/temperature".format(namespace): \
lambda _ :"SW={:.2f}".format(float(_)),
lambda _ :"SW={:.2f}".format(float(_) if is_float(_) else 60),
"{}/central_heating/enable".format(namespace): \
lambda _ :"CH={}".format('1' if _ in true_values else '0'),
lambda _ :"CH={}".format('0' if _ in false_values else '1'),
# TODO: "set/otgw/raw/+": lambda _ :publish_to_otgw("PS", _)
}
# Find the correct command generator from the dict above
command_generator = command_generators.get(msg.topic)
if command_generator:
# Get the command and send it to the OTGW
command = command_generator(msg.payload)
command = command_generator(msg.payload.decode('ascii', 'ignore'))
log.info("Sending command: '{}'".format(command))
otgw_client.write("{}\r".format(command))

Expand All @@ -92,6 +93,12 @@ def on_otgw_message(message):
qos=settings['mqtt']['qos'],
retain=settings['mqtt']['retain'])

def is_float(value):
try:
float(value)
return True
except ValueError:
return False

log.info("Initializing MQTT")

Expand Down
4 changes: 2 additions & 2 deletions opentherm_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def write(self, data):
r"""
Write data to the serial device
"""
self._serial.write("{}\r\n".format(data.rstrip('\r\n')))
self._serial.write("{}\r\n".format(data.rstrip('\r\n')).encode('ascii', 'ignore'))
self._serial.flush()

def read(self, timeout):
Expand All @@ -44,4 +44,4 @@ def read(self, timeout):
"""
if(self._serial.timeout != timeout):
self._serial.timeout = timeout
return self._serial.read(128)
return self._serial.read(128).decode('ascii', 'ignore')

0 comments on commit 21a4a6e

Please sign in to comment.