daily pastebin goal
75%
SHARE
TWEET

Untitled

a guest Jun 29th, 2018 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. '''
  3. Read Mqtt messages from Home Assistant and send them via RF.
  4. Reads codes with a 433MHz receiver and prints them.
  5. This code is connected to a Mqtt message in the table below, and is sent when that message arrives.
  6.  
  7. Toggle the switch in Home Assistant and the script shows a code.
  8.  
  9. Received rf code:
  10. 20
  11.  
  12.  
  13.  
  14. '''
  15.  
  16. codes = [
  17.     ["turn_on_light.p_arum_window", "21"],
  18.     ["turn_off_light.p_arum_window", 20]
  19. ]
  20.  
  21.  
  22. import serial, json, time
  23. import paho.mqtt.client as mqtt
  24. import traceback
  25. import logging
  26.  
  27. MQTT_HOST = '192.168.1.79'
  28. MQTT_PORT = 1883
  29. MQTT_USER = 'emonpi'
  30. MQTT_PASS = 'emonpimqtt2016'
  31. MQTT_TOPIC = 'HomeAssistant'
  32.  
  33. ardPort="/dev/ttyUSB0"
  34.  
  35. ser = serial.Serial()
  36. ser.baudrate = 9600
  37. ser.port = ardPort
  38. ser.timeout = 1
  39. ser.open()
  40. print ("Using serial port: ")
  41. print(ser.name)
  42.  
  43. def find(l, elem):
  44.     for row, i in enumerate(l):
  45.         try:
  46.             column = i.index(elem)
  47.         except ValueError:
  48.             continue
  49.         #eturn row, column
  50.     return row
  51.     return -1
  52.  
  53. def on_message(client, userdata, message):
  54.     try:
  55.         #print "Mqtt message arrived"
  56.         messrec = str(message.payload.decode("utf-8"))
  57.         #print("message received " ,str(message.payload.decode("utf-8")))
  58.         #print("message topic=",message.topic)
  59.         #print("message qos=",message.qos)
  60.         #print("message retain flag=",message.retain)
  61.         jsonstr = json.loads(messrec)
  62.         #print jsonstr
  63.         if (jsonstr['event_type']) == "call_service":
  64.             if (jsonstr['event_data']['domain']) == "homeassistant":
  65.                 #print (messrec)
  66.                 mqttrec=(jsonstr['event_data']['service_data']['entity_id'])
  67.                 service = (jsonstr['event_data']['service'])
  68.                 print ("Received ")
  69.                     print (service)
  70.                     #print (" " + mqttrec)
  71.  
  72.                     for x in range(len(mqttrec)):
  73.                         print mqttrec[x],
  74.  
  75.                                 print (" from Home Assistant")
  76.                 #print (jsonstr['event_data']['domain'])
  77.  
  78.                 # Find item in list
  79.                 match = service + "_" + mqttrec
  80.                 #print match
  81.  
  82.                 codeindex =  (find(codes, match))
  83.  
  84.                 # Is this working? Try with an nknown switch
  85.  
  86.                 codetosend = codes[codeindex][1]
  87.                 #print codes[codeindex][1]
  88.                 print "Codetosend: " + str(codetosend)
  89.                 ser.write(str(codetosend))
  90.  
  91.     except Exception as e:
  92.         logging.error(traceback.format_exc())
  93. def main():
  94.  
  95.  
  96.     mqtt_client = mqtt.Client()
  97.     mqtt_client.username_pw_set(MQTT_USER, MQTT_PASS)
  98.     mqtt_client.connect(MQTT_HOST, MQTT_PORT)
  99.     mqtt_client.subscribe(MQTT_TOPIC)
  100.     mqtt_client.on_message=on_message
  101.  
  102.     print ("Mqtt setup done")
  103.  
  104.     while 1:
  105.         rcv = ser.readline()
  106.         #print rcv
  107.         if (len(str(rcv))>0):
  108.             #print "Got serial data"
  109.         #   print rcv
  110.             recstring = rcv.split()
  111.             try:
  112.                 print ("Received rf code: " + recstring[1])
  113.             except:
  114.                 print "Index error"
  115.             ser.reset_input_buffer()
  116.         mqtt_client.loop()
  117.     mqtt_client.loop_stop()
  118. if __name__ == '__main__':
  119.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top