Skip to content

Commit

Permalink
#5 Adapt to compal 0.4.0 with fix for changed API for enabling wifi g…
Browse files Browse the repository at this point in the history
…uest networks (#6)
  • Loading branch information
frimtec authored Jun 15, 2021
1 parent b077a68 commit 5ce4d9e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 55 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ command:
### Command switch
#### Usage
```
usage: compal-wifi-switch switch [-h] [--band {2g,5g,all}] [--guest [GUEST ...]]
usage: compal-wifi-switch switch [-h] [--band {2g,5g,all}] [--guest]
[--pause PAUSE] [--host HOST] [--password PASSWORD]
[--verbose]
{on,off}
Expand All @@ -77,9 +77,7 @@ optional arguments:
-h, --help show this help message and exit
--band {2g,5g,all}, -b {2g,5g,all}
band to switch power state for (default = all)
--guest [GUEST ...], -g [GUEST ...]
list of guest network mac-addresses to activate while
switching ON wifi
--guest, -g activate guest network while switching ON wifi
--pause PAUSE, -p PAUSE
number of seconds to pause after wifi state change
(default = 60); when the pause is too short, the
Expand Down Expand Up @@ -129,7 +127,7 @@ To show the status of the wifi signal, use the following command:
==============================================================
Model : CH7465LG
HW Version : 5.01
SW Version : CH7465LG-NCIP-6.15.28-4p8-NOSH
SW Version : CH7465LG-NCIP-6.15.30-1p3-1-NOSH
Serial Number : ************
Modem MAC Address : **:**:**:**:**:**
Operator ID : LIBERTYGLOBAL
Expand Down
65 changes: 21 additions & 44 deletions compal_wifi_switch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,22 @@ def status(host, password):
wifi_status.append(wifi_band)

wifi_guest_network_settings = WifiGuestNetworkSettings(modem).wifi_guest_network_settings
guest_network_interfaces = []
guest_network_interfaces += wifi_guest_network_settings.guest_networks_2g
guest_network_interfaces += wifi_guest_network_settings.guest_networks_5g

wifi_guest_status = []
for interface in guest_network_interfaces:
entry = {
'radio': interface.radio,
'enabled': interface.enable == 1,
'mac': interface.guest_mac,
'ssid': interface.ssid,
'hidden': interface.hidden == 1 == 1
}
wifi_guest_status.append(entry)
wifi_guest_status = [
{
'radio': '2g',
'enabled': wifi_guest_network_settings.enabling_2g.enabled,
'mac': wifi_guest_network_settings.enabling_2g.guest_mac,
'ssid': wifi_guest_network_settings.properties.ssid,
'hidden': wifi_guest_network_settings.properties.hidden == 1
},
{
'radio': '5g',
'enabled': wifi_guest_network_settings.enabling_5g.enabled,
'mac': wifi_guest_network_settings.enabling_5g.guest_mac,
'ssid': wifi_guest_network_settings.properties.ssid,
'hidden': wifi_guest_network_settings.properties.hidden == 1
},
]
modem.logout()

return {
Expand All @@ -115,30 +117,13 @@ def status(host, password):

@staticmethod
def switch(host, password, state, band, guest, pause, verbose=False):
guest_networks = guest
enable_guest_networks = len(guest_networks) > 0
if enable_guest_networks:
if guest:
if state == Switch.OFF:
raise ValueError('Argument guest (--guest, -g) not allowed for switch OFF action!')

modem = Compal(host, password)
modem.login()

wifi_guest_network = WifiGuestNetworkSettings(modem)
guest_settings = wifi_guest_network.wifi_guest_network_settings

not_found_guest_networks = []
guest_network_interfaces_to_enable = []
for guest_network in guest_networks:
found_interface = Commands.__find_guest_network(guest_settings, band, guest_network)
if found_interface is None:
not_found_guest_networks.append(guest_network)
else:
guest_network_interfaces_to_enable.append(found_interface)

if len(not_found_guest_networks) > 0:
raise ValueError(f"Guest network mac-addresses {not_found_guest_networks} not found for selected band {band}!")

wifi = WifiSettings(modem)
settings = wifi.wifi_settings
print(f"Switching wifi {state.name} (band = {band})")
Expand Down Expand Up @@ -171,21 +156,13 @@ def switch(host, password, state, band, guest, pause, verbose=False):
print(f"Wait {pause}s till wifi state is changed ...")
time.sleep(pause)

if enable_guest_networks:
if guest:
modem.login()
wifi_guest_network = WifiGuestNetworkSettings(modem)
guest_settings = wifi_guest_network.wifi_guest_network_settings

indexes_to_enable = set()
for index, guest_band in guest_network_interfaces_to_enable:
interface = Commands.__guest_settings_for_band(guest_settings, guest_band)[index]
print(f"Activating guest networks {interface.guest_mac}")
interface.enable = 1
indexes_to_enable.add(index)

for index in indexes_to_enable:
wifi_guest_network.update_interface_guest_network_settings(guest_settings, index, verbose)

wifi_guest_network.update_wifi_guest_network_settings(guest_settings.properties, True)
modem.logout()
print(f"Wait {pause}s till wifi state is changed ...")
time.sleep(pause)

print("Finished.")
7 changes: 2 additions & 5 deletions compal_wifi_switch/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,8 @@ def main():
choices=list(Band),
default="all",
help="band to switch power state for (default = all)")
switch_parser.add_argument('--guest', '-g',
type=str,
nargs='*',
default=[],
help="list of guest network mac-addresses to activate while switching ON wifi")
switch_parser.add_argument('--guest', '-g', action='store_true',
help="activate guest network while switching ON wifi")
switch_parser.add_argument('--pause', '-p',
type=int,
default=60,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
compal>=0.3.2
compal>=0.4.0
lxml~=4.6.3

0 comments on commit 5ce4d9e

Please sign in to comment.