-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1409 from aparcar/power-ubus
power: add ubus NetworkPowerPort
- Loading branch information
Showing
3 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
UBUS jsonrpc interface for PoE management on OpenWrt devices. This comes in | ||
handy if devices are connected to a PoE switch running OpenWrt. | ||
The URL given in hosts in exporter.yaml must accept unauthenticated UBUS | ||
calls for the two `poe` calls `info` and `manage`. | ||
Further information is availbe at https://openwrt.org/docs/techref/ubus#acls | ||
NetworkPowerPort: | ||
model: ubus | ||
host: 'http://192.168.1.1/ubus' | ||
index: 1 | ||
""" | ||
|
||
import requests | ||
|
||
|
||
def jsonrpc_call(host, path, method, message): | ||
r = requests.post( | ||
host, | ||
json={ | ||
"jsonrpc": "2.0", | ||
"id": 1, | ||
"method": "call", | ||
"params": ["00000000000000000000000000000000", path, method, message], | ||
}, | ||
) | ||
r.raise_for_status() | ||
return r.json()["result"] | ||
|
||
|
||
def power_set(host, port, index, value): | ||
assert port is None | ||
|
||
jsonrpc_call(host, "poe", "manage", {"port": f"lan{index}", "enable": value == 1}) | ||
|
||
|
||
def power_get(host, port, index): | ||
assert port is None | ||
|
||
poe_info = jsonrpc_call(host, "poe", "info", {})[1] | ||
|
||
assert ( | ||
f"lan{index}" in poe_info["ports"] | ||
), f"Port lan{index} not found in {poe_info['ports']}" | ||
|
||
return poe_info["ports"][f"lan{index}"] != "Disabled" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters