diff --git a/custom_components/opnsense/config_flow.py b/custom_components/opnsense/config_flow.py index 43fe337..6e8feb3 100644 --- a/custom_components/opnsense/config_flow.py +++ b/custom_components/opnsense/config_flow.py @@ -77,7 +77,7 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): # when user submits has user_input async def async_step_user(self, user_input=None): """Handle the initial step.""" - errors = {} + errors: Mapping[str, Any] = {} if user_input is not None: try: name = user_input.get(CONF_NAME, False) or None @@ -121,8 +121,10 @@ async def async_step_user(self, user_input=None): except awesomeversion.exceptions.AwesomeVersionCompareException: raise UnknownFirmware() - system_info: Mapping[str, Any] = await client.get_system_info() + if not await client.is_plugin_installed(): + raise PluginMissing() + system_info: Mapping[str, Any] = await client.get_system_info() if name is None: name: str = system_info.get("name") or "OPNsense" @@ -146,6 +148,9 @@ async def async_step_user(self, user_input=None): _LOGGER.error( f"Missing Device Unique ID Error. {err.__class__.__qualname__}: {err}" ) + except PluginMissing: + errors["base"] = "plugin_missing" + _LOGGER.error("OPNsense Plugin Missing") except (aiohttp.InvalidURL, InvalidURL) as err: errors["base"] = "invalid_url_format" _LOGGER.error(f"InvalidURL Error. {err.__class__.__qualname__}: {err}") @@ -424,3 +429,7 @@ class BelowMinFirmware(Exception): class UnknownFirmware(Exception): pass + + +class PluginMissing(Exception): + pass diff --git a/custom_components/opnsense/pyopnsense/__init__.py b/custom_components/opnsense/pyopnsense/__init__.py index 413001c..9c875df 100644 --- a/custom_components/opnsense/pyopnsense/__init__.py +++ b/custom_components/opnsense/pyopnsense/__init__.py @@ -217,7 +217,7 @@ async def _exec_php(self, script) -> Mapping[str, Any]: @_log_errors async def get_host_firmware_version(self) -> None | str: firmware_info: Mapping[str, Any] | list = await self._get( - "/api/core/firmware/status" + "/api/core/firmware/info" ) if not isinstance(firmware_info, Mapping): return None @@ -226,6 +226,19 @@ async def get_host_firmware_version(self) -> None | str: self._firmware_version = firmware return firmware + async def is_plugin_installed(self) -> bool: + firmware_info: Mapping[str, Any] | list = await self._get( + "/api/core/firmware/info" + ) + if not isinstance(firmware_info, Mapping) or not isinstance( + firmware_info.get("package"), list + ): + return False + for pkg in firmware_info.get("package"): + if pkg.get("name", None) == "os-homeassistant-maxit": + return True + return False + async def _get_from_stream(self, path: str) -> Mapping[str, Any] | list: self._rest_api_query_count += 1 url: str = f"{self._url}{path}"