This repo provides an MQTT interface to the awesome python-kasa library. With that, one can use MQTT clients to turn on/off any compatible TP-Link device. Conversely, by subscribing to specific MQTT topics, users can be notified about state changes to any device configured.
The TP-Link / Kasa devices managed are kept in an easy to edit YAML file that looks like this:
mqtt:
# ip/dns for the mqtt broker
host: 192.168.1.250
username: <mqtt username>
password: <mqtt password>
globals:
# every location will be managed using a unique mqtt topic
# unless explicitly specified, this format will be used
topic_format: /{}/switch
# kasa will monitor the current state of the device every
# poll interval, in seconds. You can override on a per device
poll_interval: 11
locations:
# coffee maker. To turn it on, use mqtt publish
# topic: /coffee_maker/switch payload: on
# subscribe to /coffee_maker/switch to know its state
coffee_maker:
host: 192.168.1.21
# toaster is similar to the coffee maker, except it relies on
# kasa discovery in order to locate the device via its alias.
toaster:
alias: toaster
# example where topic is explicitly provided for a given device
kitchen lights:
host: 192.168.1.22
topic: /kitchen/light_switch
# example where we indicate a specific poll interval
pantry:
alias: storage
poll_interval: 120
Devices are connected via host or discovered by Kasa via alias. There are more attributes for further customizations, shown in the data/config.yaml file.
Starting this project can be done by setting up a service (see mqtt2kasa.service as reference), or doing the following steps:
$ ./mqtt2kasa/bin/create-env.sh && \
source ./env/bin/activate && \
export PYTHONPATH=${PWD}
$ python3 mqtt2kasa/main.py ./data/config.yaml
Granted the config properly refers to the TP-Link devices in the network, use regular MQTT tools for controlling and monitoring. Example below.
Controlling the devices via MQTT via publish:
$ MQTT=192.168.1.250 && \
mosquitto_pub -h $MQTT -t /kitchen/light_switch -m off
Subscribe to see changes to devices, regardless on how they were controlled:
$ MQTT=192.168.1.250 && \
mosquitto_sub -F '@Y-@m-@dT@H:@M:@S@z : %q : %t : %p' -h $MQTT -t '/+/switch' -t /kitchen/light_switch
2021-02-18T10:16:19-0500 : 0 : /kitchen/light_switch : on
2021-02-18T10:16:26-0500 : 0 : /kitchen/light_switch : off
2021-01-30T21:43:03-0500 : 0 : /toaster/switch : on
In order to damper endless on/off cycles, this implementation sets an async throttle for each device. If there is a need to tweak that, the attributes are located in kasa_wrapper.py.
NOTE: Use python 3.7 or newer, as this project requires a somewhat recent implementation of asyncio.
- Add support for the smart strip, so each 'child' can be controlled independently;
- Expose throttle config instead of hardcoded values;
- Improve documentation?!?
- Use strict yaml (https://hitchdev.com/strictyaml/)