Skip to content

Commit

Permalink
emeter: publish key and values as topics of emeter (#12)
Browse files Browse the repository at this point in the history
Values of emeter are also published individually.

Example:
    /dehumidifier/switch/emeter : <EmeterStatus power=3.14 voltage=122.049119 current=0.1256 total=51.493>
    /dehumidifier/switch/emeter/power : 3.14          <-- new
    /dehumidifier/switch/emeter/voltage : 122.049119  <-- new
    /dehumidifier/switch/emeter/current : 0.1256      <-- new
    /dehumidifier/switch/emeter/total : 51.493        <-- new

Fixes: #10

Signed-off-by: Flavio Fernandes <[email protected]>
Co-authored-by: Your Name <[email protected]>
  • Loading branch information
flavio-fernandes and Your Name committed Oct 21, 2023
1 parent 6408ab5 commit 9e608e0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ $ MQTT=192.168.1.250 && \
**NOTE on Metering**: If metering is supported by device and `emeter_poll_interval` was provided, it will be published via topics that end with "emeter":

```
$ mosquitto_sub -h $MQTT -t '/+/switch/emeter'
$ mosquitto_sub -h $MQTT -t '/+/switch/emeter' -t '/+/switch/emeter/#'
```

In order to damper endless on/off cycles, this implementation sets an
Expand Down
8 changes: 8 additions & 0 deletions mqtt2kasa/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import collections
from contextlib import AsyncExitStack
import re

from aiomqtt import Client, MqttError

Expand Down Expand Up @@ -67,6 +68,13 @@ async def handle_emeter_event_kasa(
)
await mqtt_send_q.put(MqttMsgEvent(topic=topic, payload=payload))

# also publish each value as a topic
# https://github.com/flavio-fernandes/mqtt2kasa/issues/10
matches = re.findall(r"(\w+)=([^\s>]+)", payload)
for key, value in matches:
emeter_topic = f"{topic}/{key}"
await mqtt_send_q.put(MqttMsgEvent(topic=emeter_topic, payload=value))


async def handle_main_event_mqtt(
mqtt_msg: MqttMsgEvent, run_state: RunState, mqtt_send_q: asyncio.Queue
Expand Down
2 changes: 1 addition & 1 deletion mqtt2kasa/tests/basic_test.sh.vagrant
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ get_simulator_lines () {

# restart service to trigger discovery
sudo systemctl restart mqtt2kasa
sleep 5
sleep 10
echo TEST: Check discovery
get_log_lines 15
grep --quiet -E 'Discovered 192\.168\.123\.201 .*thing1' ${TMP_OUTPUT} || \
Expand Down

0 comments on commit 9e608e0

Please sign in to comment.