Skip to content

Instantly share code, notes, and snippets.

@andrepcg
Created August 16, 2022 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrepcg/76a5fabb2f385c4a88eca8cfc1faafec to your computer and use it in GitHub Desktop.
Save andrepcg/76a5fabb2f385c4a88eca8cfc1faafec to your computer and use it in GitHub Desktop.
import socket
import struct
#Proxy script that receive CoAP message and forward them to Shelly HASS plugin.
#Use this if you have Shelly devices on other sub-net or VLAN
#Require ShellyForHASS 0.0.15 or later
#Change to the ip-address of your HASS server
HASS_IP = "192.168.1.XXX"
UDP_IP = "224.0.1.187"
UDP_PORT = 5683
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', UDP_PORT))
mreq = struct.pack("4sl", socket.inet_aton(UDP_IP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
while True:
try:
#Receive CoAP message
data, addr = sock.recvfrom(10240)
#Tag and add device ip-address to message
newdata = bytearray(b'prxy')
newdata.extend(socket.inet_aton(addr[0]))
newdata.extend(data)
print("Received data. Len: " + str(len(newdata)))
#Send to Shelly plugin
#sock.sendto(newdata, (HASS_IP, UDP_PORT))
except Exception as e:
print ('exception ' + str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment