Skip to content

Commit b9baad4

Browse files
committed
Hotspot voucher api interface
Signed-off-by: martin f. krafft <[email protected]>
1 parent 63dd4d5 commit b9baad4

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ USERNAME IP ADDRESS STATUS
8181

8282
Make sure you have your [Settings](#Settings) file configured correctly for these to work.
8383

84+
### [vouchers.py](vouchers.py)
85+
86+
A simple script to fetch the next ten unused vouchers, optionally limited to certain voucher groups:
87+
88+
```
89+
$ python vouchers.py kids
90+
kids: 112768, 426812, 004408, 551961, 090222, 023504, 657011, 125990, 591588, 650649
91+
```
92+
93+
If no arguments are provided, then all voucher groups are iterated. Else, only voucher groups that have any of the provided argument strings in their names are listed.
94+
8495
## Settings
8596

8697
You can store your controller settings in a configuration file to avoid hard-coding them in your scripts.

omada/omada.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,25 @@ def getWirelessGroups(self, site=None):
590590
##
591591
def getWirelessNetworks(self, group, site=None):
592592
return self.__get( f'/sites/{self.__findKey(site)}/setting/wlans/{group}/ssids' )
593+
594+
##
595+
## Returns the list of voucher groups for the given site.
596+
##
597+
def getVoucherGroups(self, site=None):
598+
return self.__geterator( f'/hotspot/sites/{self.__findKey(site)}/voucherGroups' )
599+
600+
##
601+
## Returns the details for the given voucher group and site.
602+
##
603+
def getVoucherGroupDetails(self, id, site=None):
604+
return self.__get( f'/hotspot/sites/{self.__findKey(site)}/voucherGroups/{id}' )
605+
606+
##
607+
## Returns unused vouchers for the given voucher group and site, up to an
608+
## optional maximum number
609+
##
610+
def getUnusedVouchers(self, id, maxnr=None, site=None):
611+
res = self.__get( f'/hotspot/sites/{self.__findKey(site)}/voucherGroups/{id}/printUnused' )
612+
data = res.get('data', [])
613+
return data if maxnr is None else data[:maxnr]
614+

vouchers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
from omada import Omada
5+
6+
def main():
7+
8+
omada = Omada(verbose=True)
9+
omada.login()
10+
11+
try:
12+
for vg in omada.getVoucherGroups():
13+
if len(sys.argv) > 1 and not any(arg in vg['name'] for arg in sys.argv):
14+
continue
15+
unused = omada.getUnusedVouchers(vg['id'], maxnr=10)
16+
codes = [v['code'] for v in unused]
17+
print(f"{vg['name']}: {', '.join(codes)}")
18+
19+
finally:
20+
omada.logout()
21+
22+
if __name__ == '__main__':
23+
import warnings
24+
with warnings.catch_warnings(action="ignore"):
25+
main()

0 commit comments

Comments
 (0)