Hey guys,
here a small how to (collection of infos from this forum and the net in compact form ) to make Xiaomi Smart Home products available in Pimatic.
Preconditions:
- Xiaomi Gateway must be in developer mode (see here)
- MQTT Broker (sudo apt-get install mosquitto)
- python-pip (sudo apt-get install python-pip)
– paho-mqtt (sudo pip install paho-mqtt)
– future (sudo pip install future)
Clone miHime from Git (https://github.com/jon1012/mihome) to e.g. /home/pi/ and change to mihome directory.
cd ~/mihome
then
sudo python setup.py install
Create a python script e.g. miHome_MQTT.py with this content:
import paho.mqtt.client as mqtt
from mihome.connector import XiaomiConnector
MQTT_SERVER = "127.0.0.1"
MQTT_PORT = 1883
PATH_FMT = "xiaomi/{model}/{sid}/{prop}" # short_id or sid ?
def prepare_mqtt():
client = mqtt.Client()
client.connect(MQTT_SERVER, MQTT_PORT, 60)
return client
def push_data(client, model, sid, cmd, data):
for key, value in data.items():
path = PATH_FMT.format(model=model,
sid=sid,
cmd=cmd,
prop=key)
client.publish(path, payload=value, qos=0)
client = prepare_mqtt()
cb = lambda m, s, c, d: push_data(client, m, s, c, d)
connector = XiaomiConnector(data_callback=cb)
while True:
connector.check_incoming()
Execute the script. For me it will be automatically via a rule during pimatic startup.
python ~/Homation/miHome_MQTT.py
Now you should get some results in your console like:
{u'model': u'weather.v1', u'cmd': u'read_ack', u'data': u'{"voltage":3045,"temperature":"2210","humidity":"5359","pressure":"98880"}', u'short_id': 28976, u'sid': u'158d0001ab55c0'}
{u'model': u'weather.v1', u'cmd': u'read_ack', u'data': u'{"voltage":3075,"temperature":"2208","humidity":"5365","pressure":"98890"}', u'short_id': 23182, u'sid': u'158d0001ab43ab'}
{u'model': u'weather.v1', u'cmd': u'read_ack', u'data': u'{"voltage":3015,"temperature":"2209","humidity":"5367","pressure":"98870"}', u'short_id': 32762, u'sid': u'158d0001ab3aab'}
{u'model': u'weather.v1', u'cmd': u'read_ack', u'data': u'{"voltage":3055,"temperature":"2210","humidity":"5358","pressure":"98880"}', u'short_id': 2586, u'sid': u'158d0001ab1346'}
{u'short_id': u'0', u'cmd': u'heartbeat', u'token': u'tWDavUiX4NnYErHN', u'sid': u'34ce12487e9', u'model': u'gateway', u'data': u'{"ip":"192.168.178.206"}'}
Open a second console and type in:
mosquitto_sub -v -t 'xiaomi/#'
The result should be something like this. This is the topic you can use for a MqttSensor.
xiaomi/weather.v1/158d0001ab55c0/temperature 2211
xiaomi/weather.v1/158d0001ab55c0/humidity 5390
xiaomi/weather.v1/158d0001ab55c0/pressure 98730
xiaomi/weather.v1/158d0001ab43ab/temperature 2210
xiaomi/weather.v1/158d0001ab43ab/humidity 5378
xiaomi/weather.v1/158d0001ab43ab/pressure 98730
xiaomi/weather.v1/158d0001ab3aab/temperature 2209
xiaomi/weather.v1/158d0001ab3aab/humidity 5387
xiaomi/weather.v1/158d0001ab3aab/pressure 98710
xiaomi/gateway/34ce12487e9/ip 192.168.178.206
Hope this helps.