This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
153 lines (132 loc) · 6.3 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import random
import requests
import json
import configparser
import glob
import sys
import os
import logging
from pathlib import Path
from winservice import SMWinservice
from time import sleep
from msvcrt import kbhit
import pywinusb.hid as hid
class PythonCornerExample(SMWinservice):
_svc_name_ = "JabraHomeAssistantSync"
_svc_display_name_ = "Jabra Home Assistant Sync"
_svc_description_ = "This service syncs the Jabra hook status to Home Assistant"
def start(self):
self.isrunning = True
def stop(self):
self.isrunning = False
def main(self):
while self.isrunning:
# Browse for non system HID class devices, if a telephony page
# hook usage control is available monitor value change events
# play with this value (or set it if you know your device capabilities)
# this allows to poll the telephony device for the current usage value
input_interrupt_transfers = False
# get all currently connected HID devices we could filter by doing
# something like hid.HidDeviceFilter(vendor_id = 0x1234), product Id
# filters (with masks) and other capabilities also available
all_devices = hid.HidDeviceFilter(
vendor_id=0x0B0E
).get_devices() # Vendor_id 0x0b0e = Jabra
if not all_devices:
# add a delay of 0.5 seconds for resource utilization
sleep(0.5)
logging.debug("No HID class devices attached.")
else:
# search for our target usage (the hook button)
# target pageId, usageId
usage_telephony_hook = hid.get_full_usage_id(0xB, 0x20)
def hook_pressed(new_value, event_type):
"simple usage control handler"
# this simple handler is called on 'pressed' events
# this means the usage value has changed from '1' to '0'
# no need to check the value
event_type = event_type # avoid pylint warnings
# import the config file
config = configparser.ConfigParser()
config.read(os.path.join(sys.path[0], "secrets.ini"))
if new_value:
logging.info("On Hook!")
response = requests.post(
config.get("DEFAULT", "HomeAssistantURL"),
headers={
"Authorization": config.get(
"DEFAULT", "HomeAssistantAuth"
),
"content-type": "application/json",
},
data=json.dumps(
{
"state": "on",
"attributes": {
"friendly_name": "Jabra Headset",
"icon": "mdi:headset",
},
}
),
)
logging.info(response.text)
else:
logging.info("Off Hook!")
response = requests.post(
config.get("DEFAULT", "HomeAssistantURL"),
headers={
"Authorization": config.get(
"DEFAULT", "HomeAssistantAuth"
),
"content-type": "application/json",
},
data=json.dumps(
{
"state": "off",
"attributes": {
"friendly_name": "Jabra Headset",
"icon": "mdi:headset",
},
}
),
)
logging.info(response.text)
for device in all_devices:
try:
device.open()
# browse input reports
all_input_reports = device.find_input_reports()
for input_report in all_input_reports:
if usage_telephony_hook in input_report:
# found a telephony device w/ hook button
logging.info(
"\nMonitoring {0.vendor_name} {0.product_name} "
"device.\n".format(device)
)
# add event handler (example of other available
# events: EVT_PRESSED, EVT_RELEASED, EVT_ALL, ...)
device.add_event_handler(
usage_telephony_hook,
hook_pressed,
hid.HID_EVT_CHANGED,
) # level usage
if input_interrupt_transfers:
# poll the current value (GET_REPORT directive),
# allow handler to process result
input_report.get()
while (
not kbhit()
and device.is_plugged()
and self.isrunning
):
# just keep the device opened to receive events
sleep(0.5)
# return - Uncomment if you want the service to stop
finally:
device.close()
logging.info(
"Sorry, no one of the attached HID class devices "
"provide any Telephony Hook button"
)
if __name__ == "__main__":
PythonCornerExample.parse_command_line()