Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import homeassistant.remote as remote
- import socket
- api = remote.API('https://ADDRESS.duckdns.org', 'PASSWORD', 443) # ('HA Address', 'HA Password', HA Port)
- print(remote.validate_api(api))
- HOST = ""
- PORT = 4999 # Listening Port
- # Retrieve input from RTI
- # RTI 2-Way driver comand srting syntax examples:
- # ,media_player*select_source*entity_id*media_player.living_room_tv*source*Netflix^
- # ,vacuum*turn_on*entity_id*vacuum.roomba**^
- def msg_to_hass(strCommand):
- domain = strCommand.split("*")[0] # HA Domain
- service = strCommand.split("*")[1] # HA Service
- field = strCommand.split("*")[2] # HA Field1
- device = strCommand.split("*")[3] # HA Field1 Value
- field2 = strCommand.split("*")[4] # HA Field2
- device2 = strCommand.split("*")[5] # HA Field2 Value
- data = {} # initialize dictionary
- data[field] = device # insert field 1 and value into dictionary
- data[field2] = device2 # insert field 2 and value into dictionary
- new_data={k:data[k] for k in data if data[k]} # Remove blank entries from data dictionary and save as new_data
- print(domain, service, new_data) # print domain, service, and data to send to HA
- remote.call_service(api, domain, service, new_data) # send command to HA
- state = remote.get_state(api, device) # request device state from HA
- print(state) # Print state of HA Device
- def create_listen_socket(host, port):
- """ Setup the sockets our server will receive connection
- requests on """
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- sock.bind((host, port))
- sock.listen(100)
- return sock
- def recv_msg(sock):
- """ Wait for data to arrive on the socket, then parse into
- messages using b'\0' as message delimiter """
- data = bytearray()
- msg = ''
- # Repeatedly read 4096 bytes off the socket, storing the bytes
- # in data until we see a delimiter
- while not msg:
- recvd = sock.recv(4096)
- if not recvd:
- # Socket has been closed prematurely
- raise ConnectionError()
- data = data + recvd
- #if b'\0' in recvd:
- if b'^' in recvd: # ^ is the last character in the message. Strip the starting comma.
- #msg = data.rstrip(b'\0')
- msg = data
- msg = msg.decode('utf-8')
- msg = msg.split(",")[-1]
- return msg.rstrip("^")
- def prep_msg(msg):
- """ Prepare a string to be sent as a message """
- msg += '\0'
- return msg.encode('utf-8')
- def send_msg(sock, msg):
- """ Send a string over a socket, preparing it first """
- data = prep_msg(msg)
- sock.sendall(data)
- def handle_client(sock, addr):
- """ Receive data from the client via sock and echo it back """
- try:
- msg = recv_msg(sock) # Blocks until received complete message
- print('{}: {}'.format(addr, msg))
- #send_msg(sock, msg) # Blocks until sent
- msg_to_hass(msg)
- except (ConnectionError, BrokenPipeError):
- print('Socket error')
- finally:
- handle_client(client_sock, addr) # loop back to recv_msg
- # print('Closed connection to {}'.format(addr))
- # sock.close()
- if __name__ == '__main__':
- listen_sock = create_listen_socket(HOST, PORT)
- addr = listen_sock.getsockname()
- print('Listening on {}'.format(addr))
- while True:
- client_sock, addr = listen_sock.accept()
- print('Connection from {}'.format(addr))
- handle_client(client_sock, addr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement