-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path__init__.py
527 lines (469 loc) · 19.1 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
"""Support for OPNsense."""
from collections.abc import MutableMapping
from datetime import timedelta
import logging
from typing import Any
import aiohttp
import awesomeversion
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
CONF_URL,
CONF_USERNAME,
CONF_VERIFY_SSL,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
entity_registry as er,
issue_registry as ir,
)
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.typing import ConfigType
from .const import (
CONF_DEVICE_TRACKER_ENABLED,
CONF_DEVICE_TRACKER_SCAN_INTERVAL,
CONF_DEVICE_UNIQUE_ID,
CONF_TLS_INSECURE,
COORDINATOR,
DEFAULT_DEVICE_TRACKER_ENABLED,
DEFAULT_DEVICE_TRACKER_SCAN_INTERVAL,
DEFAULT_SCAN_INTERVAL,
DEFAULT_TLS_INSECURE,
DEFAULT_VERIFY_SSL,
DEVICE_TRACKER_COORDINATOR,
DOMAIN,
LOADED_PLATFORMS,
OPNSENSE_CLIENT,
OPNSENSE_LTD_FIRMWARE,
OPNSENSE_MIN_FIRMWARE,
PLATFORMS,
SHOULD_RELOAD,
UNDO_UPDATE_LISTENER,
VERSION,
)
from .coordinator import OPNsenseDataUpdateCoordinator
from .helpers import is_private_ip
from .pyopnsense import OPNsenseClient
from .services import async_setup_services
_LOGGER: logging.Logger = logging.getLogger(__name__)
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
if hass.data[DOMAIN][entry.entry_id].get(SHOULD_RELOAD, True):
hass.async_create_task(hass.config_entries.async_reload(entry.entry_id))
else:
hass.data[DOMAIN][entry.entry_id][SHOULD_RELOAD] = True
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Call the method to setup the integration-level services."""
await async_setup_services(hass)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up OPNsense from a config entry."""
config = entry.data
options = entry.options
url: str = config[CONF_URL]
username: str = config[CONF_USERNAME]
password: str = config[CONF_PASSWORD]
verify_ssl: bool = config.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL)
device_tracker_enabled: bool = options.get(
CONF_DEVICE_TRACKER_ENABLED, DEFAULT_DEVICE_TRACKER_ENABLED
)
config_device_id: str = config[CONF_DEVICE_UNIQUE_ID]
client = OPNsenseClient(
url=url,
username=username,
password=password,
session=async_create_clientsession(
hass=hass,
raise_for_status=False,
cookie_jar=aiohttp.CookieJar(unsafe=is_private_ip(url)),
),
opts={"verify_ssl": verify_ssl},
name=entry.title,
)
scan_interval: int = options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
_LOGGER.info("Starting hass-opnsense %s", VERSION)
coordinator = OPNsenseDataUpdateCoordinator(
hass=hass,
name=f"{entry.title} state",
update_interval=timedelta(seconds=scan_interval),
client=client,
device_unique_id=config_device_id,
)
# Trigger repair task and shutdown if device id has changed
router_device_id: str = await client.get_device_unique_id()
_LOGGER.debug(
"[init async_setup_entry]: config device id: %s, router device id: %s",
config_device_id,
router_device_id,
)
if router_device_id != config_device_id and router_device_id:
ir.async_create_issue(
hass=hass,
domain=DOMAIN,
issue_id=f"{config_device_id}_device_id_mismatched",
is_fixable=False,
is_persistent=False,
severity=ir.IssueSeverity.ERROR,
translation_key="device_id_mismatched",
)
_LOGGER.error(
"OPNsense Device ID has changed which indicates new or changed hardware. "
"In order to accomodate this, hass-opnsense needs to be removed and reinstalled for this router. "
"hass-opnsense is shutting down."
)
await coordinator.async_shutdown()
return False
firmware: str | None = await client.get_host_firmware_version()
_LOGGER.info("OPNsense Firmware %s", firmware)
try:
if awesomeversion.AwesomeVersion(firmware) < awesomeversion.AwesomeVersion(
OPNSENSE_MIN_FIRMWARE
):
ir.async_create_issue(
hass,
DOMAIN,
f"opnsense_{firmware}_below_min_firmware_{OPNSENSE_MIN_FIRMWARE}",
is_fixable=False,
is_persistent=False,
issue_domain=DOMAIN,
severity=ir.IssueSeverity.ERROR,
translation_key="below_min_firmware",
translation_placeholders={
"version": str(VERSION),
"min_firmware": str(OPNSENSE_MIN_FIRMWARE),
"firmware": firmware or "Unknown",
},
)
await coordinator.async_shutdown()
return False
if awesomeversion.AwesomeVersion(firmware) < awesomeversion.AwesomeVersion(
OPNSENSE_LTD_FIRMWARE
):
ir.async_create_issue(
hass,
DOMAIN,
f"opnsense_{firmware}_below_ltd_firmware_{OPNSENSE_LTD_FIRMWARE}",
is_fixable=False,
is_persistent=False,
issue_domain=DOMAIN,
severity=ir.IssueSeverity.WARNING,
translation_key="below_ltd_firmware",
translation_placeholders={
"version": str(VERSION),
"ltd_firmware": str(OPNSENSE_LTD_FIRMWARE),
"firmware": firmware or "Unknown",
},
)
else:
ir.async_delete_issue(
hass,
DOMAIN,
f"opnsense_{firmware}_below_min_firmware_{OPNSENSE_MIN_FIRMWARE}",
)
ir.async_delete_issue(
hass,
DOMAIN,
f"opnsense_{firmware}_below_ltd_firmware_{OPNSENSE_LTD_FIRMWARE}",
)
except awesomeversion.exceptions.AwesomeVersionCompareException:
_LOGGER.warning("Unable to confirm OPNsense Firmware version")
await coordinator.async_config_entry_first_refresh()
platforms: list = PLATFORMS.copy()
device_tracker_coordinator = None
if not device_tracker_enabled and "device_tracker" in platforms:
platforms.remove("device_tracker")
else:
device_tracker_scan_interval = options.get(
CONF_DEVICE_TRACKER_SCAN_INTERVAL, DEFAULT_DEVICE_TRACKER_SCAN_INTERVAL
)
device_tracker_coordinator = OPNsenseDataUpdateCoordinator(
hass=hass,
name=f"{entry.title} Device Tracker state",
update_interval=timedelta(seconds=device_tracker_scan_interval),
client=client,
device_unique_id=config_device_id,
device_tracker_coordinator=True,
)
undo_listener = entry.add_update_listener(_async_update_listener)
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = {
COORDINATOR: coordinator,
DEVICE_TRACKER_COORDINATOR: device_tracker_coordinator,
OPNSENSE_CLIENT: client,
UNDO_UPDATE_LISTENER: [undo_listener],
LOADED_PLATFORMS: platforms,
CONF_DEVICE_UNIQUE_ID: config_device_id,
}
if device_tracker_enabled and device_tracker_coordinator:
# Fetch initial data so we have data when entities subscribe
await device_tracker_coordinator.async_config_entry_first_refresh()
await hass.config_entries.async_forward_entry_setups(entry, platforms)
return True
async def async_remove_config_entry_device(
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry
) -> bool:
"""Remove OPNsense Devices that aren't Device Tracker Devices and without any linked entities."""
if device_entry.via_device_id:
_LOGGER.error("Remove OPNsense Device Tracker Devices via the Integration Configuration")
return False
entity_registry = er.async_get(hass)
for ent in er.async_entries_for_config_entry(entity_registry, config_entry.entry_id):
if ent.device_id == device_entry.id:
_LOGGER.error("Cannot remove OPNsense Devices with linked entities at this time")
return False
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
platforms = hass.data[DOMAIN][entry.entry_id][LOADED_PLATFORMS]
unload_ok = await hass.config_entries.async_unload_platforms(entry, platforms)
for listener in hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]:
listener()
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
async def _migrate_1_to_2(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
tls_insecure = config_entry.data.get(CONF_TLS_INSECURE, DEFAULT_TLS_INSECURE)
data = dict(config_entry.data)
# remove tls_insecure
if CONF_TLS_INSECURE in data:
del data[CONF_TLS_INSECURE]
# add verify_ssl
if CONF_VERIFY_SSL not in data:
data[CONF_VERIFY_SSL] = not tls_insecure
hass.config_entries.async_update_entry(config_entry, data=data, version=2)
return True
async def _migrate_2_to_3(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug("[migrate_2_to_3] Initial Version: %s", config_entry.version)
entity_registry = er.async_get(hass)
device_registry = dr.async_get(hass)
config = config_entry.data
url: str = config[CONF_URL]
username: str = config[CONF_USERNAME]
password: str = config[CONF_PASSWORD]
verify_ssl: bool = config.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL)
client = OPNsenseClient(
url=url,
username=username,
password=password,
session=async_create_clientsession(
hass=hass,
raise_for_status=False,
cookie_jar=aiohttp.CookieJar(unsafe=is_private_ip(url)),
),
opts={"verify_ssl": verify_ssl},
)
new_device_unique_id: str | None = await client.get_device_unique_id()
if not new_device_unique_id:
_LOGGER.error("Missing Device Unique ID for Migration to Version 3")
return False
_LOGGER.debug("[migrate_2_to_3] new_device_unique_id: %s", new_device_unique_id)
for dev in dr.async_entries_for_config_entry(
device_registry, config_entry_id=config_entry.entry_id
):
_LOGGER.debug("[migrate_2_to_3] dev: %s", dev)
is_main_dev: bool = any(t[0] == "opnsense" for t in dev.identifiers)
if is_main_dev:
new_identifiers = {
(t[0], new_device_unique_id) if t[0] == "opnsense" else t for t in dev.identifiers
}
_LOGGER.debug(
"[migrate_2_to_3] dev.identifiers: %s, new_identifiers: %s",
dev.identifiers,
new_identifiers,
)
try:
new_dev = device_registry.async_update_device(
dev.id, new_identifiers=new_identifiers
)
_LOGGER.debug("[migrate_2_to_3] new_main_dev: %s", new_dev)
except dr.DeviceIdentifierCollisionError as e:
_LOGGER.error(
"Error migrating device: %s. %s: %s",
dev.identifiers,
e.__class__.__qualname__,
e,
)
for ent in er.async_entries_for_config_entry(entity_registry, config_entry.entry_id):
# _LOGGER.debug(f"[migrate_2_to_3] ent: {ent}")
platform = ent.entity_id.split(".")[0]
try:
_, unique_id_suffix = ent.unique_id.split("_", 1)
except ValueError:
unique_id_suffix = f"mac_{ent.unique_id}"
new_unique_id: str = (
(f"{new_device_unique_id}_{unique_id_suffix}").replace(":", "_").strip()
)
_LOGGER.debug(
"[migrate_2_to_3] ent: %s, platform: %s, unique_id: %s, new_unique_id: %s",
ent.entity_id,
platform,
ent.unique_id,
new_unique_id,
)
try:
new_ent = entity_registry.async_update_entity(
ent.entity_id, new_unique_id=new_unique_id
)
_LOGGER.debug(
"[migrate_2_to_3] new_ent: %s, unique_id: %s",
new_ent.entity_id,
new_ent.unique_id,
)
except ValueError as e:
_LOGGER.error(
"Error migrating entity: %s. %s: %s",
ent.entity_id,
e.__class__.__qualname__,
e,
)
new_data: MutableMapping[str, Any] = dict(config_entry.data)
new_data.update({CONF_DEVICE_UNIQUE_ID: new_device_unique_id})
_LOGGER.debug(
"[migrate_2_to_3] data: %s, new_data: %s, unique_id: %s, new_unique_id: %s",
config_entry.data,
new_data,
config_entry.unique_id,
new_device_unique_id,
)
new_entry_bool = hass.config_entries.async_update_entry(
config_entry, data=new_data, unique_id=new_device_unique_id, version=3
)
if new_entry_bool:
_LOGGER.debug("[migrate_2_to_3] config_entry update sucessful")
else:
_LOGGER.error("Migration of config_entry to version 3 unsucessful")
return False
return True
async def _migrate_3_to_4(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug("[migrate_3_to_4] Initial Version: %s", config_entry.version)
entity_registry = er.async_get(hass)
config = config_entry.data
url: str = config[CONF_URL]
username: str = config[CONF_USERNAME]
password: str = config[CONF_PASSWORD]
verify_ssl: bool = config.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL)
client = OPNsenseClient(
url=url,
username=username,
password=password,
session=async_create_clientsession(
hass=hass,
raise_for_status=False,
cookie_jar=aiohttp.CookieJar(unsafe=is_private_ip(url)),
),
opts={"verify_ssl": verify_ssl},
)
telemetry = await client.get_telemetry()
for ent in er.async_entries_for_config_entry(entity_registry, config_entry.entry_id):
platform = ent.entity_id.split(".")[0]
if platform == Platform.SENSOR:
# _LOGGER.debug(f"[migrate_3_to_4] ent: {ent}")
if "_telemetry_interface_" in ent.unique_id:
new_unique_id: str | None = ent.unique_id.replace(
"_telemetry_interface_", "_interface_"
)
elif "_telemetry_gateway_" in ent.unique_id:
new_unique_id = ent.unique_id.replace("_telemetry_gateway_", "_gateway_")
elif "_connected_client_count" in ent.unique_id:
try:
entity_registry.async_remove(ent.entity_id)
_LOGGER.debug("[migrate_3_to_4] removed_entity_id: %s", ent.entity_id)
except (KeyError, ValueError) as e:
_LOGGER.error(
"Error removing entity: %s. %s: %s",
ent.entity_id,
e.__class__.__qualname__,
e,
)
continue
elif "_telemetry_openvpn_" in ent.unique_id:
new_unique_id = ent.unique_id.replace("_telemetry_openvpn_", "_openvpn_")
elif "_telemetry_filesystems_" in ent.unique_id:
new_unique_id = None
for filesystem in telemetry.get("filesystems", []):
device_name: str = (
filesystem.get("device", "").replace("/", "_slash_").strip("_")
).lower()
unique_id_device_name: str = (
ent.unique_id.split("_telemetry_filesystems_")[1]
).lower()
if device_name == unique_id_device_name:
mpoint: str = filesystem.get("mountpoint", "")
if mpoint == "/":
mountpoint = "root"
else:
mountpoint = mpoint.replace("/", "_").strip("_")
new_unique_id = ent.unique_id.replace(device_name, mountpoint)
break
if not new_unique_id or ent.unique_id == new_unique_id:
continue
else:
continue
_LOGGER.debug(
"[migrate_3_to_4] ent: %s, platform: %s, unique_id: %s, new_unique_id: %s",
ent.entity_id,
platform,
ent.unique_id,
new_unique_id,
)
if not new_unique_id:
_LOGGER.error("Error migrating entity: %s", ent.entity_id)
continue
try:
updated_ent = entity_registry.async_update_entity(
ent.entity_id, new_unique_id=new_unique_id
)
_LOGGER.debug(
"[migrate_3_to_4] updated_entity_id: %s, updated_unique_id: %s",
updated_ent.entity_id,
updated_ent.unique_id,
)
except ValueError as e:
_LOGGER.error(
"Error migrating entity: %s. %s: %s",
ent.entity_id,
e.__class__.__qualname__,
e,
)
new_entry_bool = hass.config_entries.async_update_entry(config_entry, version=4)
if new_entry_bool:
_LOGGER.debug("[migrate_3_to_4] config_entry update sucessful")
else:
_LOGGER.error("Migration of config_entry to version 4 unsucessful")
return False
return True
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Migrate an old config entry."""
version = config_entry.version
if version > 4:
# This means the user has downgraded from a future version
_LOGGER.error(
"hass-opnsense downgraded and current config not compatible with earlier versions. Integration mut be removed and reinstalled."
)
return False
_LOGGER.debug("Migrating from version %s", version)
# 1 -> 2: tls_insecure to verify_ssl
if version == 1:
v1to2: bool = await _migrate_1_to_2(hass, config_entry)
if not v1to2:
return False
version = 2
# 2 -> 3: Change unique device id to use lowest MAC address
if version == 2:
v2to3: bool = await _migrate_2_to_3(hass, config_entry)
if not v2to3:
return False
version = 3
# 3 -> 4: Moving interfaces, gateways and openvpn out of telemetry
if version == 3:
v3to4: bool = await _migrate_3_to_4(hass, config_entry)
if not v3to4:
return False
version = 4
_LOGGER.info("Migration to version %s successful", version)
return True