-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevdev2hass.py
83 lines (72 loc) · 2.29 KB
/
evdev2hass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#! /usr/bin/python3
# capture evdev events and send to hass API as events
# reference:
# https://www.home-assistant.io/integrations/keyboard_remote/
# https://developers.home-assistant.io/docs/api/rest/
# https://docs.python-requests.org/en/master/
import evdev
import argparse
import os
import sys
from requests import post
valueToAction = ['release', 'press', 'hold']
def loop(identity, device, endpoint, token):
dev = evdev.InputDevice(device)
dev.grab()
print("Grabbing", dev)
# print(dev.capabilities(verbose=True))
for event in dev.read_loop():
# interesting properties: type (key, etc), code (scan code), value (up, down, hold)
if event.type == evdev.ecodes.EV_KEY:
key = evdev.ecodes.KEY[event.code]
# some reverse mapping can point to more than one entry
if type(key) is list:
key = key[0]
# debug
print(event.type, event.code, event.value, key)
# call API
payload = {
# https://stackoverflow.com/questions/4271740/how-can-i-use-python-to-get-the-system-hostname
'identity': identity,
'device': device,
# 'eventType': event.type,
'eventTimeSec': event.sec,
'eventCode': event.code,
'eventValue': event.value,
'action': valueToAction[event.value],
'key': key
}
url = endpoint + "events/evdev"
headers = {
"Authorization": "Bearer " + token,
"content-type": "application/json",
}
try:
response = post(url, headers=headers, json=payload)
print(response.status_code, response.text)
response.raise_for_status()
except:
e = sys.exc_info()[0]
print(e)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='evdev2mqtt -- Evdev to MQTT Gateway')
parser.add_argument("-e", "--endpoint",
dest="endpoint",
default="http://hassio.lan:8123/api/",
help="HASS API end-point")
parser.add_argument("-t", "--token",
dest="token",
default="",
required=True,
help="HASS API authentication token")
parser.add_argument("-d", "--device",
dest="device",
default="/dev/input/event1",
help="Path to the evdev device")
parser.add_argument("-i", "--identity",
dest="identity",
default=os.uname()[1],
help="Idnetity of this script/container")
args = parser.parse_args()
loop(args.identity, args.device, args.endpoint, args.token)