Wow Petro, I am speechless that you cooked up the python script for me! Tbh, I am not familiar with python. I just âcopiedâ some codes from this forum and changed it to suit my needs.
The basic functionalities are there in your script but I need the entity_id to be dynamic: I have 5 group of lights and 2 dimmers. Each dimmer has 4 buttons (Aeotec quad wallmote) that dims up/down each of its assigned entities. 1st dimmer: light.living_room, light.dining_lights, light.corner_light, light.stairs_light and 2nd dimmer: light.bedroom. Button 1 - 4 is assigned to the first 4 pairs and the 1st button in the 2nd dimmer is assigned to light.bedroom.
Here is the python code to detect the swipe up/down actions:
#!/usr/bin/env python3
import socket
import requests
from subprocess import Popen, PIPE
import json
# Match Rule
# Match the start of swipe
MATCHER1 = 'Received Configuration report: Parameter=9'
# Match the end of swipe
MATCHER2 = 'Received Configuration report: Parameter=10'
# Match which button is used on the wallmote quad
MATCHER3 = 'Decrypted Packet: 0x00, 0x70, 0x06, 0x09, 0x04'
# HA information
TOKEN = 'secret'
#TOKEN = False
OZW_LOG = '/home/homeassistant/.homeassistant/OZW_Log.txt'
debug = True
# Open the tail of our OZW_Log and scan for new lines;
log = Popen(('/usr/bin/tail', '-F', '-n', '0', OZW_LOG), stdout=PIPE)
while True:
# Get most recent line and massage it;
line = log.stdout.readline()
if not line:
break
line = line.strip().decode('utf-8')
# Fast match
if MATCHER1 not in line:
if MATCHER2 not in line:
if MATCHER3 not in line:
continue
if "Parameter=9" in line:
parameter = line.split(',')[-2].replace('Received Configuration report: ','').strip()
parameter9 = parameter.split('=')[1].strip()
nodeID = line.split(',')[1].strip().lower()
if "node002" in nodeID:
node = "light.living_room"
if "node003" in nodeID:
node = "light.bedroom"
value9 = line.split('=')[-1].strip()
value9 = int(value9)
continue
elif "Parameter=10" in line:
parameter = line.split(',')[-2].replace('Received Configuration report: ','').strip()
parameter10 = parameter.split('=')[1].strip()
#parameter10 = parameter
value10 = line.split('=')[-1].strip()
value10 = int(value10)
elif "Decrypted Packet:" in line:
button = line.split(',')[-4].replace(' 0x0','').strip()
continue
if parameter9 and parameter10 is not None:
if value9 < value10:
action = 'swipe_up'
else:
action = 'swipe_down'
if node is not None:
if "light.living_room" in node:
if "1" in button:
light_id = "light.living_room_light"
if "2" in button:
light_id = "light.dining_lights"
if "3" in button:
light_id = "light.corner_light"
if "4" in button:
light_id = "light.stairs_light"
elif "light.bedroom" in node:
if "1" in button:
light_id = 'light.bedroom_light'
event = 'dimmer_{0}'.format(action)
URI = URI = 'http://192.xxx.xxx.xxx:8123/api/services/script/{0}'.format(event)
if debug:
print(URI)
if debug:
print(event)
if event:
#data = {"entity_id": "{0}".format(event), "variables":{"light_id":"{0}".format(light_id)}}
data = {"data":{"light_id":"{0}".format(light_id)}}
if debug:
print(data)
if TOKEN:
resp = requests.post(URI, data=json.dumps(data), headers={'Authorization': 'Bearer {0}'.format(TOKEN), 'content-type': 'application/json'})
else:
resp = requests.post(URI, data=json.dumps(data), headers={'content-type': 'application/json'})
if debug:
print(resp)
The codes are redundant I know. Your solution is much more elegant but since I am noob in python, I will need some time to understand it to extend your code to use in my situation. Perhaps this is a good starting point for me:
https://www.home-assistant.io/cookbook/python_component_simple_alarm/
I supposed I can use a python script as custom_component?
I really appreciate your help in cooking up the python script!!