Skip to content

Commit

Permalink
Merge pull request #288 from Snuffy2/Improve-Firmware-check
Browse files Browse the repository at this point in the history
Improve firmware check
  • Loading branch information
Snuffy2 authored Oct 24, 2024
2 parents 2979ce9 + 0cbc2ca commit e7b9259
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 11 additions & 2 deletions custom_components/opnsense/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand All @@ -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}")
Expand Down Expand Up @@ -424,3 +429,7 @@ class BelowMinFirmware(Exception):

class UnknownFirmware(Exception):
pass


class PluginMissing(Exception):
pass
15 changes: 14 additions & 1 deletion custom_components/opnsense/pyopnsense/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand Down

0 comments on commit e7b9259

Please sign in to comment.