From 24600ef33d47804b314703a135c335331bf22297 Mon Sep 17 00:00:00 2001 From: thannerfabian Date: Sat, 15 Apr 2023 08:55:55 +0200 Subject: [PATCH 1/5] Added profiles --- omada/omada.py | 8 +++++- profiles.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100755 profiles.py diff --git a/omada/omada.py b/omada/omada.py index 987cb08..d821d97 100644 --- a/omada/omada.py +++ b/omada/omada.py @@ -496,7 +496,13 @@ def getSiteSettings(self, site=None): ## def setSiteSettings(self, settings, site=None): return self.__patch( f'/sites/{self.__findKey(site)}/setting', json=settings ) - + + ## + ## Returns the list of profiles for the given site. + ## + def getProfiles(self, site=None): + return self.__geterator( f'/sites/{self.__findKey(site)}/setting/lan/profiles' ) + ## ## Returns the list of timerange profiles for the given site. ## diff --git a/profiles.py b/profiles.py new file mode 100755 index 0000000..1d5d3bc --- /dev/null +++ b/profiles.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import sys, collections +from omada import Omada + +FIELDDEF = collections.OrderedDict([ + ('id', ('ID', 30)), + ('name', ('NAME', 30)), + ('poe', ('POE', 30)) +]) + +def format_poe (poe): + if poe == 0: + return 'Disabled' + elif poe == 1: + return 'Enabled' + elif poe == 2: + return 'Keep the device settings' + else: + return 'Unknown' + + +def print_header(): + + if sys.stdout.isatty(): + sys.stdout.write( '\33[1m' ) + + for key in FIELDDEF: + text,width = FIELDDEF[key] + sys.stdout.write( text.ljust(abs(width)) ) + + if sys.stdout.isatty(): + sys.stdout.write( '\33[0m' ) + + sys.stdout.write( '\n' ) + +def print_profile ( profile ): + for key in profile: + if key == 'poe': + profile[key] = format_poe( profile['poe']) + + for key in FIELDDEF: + text = profile[key] if key in profile else '--' + if not isinstance(text, str): text = str(text) + + width = FIELDDEF[key][1] + + if len(text) >= abs(width): + text = text[0:abs(width)-4] + '... ' + elif width > 0: + text = text.ljust(abs(width)) + else: + text = text.rjust(abs(width)-1) + + sys.stdout.write( text ) + + sys.stdout.write( '\n' ) + + +def main(): + omada = Omada() + omada.login() + + print_header() + + profiles = omada.getProfiles() + + for profile in profiles: + print_profile( profile ) + + omada.logout() + +if __name__ == '__main__': + main() From 2fda0c32cac195c454ef46d19b566b66734aff0f Mon Sep 17 00:00:00 2001 From: thannerfabian Date: Sat, 15 Apr 2023 09:00:51 +0200 Subject: [PATCH 2/5] Added native Network Id --- profiles.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/profiles.py b/profiles.py index 1d5d3bc..f3efacd 100755 --- a/profiles.py +++ b/profiles.py @@ -4,9 +4,10 @@ from omada import Omada FIELDDEF = collections.OrderedDict([ - ('id', ('ID', 30)), - ('name', ('NAME', 30)), - ('poe', ('POE', 30)) + ('id', ('ID', 30)), + ('name', ('NAME', 30)), + ('poe', ('POE', 30)), + ('nativeNetworkId', ('Native Network ID', 30)), ]) def format_poe (poe): From 5ea107d9d5b1fa01bffe05c6e4c96d20097d587d Mon Sep 17 00:00:00 2001 From: thannerfabian Date: Sat, 15 Apr 2023 10:00:20 +0200 Subject: [PATCH 3/5] Added poe settings --- omada/omada.py | 28 ++++++++++++++++++++++++++++ poe.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 poe.py diff --git a/omada/omada.py b/omada/omada.py index d821d97..f046e5a 100644 --- a/omada/omada.py +++ b/omada/omada.py @@ -502,6 +502,34 @@ def setSiteSettings(self, settings, site=None): ## def getProfiles(self, site=None): return self.__geterator( f'/sites/{self.__findKey(site)}/setting/lan/profiles' ) + + ## + ## Returns the settings for a profile for a given site. + ## + def getProfileSettings(self, profileId, site=None): + profiles = self.getProfiles() + for profile in profiles: + if profile['id'] == profileId: + return profile + + return + + ## + ## Returns the profile Id of a profile for a given site. + ## + def getProfileId(self, profileName, site=None): + profiles = self.getProfiles() + for profile in profiles: + if profile['name'] == profileName: + return profile['id'] + + return + + ## + ## Push back the settings for the profile + ## + def setPoeForProfile(self, profileId, settings, site=None): + return self.__patch( f'/sites/{self.__findKey(site)}/setting/lan/profiles/{profileId}', json=settings) ## ## Returns the list of timerange profiles for the given site. diff --git a/poe.py b/poe.py new file mode 100644 index 0000000..0aa76ca --- /dev/null +++ b/poe.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import sys, collections +from omada import Omada + +def format_poe(poe): + if poe == 0: + return 'Disabled' + elif poe == 1: + return 'Enabled' + elif poe == 2: + return "Keep the Device's Settings" + +def main(): + if len(sys.argv) > 2 and sys.argv[2] not in ("0", "1", "2"): + print( f"usage: {sys.argv[0]} [profile name] [0|1|2]" ) + return + + omada = Omada() + omada.login() + + if len(sys.argv) > 2: + profileId = omada.getProfileId(sys.argv[1]) + settings = omada.getProfileSettings(profileId) + settings['poe'] = sys.argv[2] + omada.setPoeForProfile(profileId, settings) + + settings = omada.getProfileSettings(profileId) + print( f"Changed the poe setting for profile {settings['name']} to {format_poe(settings['poe'])}.") + + omada.logout() + +if __name__ == '__main__': + main() From 8a103866383bbf7f32dafd40c9f940bd44d7e746 Mon Sep 17 00:00:00 2001 From: thannerfabian Date: Sat, 15 Apr 2023 10:04:54 +0200 Subject: [PATCH 4/5] Rename function --- omada/omada.py | 2 +- poe.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/omada/omada.py b/omada/omada.py index f046e5a..4efa932 100644 --- a/omada/omada.py +++ b/omada/omada.py @@ -528,7 +528,7 @@ def getProfileId(self, profileName, site=None): ## ## Push back the settings for the profile ## - def setPoeForProfile(self, profileId, settings, site=None): + def setProfileSettings(self, profileId, settings, site=None): return self.__patch( f'/sites/{self.__findKey(site)}/setting/lan/profiles/{profileId}', json=settings) ## diff --git a/poe.py b/poe.py index 0aa76ca..28c09c2 100644 --- a/poe.py +++ b/poe.py @@ -23,7 +23,7 @@ def main(): profileId = omada.getProfileId(sys.argv[1]) settings = omada.getProfileSettings(profileId) settings['poe'] = sys.argv[2] - omada.setPoeForProfile(profileId, settings) + omada.setProfileSettings(profileId, settings) settings = omada.getProfileSettings(profileId) print( f"Changed the poe setting for profile {settings['name']} to {format_poe(settings['poe'])}.") From b169929137ec8647bf520067aa2e75e61839a9af Mon Sep 17 00:00:00 2001 From: Fabian Thanner Date: Sat, 14 Sep 2024 09:57:41 +0200 Subject: [PATCH 5/5] Adapt to use config file --- alerts.py | 2 +- clients.py | 2 +- devices.py | 2 +- events.py | 2 +- led.py | 2 +- poe.py | 2 +- profiles.py | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/alerts.py b/alerts.py index 9f4cee0..7d3ef18 100755 --- a/alerts.py +++ b/alerts.py @@ -60,7 +60,7 @@ def print_alert( alert ): sys.stdout.write( '\n' ) def main(): - omada = Omada() + omada = Omada('omada.cfg') omada.login() print_header() diff --git a/clients.py b/clients.py index 6eefa6e..7f63a1b 100755 --- a/clients.py +++ b/clients.py @@ -98,7 +98,7 @@ def print_client( client ): sys.stdout.write( '\n' ) def main(): - omada = Omada() + omada = Omada('omada.cfg') omada.login() print_header() diff --git a/devices.py b/devices.py index 62bb7e0..8ff3933 100755 --- a/devices.py +++ b/devices.py @@ -66,7 +66,7 @@ def print_device( device ): sys.stdout.write( '\n' ) def main(): - omada = Omada() + omada = Omada('omada.cfg') omada.login() print_header() diff --git a/events.py b/events.py index d85d395..fc70b23 100755 --- a/events.py +++ b/events.py @@ -61,7 +61,7 @@ def print_event( event ): sys.stdout.write( '\n' ) def main(): - omada = Omada() + omada = Omada('omada.cfg') omada.login() print_header() diff --git a/led.py b/led.py index c92c5ec..7695556 100755 --- a/led.py +++ b/led.py @@ -8,7 +8,7 @@ def main(): print( f"usage: {sys.argv[0]} [on|off]" ) return - omada = Omada() + omada = Omada('omada.cfg') omada.login() settings = omada.getSiteSettings() diff --git a/poe.py b/poe.py index 28c09c2..9d9d578 100644 --- a/poe.py +++ b/poe.py @@ -16,7 +16,7 @@ def main(): print( f"usage: {sys.argv[0]} [profile name] [0|1|2]" ) return - omada = Omada() + omada = Omada('omada.cfg') omada.login() if len(sys.argv) > 2: diff --git a/profiles.py b/profiles.py index f3efacd..99b7329 100755 --- a/profiles.py +++ b/profiles.py @@ -59,7 +59,7 @@ def print_profile ( profile ): def main(): - omada = Omada() + omada = Omada('omada.cfg') omada.login() print_header()