forked from ThanhCN0/openrgb_ha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
235 lines (198 loc) · 7.22 KB
/
__init__.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""The OpenRGB integration."""
import asyncio
import logging
from openrgb import OpenRGBClient
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import CONF_CLIENT_ID, CONF_HOST, CONF_PORT
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from .const import (
DEFAULT_CLIENT_ID,
DEFAULT_PORT,
DOMAIN,
ENTRY_IS_SETUP,
ORGB_DATA,
ORGB_DISCOVERY_NEW,
ORGB_TRACKER,
SERVICE_FORCE_UPDATE,
SERVICE_PULL_DEVICES,
SIGNAL_DELETE_ENTITY,
SIGNAL_UPDATE_ENTITY,
TRACK_INTERVAL,
)
from .helpers import orgb_entity_id
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema(
vol.All(
cv.deprecated(DOMAIN),
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_CLIENT_ID, default=DEFAULT_CLIENT_ID): cv.string,
}
)
},
),
extra=vol.ALLOW_EXTRA,
)
def autolog(message):
"Automatically log the current function details."
import inspect
# Get the previous frame in the stack, otherwise it would
# be this function!!!
func = inspect.currentframe().f_back.f_code
# Dump the message + the name of this function to the log.
_LOGGER.debug("%s: %s in %s:%i" % (
message,
func.co_name,
func.co_filename,
func.co_firstlineno
))
async def async_setup(hass, config):
"""Set up the OpenRGB integration."""
conf = config.get(DOMAIN)
if conf is not None:
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
)
)
return True
async def async_setup_entry(hass, entry):
"""Set up OpenRGB platform."""
autolog("<<<")
try:
orgb = OpenRGBClient(
entry.data[CONF_HOST],
entry.data[CONF_PORT],
name=entry.data[CONF_CLIENT_ID],
)
except ConnectionError as err:
_LOGGER.error("Connection error during integration setup. Error: %s", err)
autolog(">>>")
def connection_recovered():
autolog("<<<")
if not hass.data[DOMAIN]["online"]:
_LOGGER.info(
"Connection reestablished to OpenRGB SDK Server at %s:%i",
entry.data[CONF_HOST],
entry.data[CONF_PORT],
)
hass.data[DOMAIN]["online"] = True
async_dispatcher_send(hass, SIGNAL_UPDATE_ENTITY)
autolog(">>>")
def connection_failed():
autolog("<<<")
if hass.data[DOMAIN]["online"]:
hass.data[DOMAIN][ORGB_DATA].comms.stop_connection()
_LOGGER.info(
"Connection lost to OpenRGB SDK Server at %s:%i",
entry.data[CONF_HOST],
entry.data[CONF_PORT],
)
hass.data[DOMAIN]["online"] = False
async_dispatcher_send(hass, SIGNAL_UPDATE_ENTITY)
autolog(">>>")
hass.data[DOMAIN] = {
"online": True,
ORGB_DATA: orgb,
ORGB_TRACKER: None,
ENTRY_IS_SETUP: set(),
"entities": {},
"pending": {},
"connection_failed": connection_failed,
"connection_recovered": connection_recovered,
}
# Initial device load
async def async_load_devices(device_list):
device_type_list = {}
for device in device_list:
ha_type = "light"
if ha_type not in device_type_list:
device_type_list[ha_type] = []
device_type_list[ha_type].append(device)
entity_id = orgb_entity_id(device)
if entity_id not in hass.data[DOMAIN]["entities"]:
hass.data[DOMAIN]["entities"][entity_id] = None
for ha_type, dev_ids in device_type_list.items():
config_entries_key = f"{ha_type}.openrgb"
if config_entries_key not in hass.data[DOMAIN][ENTRY_IS_SETUP]:
hass.data[DOMAIN]["pending"][ha_type] = dev_ids
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "light")
)
hass.data[DOMAIN][ENTRY_IS_SETUP].add(config_entries_key)
else:
async_dispatcher_send(
hass, ORGB_DISCOVERY_NEW.format("light"), device_list
)
def _get_updated_devices():
autolog("<<<")
try:
orgb.update()
except ConnectionError:
autolog(">>>exception")
hass.data[DOMAIN]["connection_failed"]()
return None
autolog(">>>")
return orgb.devices
await async_load_devices(_get_updated_devices())
async def async_poll_devices_update(event_time):
if not hass.data[DOMAIN]["online"]:
# try to reconnect
try:
hass.data[DOMAIN][ORGB_DATA].comms.start_connection()
hass.data[DOMAIN]["connection_recovered"]()
except ConnectionError:
hass.data[DOMAIN]["connection_failed"]()
device_list = await hass.async_add_executor_job(_get_updated_devices)
if device_list is None:
return
await async_load_devices(device_list)
newlist_ids = []
for device in device_list:
newlist_ids.append(orgb_entity_id(device))
for dev_id in list(hass.data[DOMAIN]["entities"]):
# Clean up stale devices, or alert them that new info is available.
if dev_id not in newlist_ids:
async_dispatcher_send(hass, SIGNAL_DELETE_ENTITY, dev_id)
hass.data[DOMAIN]["entities"].pop(dev_id)
else:
async_dispatcher_send(hass, SIGNAL_UPDATE_ENTITY, dev_id)
hass.data[DOMAIN][ORGB_TRACKER] = async_track_time_interval(
hass, async_poll_devices_update, TRACK_INTERVAL
)
hass.services.async_register(
DOMAIN, SERVICE_PULL_DEVICES, async_poll_devices_update
)
async def async_force_update(call):
"""Force all devices to pull data."""
async_dispatcher_send(hass, SIGNAL_UPDATE_ENTITY)
hass.services.async_register(DOMAIN, SERVICE_FORCE_UPDATE, async_force_update)
return True
async def async_unload_entry(hass, entry):
"""Unloading the OpenRGB platforms."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(
entry, component.split(".", 1)[0]
)
for component in hass.data[DOMAIN][ENTRY_IS_SETUP]
]
)
)
if unload_ok:
hass.data[DOMAIN][ENTRY_IS_SETUP] = set()
hass.data[DOMAIN][ORGB_TRACKER]()
hass.data[DOMAIN][ORGB_TRACKER] = None
hass.data[DOMAIN][ORGB_DATA].comms.stop_connection()
hass.data[DOMAIN][ORGB_DATA] = None
hass.services.async_remove(DOMAIN, SERVICE_FORCE_UPDATE)
hass.services.async_remove(DOMAIN, SERVICE_PULL_DEVICES)
hass.data.pop(DOMAIN)
return unload_ok