Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change notices functions to REST API #203

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
get_notices to REST API
Remove are_notices_pending and incorporate into get_notices
  • Loading branch information
Snuffy2 committed Sep 20, 2024
commit 354a1f52a21f0f5aef3124b3722ab49ada6cac21
10 changes: 1 addition & 9 deletions custom_components/opnsense/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ async def _get_carp_status(self):
async def _get_dhcp_leases(self):
return await self._client.get_dhcp_leases()

@_log_timing
async def _are_notices_pending(self):
return await self._client.are_notices_pending()

@_log_timing
async def _get_notices(self):
return await self._client.get_notices()
Expand Down Expand Up @@ -147,11 +143,7 @@ async def _async_update_data(self):
# self._state["dhcp_leases"] = await self._client.get_dhcp_leases()
self._state["dhcp_leases"] = []
self._state["dhcp_stats"] = {}
self._state["notices"] = {}
self._state["notices"][
"pending_notices_present"
] = await self._are_notices_pending()
self._state["notices"]["pending_notices"] = await self._get_notices()
self._state["notices"] = await self._get_notices()

lease_stats: Mapping[str, int] = {"total": 0, "online": 0, "offline": 0}
for lease in self._state["dhcp_leases"]:
Expand Down
88 changes: 21 additions & 67 deletions custom_components/opnsense/pyopnsense/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1617,75 +1617,29 @@ async def _get_telemetry_legacy(self) -> Mapping[str, Any]:
return telemetry

@_log_errors
async def are_notices_pending(self) -> Mapping[str, Any]:
script: str = r"""
if (file_exists('/usr/local/etc/inc/notices.inc')) {
require_once '/usr/local/etc/inc/notices.inc';

$toreturn = [
"data" => are_notices_pending(),
];
} else {
$status = new \OPNsense\System\SystemStatus();
$pending = false;
foreach ($status->getSystemStatus() as $key => $value) {
if ($value["statusCode"] != 2) {
$pending = true;
break;
}
}
$toreturn = [
"data" => $pending,
];
}
"""
response: Mapping[str, Any] = await self._exec_php(script)
if response is None or not isinstance(response, Mapping):
_LOGGER.error("Invalid data returned from are_notices_pending")
return {}
return response.get("data", {})

@_log_errors
async def get_notices(self):
script: str = r"""
if (file_exists('/usr/local/etc/inc/notices.inc')) {
require_once '/usr/local/etc/inc/notices.inc';

$toreturn = [
"data" => get_notices(),
];
} else {
$status = new \OPNsense\System\SystemStatus();
$toreturn = [
"data" => $status->getSystemStatus(),
];
}
"""
response: Mapping[str, Any] = await self._exec_php(script)
if response is None or not isinstance(response, Mapping):
_LOGGER.error("Invalid data returned from get_notices -> getSystemStatus")
return []
value: Mapping[str, Any] = response.get("data", [])
async def get_notices(self) -> list:
notices_info: Mapping[str, Any] | list = await self._get(
"/api/core/system/status"
)
_LOGGER.debug(f"[get_notices] notices_info: {notices_info}")

if isinstance(value, list):
if not isinstance(notices_info, Mapping):
return []

notices: list = []
for key in value.keys():
notice: Mapping[str, Any] = value.get(key)
# 22.7.2+
if "statusCode" in notice.keys():
if notice["statusCode"] != 2:
real_notice = {}
real_notice["notice"] = notice["message"]
real_notice["id"] = key
real_notice["created_at"] = notice["timestamp"]
notices.append(real_notice)
else:
notice["created_at"] = key
notice["id"] = key
notices.append(notice)

pending_notices_present = False
pending_notices: list = []
for key, notice in notices_info.items():
if isinstance(notices_info, Mapping) and notice.get("statusCode", 2) != 2:
pending_notices_present = True
real_notice: Mapping[str, Any] = {}
real_notice["notice"] = notice.get("message", None)
real_notice["id"] = key
real_notice["created_at"] = notice.get("timestamp", None)
pending_notices.append(real_notice)

notices: Mapping[str, Any] = {}
notices["pending_notices_present"] = pending_notices_present
notices["pending_notices"] = pending_notices
_LOGGER.debug(f"[get_notices] notices: {notices}")
return notices

@_log_errors
Expand Down