Skip to content

Commit

Permalink
Merge pull request #8 from mliljedahl/bug/wrong_device
Browse files Browse the repository at this point in the history
Support for dimmers added.
  • Loading branch information
mliljedahl authored Oct 10, 2021
2 parents 7aa52c2 + bcead65 commit a052649
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 59 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ The following are the known limitations.
* The only sensors that have been tested are the temperature and humidity sensors. As for devices only on/off switches have been tested.
* Tested with a TellStick Duo, might also work with the TellStick or other controllers supported by telldus-core.


## Internal tools

`tdtool` is compiled to the docker image.

```
$ docker exec telldus-core-mqtt tdtool --help
```

## Development

For now the only sensors that have been tested are the temperature and humidity sensors. As for devices only on/off switches have been tested. So there is still much to test and develop in this project. Please file an [issue](https://github.com/mliljedahl/telldus-core-mqtt/issues) or even better, provide a [pull request](https://github.com/mliljedahl/telldus-core-mqtt/pulls).
Expand Down
2 changes: 1 addition & 1 deletion config_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ home_assistant:
state_topic: !ENV ${TDM_STATE_TOPIC:telldus}

telldus:
repeat_cmd: !ENV ${TDM_REPEAT_CMD:5}
repeat_cmd: !ENV ${TDM_REPEAT_CMD:3}

mqtt:
broker: !ENV ${TDM_MQTT_SERVER:127.0.0.1}
Expand Down
2 changes: 1 addition & 1 deletion logging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ loggers:
propagate: no
root:
level: INFO
handlers: [console]
handlers: [console]
104 changes: 69 additions & 35 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import logging.config

import src.telldus as telldus
import tellcore.telldus as td
import tellcore.constants as const
from src.telldus import const, td

from paho.mqtt import client as mqtt_client

Expand Down Expand Up @@ -64,42 +63,76 @@ def subscribe_device(client: mqtt_client):
def on_message(client, userdata, msg):
logging.info(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
id = msg.topic.split('/')[1]
module = msg.topic.split('/')[2]
action = msg.topic.split('/')[3]
cmd_status = False

# TODO: Support other than on/off
if int(msg.payload.decode()) == int(const.TELLSTICK_TURNON):
logging.debug('[DEVICE] Sending command ON to device id {}'.format(id))
cmd_status = d.turn_on(id)
if module == 'light':
if action == 'dim':
logging.debug('[DEVICE] Sending command DIM "{}" to device id {}'.format(int(msg.payload.decode()), id))
cmd_status = d.dim(id, int(msg.payload.decode()))

if int(msg.payload.decode()) == int(const.TELLSTICK_TURNOFF):
logging.debug('[DEVICE] Sending command OFF to device id {}'.format(id))
cmd_status = d.turn_off(id)
if action != 'dim':
if int(msg.payload.decode()) == int(const.TELLSTICK_TURNON):
logging.debug('[DEVICE] Sending command ON to device id {}'.format(id))
cmd_status = d.turn_on(id)

if cmd_status:
pass
if int(msg.payload.decode()) == int(const.TELLSTICK_TURNOFF):
logging.debug('[DEVICE] Sending command OFF to device id {}'.format(id))
cmd_status = d.turn_off(id)

if int(msg.payload.decode()) == int(const.TELLSTICK_BELL):
logging.debug('[DEVICE] Sending command BELL to device id {}'.format(id))
cmd_status = d.bell(id)

if int(msg.payload.decode()) == int(const.TELLSTICK_EXECUTE):
logging.debug('[DEVICE] Sending command EXECUTE to device id {}'.format(id))
cmd_status = d.execute(id)

if int(msg.payload.decode()) == int(const.TELLSTICK_UP):
logging.debug('[DEVICE] Sending command UP to device id {}'.format(id))
cmd_status = d.up(id)

if int(msg.payload.decode()) == int(const.TELLSTICK_DOWN):
logging.debug('[DEVICE] Sending command DOWN to device id {}'.format(id))
cmd_status = d.down(id)

if int(msg.payload.decode()) == int(const.TELLSTICK_STOP):
logging.debug('[DEVICE] Sending command STOP to device id {}'.format(id))
cmd_status = d.stop(id)

if not cmd_status:
logging.debug('[DEVICE] Command "{}" not supported, please open a github issue with this message.'.format(msg))

client.subscribe('{}/+/+/set'.format(config['home_assistant']['state_topic']))
client.on_message = on_message


def device_event(id_, method, data, cid):
method_string = METHODS.get(method, "UNKNOWN METHOD {0}".format(method))
string = "[DEVICE] {0} -> {1} ({2})".format(id_, method_string, method)
method_string = METHODS.get(method, 'UNKNOWN METHOD {0}'.format(method))
string = '[DEVICE] {0} -> {1} ({2})'.format(id_, method_string, method)
if method == const.TELLSTICK_DIM:
string += " [{0}]".format(data)
string += ' [{0}]'.format(data)
logging.debug(string)

# TODO: Need method to lookup id and get model, now assuming "switch"
topic = d.create_topic(id_, 'switch')
data = d.create_topic_data('switch', method)
publish_mqtt(mqtt_client, topic, data)
if method == const.TELLSTICK_DIM:
topic = d.create_topic(id_, 'light')
topic_data = d.create_topic_data('light', data)
else:
topic = d.create_topic(id_, 'switch')
topic_data = d.create_topic_data('switch', method)
publish_mqtt(mqtt_client, topic, topic_data)


def sensor_event(protocol, model, id_, dataType, value, timestamp, cid):
type_string = TYPES.get(dataType, "UNKNOWN METHOD {0}".format(dataType))
string = "[SENSOR] {0} {1} ({2}) = {3}".format(id_, model, type_string, value)
type_string = TYPES.get(dataType, 'UNKNOWN METHOD {0}'.format(dataType))
string = '[SENSOR] {0} {1} ({2}) = {3}'.format(id_, model, type_string, value)
logging.debug(string)

# Sensors can be added or discovered in telldus-core without a restart, ensure config topic for HASS
sensor_topics = s.create_topics(s.get(id_))
initial_publish(mqtt_client, sensor_topics)

topic = s.create_topic(id_, type_string)
data = s.create_topic_data(type_string, value)
publish_mqtt(mqtt_client, topic, data)
Expand All @@ -111,24 +144,35 @@ def initial_publish(mqtt_client, topics):
publish_mqtt(mqtt_client, topic['config']['topic'], topic['config']['data'])
if 'state' in topic:
publish_mqtt(mqtt_client, topic['state']['topic'], topic['state']['data'])
if 'command' in topic:
if 'data' in topic['command']:
publish_mqtt(mqtt_client, topic['command']['topic'], topic['command']['data'])



with open('./logging.yaml', 'r') as stream:
logging_config = yaml.load(stream, Loader=yaml.FullLoader)

logging.config.dictConfig(logging_config)
logger = logging.getLogger('telldus-core-mqtt')
logger = logging.getLogger('telldus-core-mqtt-main')

# Wait 5s for telldus-core to start and start collecting data
logging.info('Waiting for telldus-core to start...')
time.sleep(5)
logging.info('telldus-core have started.')

# Setup connection MQTT server
c = telldus.Telldus()
config = c.get_config
client_id = 'telldus-core-mqtt-{}'.format(random.randint(0, 1000))
mqtt_client = connect_mqtt(config, client_id)

# On program start, collect sensors to publish to MQTT server
s = telldus.Sensor(c.td_core)
sensor_topics = s.create_topics(s.get())
initial_publish(mqtt_client, sensor_topics)

# On program start, collect devices to publish to MQTT server
d = telldus.Device(c.td_core)
device_topics = d.create_topics(d.get())
initial_publish(mqtt_client, device_topics)

# Initialize event listener for telldus-core
telldus_core = asyncio.get_event_loop()
dispatcher = td.AsyncioCallbackDispatcher(telldus_core)
Expand All @@ -139,16 +183,6 @@ def initial_publish(mqtt_client, topics):
callbacks.append(core.register_device_event(device_event))
callbacks.append(core.register_sensor_event(sensor_event))

# On program start, collect sensors to publish to MQTT server
s = telldus.Sensor()
sensor_topics = s.create_topics(s.get())
initial_publish(mqtt_client, sensor_topics)

# On program start, collect devices to publish to MQTT server
d = telldus.Device()
device_topics = d.create_topics(d.get())
initial_publish(mqtt_client, device_topics)

# Main loop
try:
subscribe_device(mqtt_client)
Expand Down
Loading

0 comments on commit a052649

Please sign in to comment.