Skip to content

Instantly share code, notes, and snippets.

@bphermansson
Created June 29, 2018 13:21
Show Gist options
  • Select an option

  • Save bphermansson/8084539fe0af81b256d055a25061daf9 to your computer and use it in GitHub Desktop.

Select an option

Save bphermansson/8084539fe0af81b256d055a25061daf9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''
Read Mqtt messages from Home Assistant and send them via RF.
Reads codes with a 433MHz receiver and prints them.
This code is connected to a Mqtt message in the table below, and is sent when that message arrives.
Toggle the switch in Home Assistant and the script shows a code.
Received rf code:
20
'''
codes = [
["turn_on_light.p_arum_window", "21"],
["turn_off_light.p_arum_window", 20]
]
import serial, json, time
import paho.mqtt.client as mqtt
import traceback
import logging
MQTT_HOST = '192.168.1.79'
MQTT_PORT = 1883
MQTT_USER = 'emonpi'
MQTT_PASS = 'emonpimqtt2016'
MQTT_TOPIC = 'HomeAssistant'
ardPort="/dev/ttyUSB0"
ser = serial.Serial()
ser.baudrate = 9600
ser.port = ardPort
ser.timeout = 1
ser.open()
print ("Using serial port: ")
print(ser.name)
def find(l, elem):
for row, i in enumerate(l):
try:
column = i.index(elem)
except ValueError:
continue
#eturn row, column
return row
return -1
def on_message(client, userdata, message):
try:
#print "Mqtt message arrived"
messrec = str(message.payload.decode("utf-8"))
#print("message received " ,str(message.payload.decode("utf-8")))
#print("message topic=",message.topic)
#print("message qos=",message.qos)
#print("message retain flag=",message.retain)
jsonstr = json.loads(messrec)
#print jsonstr
if (jsonstr['event_type']) == "call_service":
if (jsonstr['event_data']['domain']) == "homeassistant":
#print (messrec)
mqttrec=(jsonstr['event_data']['service_data']['entity_id'])
service = (jsonstr['event_data']['service'])
print ("Received ")
print (service)
#print (" " + mqttrec)
for x in range(len(mqttrec)):
print mqttrec[x],
print (" from Home Assistant")
#print (jsonstr['event_data']['domain'])
# Find item in list
match = service + "_" + mqttrec
#print match
codeindex = (find(codes, match))
# Is this working? Try with an nknown switch
codetosend = codes[codeindex][1]
#print codes[codeindex][1]
print "Codetosend: " + str(codetosend)
ser.write(str(codetosend))
except Exception as e:
logging.error(traceback.format_exc())
def main():
mqtt_client = mqtt.Client()
mqtt_client.username_pw_set(MQTT_USER, MQTT_PASS)
mqtt_client.connect(MQTT_HOST, MQTT_PORT)
mqtt_client.subscribe(MQTT_TOPIC)
mqtt_client.on_message=on_message
print ("Mqtt setup done")
while 1:
rcv = ser.readline()
#print rcv
if (len(str(rcv))>0):
#print "Got serial data"
# print rcv
recstring = rcv.split()
try:
print ("Received rf code: " + recstring[1])
except:
print "Index error"
ser.reset_input_buffer()
mqtt_client.loop()
mqtt_client.loop_stop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment