Skip to content

Commit

Permalink
Speed improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mliljedahl committed Oct 24, 2021
1 parent 3f89b0c commit d8798cd
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 92 deletions.
72 changes: 41 additions & 31 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import asyncio
import logging.config
import random
import threading
import time

import yaml
Expand All @@ -12,6 +13,8 @@
import src.telldus as telldus
from src.telldus import const, td

THREADING_RLOCK = threading.RLock()

TYPES = {const.TELLSTICK_TEMPERATURE: 'temperature',
const.TELLSTICK_HUMIDITY: 'humidity',
const.TELLSTICK_RAINRATE: 'rainrate',
Expand Down Expand Up @@ -50,14 +53,13 @@ def on_connect(client, userdata, flags, return_code):


def publish_mqtt(client, topic, msg):
time.sleep(1)

result = client.publish(topic, msg, retain=True)
with THREADING_RLOCK:
result = client.publish(topic, msg, retain=True)

if result[0] == 0:
logging.info('Send "%s" to topic "%s"', msg, topic)
else:
logging.error('Failed to send message to topic "%s"', topic)
if result[0] == 0:
logging.info('Send "%s" to topic "%s"', msg, topic)
else:
logging.error('Failed to send message to topic "%s"', topic)


def subscribe_device(client: mqtt_client):
Expand Down Expand Up @@ -90,39 +92,47 @@ def on_message(client, userdata, msg):

if action != 'dim' and module != 'light':
if int(msg.payload.decode()) == int(const.TELLSTICK_TURNON):
topic = d.create_topic(device_id, 'switch')
topic_data = d.create_topic_data('switch', const.TELLSTICK_TURNON)
publish_mqtt(mqtt_device, topic, topic_data)

logging.debug('[DEVICE] Sending command ON to device '
'id %s', device_id)
cmd_status = d.turn_on(device_id)

if int(msg.payload.decode()) == int(const.TELLSTICK_TURNOFF):
topic = d.create_topic(device_id, 'switch')
topic_data = d.create_topic_data('switch', const.TELLSTICK_TURNOFF)
publish_mqtt(mqtt_device, topic, topic_data)

logging.debug('[DEVICE] Sending command OFF to device '
'id %s', device_id)
cmd_status = d.turn_off(device_id)

if int(msg.payload.decode()) == int(const.TELLSTICK_BELL):
logging.debug('[DEVICE] Sending command BELL to device '
'id %s', device_id)
cmd_status = d.bell(device_id)

if int(msg.payload.decode()) == int(const.TELLSTICK_EXECUTE):
logging.debug('[DEVICE] Sending command EXECUTE to device '
'id %s', device_id)
cmd_status = d.execute(device_id)

if int(msg.payload.decode()) == int(const.TELLSTICK_UP):
logging.debug('[DEVICE] Sending command UP to device id %s',
device_id)
cmd_status = d.up(device_id)

if int(msg.payload.decode()) == int(const.TELLSTICK_DOWN):
logging.debug('[DEVICE] Sending command DOWN to device id %s',
device_id)
cmd_status = d.down(device_id)

if int(msg.payload.decode()) == int(const.TELLSTICK_STOP):
logging.debug('[DEVICE] Sending command STOP to device id %s',
device_id)
cmd_status = d.stop(device_id)
# if int(msg.payload.decode()) == int(const.TELLSTICK_BELL):
# logging.debug('[DEVICE] Sending command BELL to device '
# 'id %s', device_id)
# cmd_status = d.bell(device_id)

# if int(msg.payload.decode()) == int(const.TELLSTICK_EXECUTE):
# logging.debug('[DEVICE] Sending command EXECUTE to device '
# 'id %s', device_id)
# cmd_status = d.execute(device_id)

# if int(msg.payload.decode()) == int(const.TELLSTICK_UP):
# logging.debug('[DEVICE] Sending command UP to device id %s',
# device_id)
# cmd_status = d.up(device_id)

# if int(msg.payload.decode()) == int(const.TELLSTICK_DOWN):
# logging.debug('[DEVICE] Sending command DOWN to device id %s',
# device_id)
# cmd_status = d.down(device_id)

# if int(msg.payload.decode()) == int(const.TELLSTICK_STOP):
# logging.debug('[DEVICE] Sending command STOP to device id %s',
# device_id)
# cmd_status = d.stop(device_id)

if not cmd_status:
logging.debug('[DEVICE] Command "%s" not supported, please open'
Expand Down
118 changes: 57 additions & 61 deletions src/telldus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import json
import logging
import time
import threading

import tellcore.constants as const
import tellcore.telldus as td
Expand All @@ -17,6 +17,8 @@
logging.config.dictConfig(logging_config)
logger = logging.getLogger('telldus-core-mqtt')

THREADING_RLOCK = threading.RLock()


class Telldus:
def __init__(self, core=None):
Expand Down Expand Up @@ -306,80 +308,74 @@ def get(self, device_id=None):
return devices_data

def turn_on(self, device_id):
device = self._find_device(device_id)
if device is not None:
for _i in range(int(self.config['telldus']['repeat_cmd'])):
time.sleep(1)
device.turn_on()
return True
return False
with THREADING_RLOCK:
device = self._find_device(device_id)
if device is not None:
for _i in range(int(self.config['telldus']['repeat_cmd'])):
device.turn_on()
return True
return False

def turn_off(self, device_id):
device = self._find_device(device_id)
if device is not None:
for _i in range(int(self.config['telldus']['repeat_cmd'])):
time.sleep(1)
device.turn_off()
return True
return False

def bell(self, device_id):
device = self._find_device(device_id)
if device is not None:
for _i in range(int(self.config['telldus']['repeat_cmd'])):
time.sleep(1)
device.bell()
return True
return False
with THREADING_RLOCK:
device = self._find_device(device_id)
if device is not None:
for _i in range(int(self.config['telldus']['repeat_cmd'])):
device.turn_off()
return True
return False

def dim(self, device_id, value):
if int(value) >= 0 and int(value) <= 255:
device = self._find_device(device_id)
if device is not None:
for _i in range(int(self.config['telldus']['repeat_cmd'])):
time.sleep(1)
device.dim(int(value))
return True

logging.warning('Dim value "%d" not in range 0 - 255', int(value))
return False

def execute(self, device_id):
device = self._find_device(device_id)
if device is not None:
for _i in range(int(self.config['telldus']['repeat_cmd'])):
time.sleep(1)
device.execute()
return True
return False

def up(self, device_id):
# pylint: disable=invalid-name
device = self._find_device(device_id)
if device is not None:
for _i in range(int(self.config['telldus']['repeat_cmd'])):
time.sleep(1)
device.up()
return True
return False

def down(self, device_id):
device = self._find_device(device_id)
if device is not None:
for _i in range(int(self.config['telldus']['repeat_cmd'])):
time.sleep(1)
device.down()
return True
return False

def stop(self, device_id):
device = self._find_device(device_id)
if device is not None:
for _i in range(int(self.config['telldus']['repeat_cmd'])):
time.sleep(1)
device.stop()
return True
return False
# def bell(self, device_id):
# device = self._find_device(device_id)
# if device is not None:
# for _i in range(int(self.config['telldus']['repeat_cmd'])):
# device.bell()
# return True
# return False

# def execute(self, device_id):
# device = self._find_device(device_id)
# if device is not None:
# for _i in range(int(self.config['telldus']['repeat_cmd'])):
# device.execute()
# return True
# return False

# def up(self, device_id):
# # pylint: disable=invalid-name
# device = self._find_device(device_id)
# if device is not None:
# for _i in range(int(self.config['telldus']['repeat_cmd'])):
# device.up()
# return True
# return False

# def down(self, device_id):
# device = self._find_device(device_id)
# if device is not None:
# for _i in range(int(self.config['telldus']['repeat_cmd'])):
# device.down()
# return True
# return False

# def stop(self, device_id):
# device = self._find_device(device_id)
# if device is not None:
# for _i in range(int(self.config['telldus']['repeat_cmd'])):
# device.stop()
# return True
# return False

def _find_device(self, device_id):
for device in self.core.devices():
Expand Down

0 comments on commit d8798cd

Please sign in to comment.