Skip to content

Commit

Permalink
Show repair and shutdown if new device id detected
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffy2 committed Nov 25, 2024
1 parent a333d50 commit 7a8711c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
47 changes: 31 additions & 16 deletions custom_components/opnsense/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,10 @@
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import (
config_validation as cv,
)
from homeassistant.helpers import (
device_registry as dr,
)
from homeassistant.helpers import (
entity_registry as er,
)
from homeassistant.helpers import (
issue_registry as ir,
)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand Down Expand Up @@ -89,7 +81,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
device_tracker_enabled: bool = options.get(
CONF_DEVICE_TRACKER_ENABLED, DEFAULT_DEVICE_TRACKER_ENABLED
)
device_unique_id = config[CONF_DEVICE_UNIQUE_ID]
config_device_id: str = config[CONF_DEVICE_UNIQUE_ID]

client = OPNsenseClient(
url=url,
Expand All @@ -108,11 +100,34 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
name=f"{entry.title} state",
update_interval=timedelta(seconds=scan_interval),
client=client,
device_unique_id=device_unique_id,
device_unique_id=config_device_id,
)

await coordinator.async_config_entry_first_refresh()

# Trigger repair task and shutdown if device id has changed
router_device_id: str = coordinator.data.get("device_unique_id")
_LOGGER.debug(
f"[init async_setup_entry]: config device id: {config_device_id}, router 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 = coordinator.data.get("host_firmware_version", None)
_LOGGER.info(f"OPNsense Firmware {firmware}")
try:
Expand Down Expand Up @@ -183,7 +198,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
name=f"{entry.title} Device Tracker state",
update_interval=timedelta(seconds=device_tracker_scan_interval),
client=client,
device_unique_id=device_unique_id,
device_unique_id=config_device_id,
device_tracker_coordinator=True,
)

Expand All @@ -196,7 +211,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
OPNSENSE_CLIENT: client,
UNDO_UPDATE_LISTENER: [undo_listener],
LOADED_PLATFORMS: platforms,
CONF_DEVICE_UNIQUE_ID: device_unique_id,
CONF_DEVICE_UNIQUE_ID: config_device_id,
}

if device_tracker_enabled:
Expand Down
34 changes: 30 additions & 4 deletions custom_components/opnsense/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from typing import Any

from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .const import ATTR_UNBOUND_BLOCKLIST
from .const import ATTR_UNBOUND_BLOCKLIST, DOMAIN
from .helpers import dict_get
from .pyopnsense import OPNsenseClient

Expand All @@ -33,6 +34,7 @@ def __init__(
self._client: OPNsenseClient = client
self._state: Mapping[str, Any] = {}
self._device_tracker_coordinator: bool = device_tracker_coordinator
self._mismatched_count = 0
self._device_unique_id: str = device_unique_id
super().__init__(
hass,
Expand Down Expand Up @@ -146,11 +148,35 @@ async def _async_update_data(self) -> Mapping[str, Any]:
)
return {}
if self._state.get("device_unique_id") != self._device_unique_id:
_LOGGER.error(
f"Coordinator error. OPNsense Router Device ID ({self._state.get('device_unique_id')}) differs from the one saved in hass-opnsense ({self._device_unique_id})"
_LOGGER.debug(
f"[Coordinator async_update_data]: config device id: {self._device_unique_id}, "
f"router device id: {self._state.get('device_unique_id')}"
)
# Create repair task here
if self._state.get("device_unique_id"):
_LOGGER.error(
f"Coordinator error. OPNsense Router Device ID ({self._state.get('device_unique_id')}) differs from the one saved in hass-opnsense ({self._device_unique_id})"
)
self._mismatched_count += 1
# Trigger repair task and shutdown if this happens 3 times in a row
if self._mismatched_count == 3:
ir.async_create_issue(
hass=self.hass,
domain=DOMAIN,
issue_id=f"{self._device_unique_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 self.async_shutdown()
return {}
else:
self._mismatched_count = 0

# calculate pps and kbps
update_time = dict_get(self._state, "update_time")
Expand Down
4 changes: 4 additions & 0 deletions custom_components/opnsense/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"below_ltd_firmware": {
"title": "OPNsense Firmware below Recommended Version",
"description": "Some hass-opnsense {version} functions require OPNsense firmware {ltd_firmware} or later. With firmware {firmware}, some functions may not work and there may be errors in the logs."
},
"device_id_mismatched": {
"title": "OPNsense Hardware Has Changed",
"description": "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."
}
},
"title": "OPNsense"
Expand Down

0 comments on commit 7a8711c

Please sign in to comment.