Skip to content

Commit

Permalink
Merge pull request #194 from Snuffy2/Change-system_halt-and-system_re…
Browse files Browse the repository at this point in the history
…boot-to-REST-API

Change system_halt and system_reboot to REST API
  • Loading branch information
alexdelprete authored Sep 20, 2024
2 parents ae55dc3 + e69c133 commit a2b2676
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
44 changes: 11 additions & 33 deletions custom_components/opnsense/pyopnsense/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import time
from typing import Any
from urllib.parse import quote_plus, urlparse
from xml.parsers.expat import ExpatError
import xmlrpc.client
import zoneinfo

Expand Down Expand Up @@ -846,41 +845,20 @@ async def delete_arp_entry(self, ip) -> None:
await self._exec_php(script)

@_log_errors
async def system_reboot(self) -> None:
script: str = r"""
// /usr/local/opnsense/mvc/app/library/OPNsense/Core/Backend.php
use OPNsense\Core\Backend;
$backend = new Backend();
$backend->configdRun('system reboot', true);
$toreturn = [
"data" => true,
];
"""
try:
await self._exec_php(script)
except ExpatError:
# ignore response failures because the system is going down
pass
async def system_reboot(self) -> bool:
response: Mapping[str, Any] | list = await self._post("/api/core/system/reboot")
_LOGGER.debug(f"[system_reboot] response: {response}")
if isinstance(response, Mapping) and response.get("status", "") == "ok":
return True
return False

@_log_errors
async def system_halt(self) -> None:
script: str = r"""
use OPNsense\Core\Backend;
$backend = new Backend();
$backend->configdRun('system halt', true);
$toreturn = [
"data" => true,
];
"""
try:
await self._exec_php(script)
except ExpatError:
# ignore response failures because the system is going down
pass
response: Mapping[str, Any] | list = await self._post("/api/core/system/halt")
_LOGGER.debug(f"[system_halt] response: {response}")
if isinstance(response, Mapping) and response.get("status", "") == "ok":
return True
return False

@_log_errors
async def send_wol(self, interface, mac) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions function_method.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
| Get Carp Status | Yes | Yes | |
| Get Carp Interfaces | Yes | Yes | |
| Delete ARP Entry | Yes | | |
| System Reboot | Yes | Yes | |
| System Halt | Yes | Yes | |
| Are Notices Pending | Yes | Yes | |
| Get Notices | Yes | Yes | |
| Close Notice | Yes | Yes | |
Expand All @@ -49,6 +47,8 @@
| Upgrade Firmware | /api/core/firmware/update<br>/api/core/firmware/upgrade | 2018 | |
| Firmware Upgrade Status | /api/core/firmware/upgradestatus | 2018 | |
| Firmware Changelog | /api/core/firmware/changelog/ | 2018 | |
| System Reboot | /api/core/system/reboot | 20.1 | |
| System Halt | /api/core/system/halt | 20.1 | |
| Send WOL | /api/wol/wol/set | 2018 | |
| Get ARP Table | /api/diagnostics/interface/search_arp | 2022 | |
| Get System Info | /api/diagnostics/system/systemInformation | 24.7 | Partial: Still using XMLRPC for Getting Device ID |

0 comments on commit a2b2676

Please sign in to comment.