14
14
HuaweiConnectionInfo ,
15
15
HuaweiDeviceNode ,
16
16
HuaweiFilterInfo ,
17
+ HuaweiFilterItem ,
17
18
HuaweiRouterInfo ,
19
+ HuaweiUrlFilterInfo ,
18
20
)
19
21
from .coreapi import APICALL_ERRCAT_UNAUTHORIZED , ApiCallError , HuaweiCoreApi
20
22
21
23
SWITCH_NFC : Final = "nfc_switch"
22
24
SWITCH_WIFI_80211R : Final = "wifi_80211r_switch"
23
25
SWITCH_WIFI_TWT : Final = "wifi_twt_switch"
24
26
SWITCH_WLAN_FILTER : Final = "wlan_filter_switch"
27
+ SWITCH_URL_FILTER : Final = "url_filter_switch"
25
28
26
29
ACTION_REBOOT : Final = "reboot_action"
27
30
28
31
CONNECTED_VIA_ID_PRIMARY : Final = "primary"
29
32
30
33
FEATURE_NFC : Final = "feature_nfc"
34
+ FEATURE_URL_FILTER : Final = "feature_url_filter"
31
35
FEATURE_WIFI_80211R : Final = "feature_wifi_80211r"
32
36
FEATURE_WIFI_TWT : Final = "feature_wifi_twt"
33
37
FEATURE_WLAN_FILTER : Final = "feature_wlan_filter"
43
47
_URL_REPEATER_INFO : Final = "api/ntwk/repeaterinfo"
44
48
_URL_WANDETECT : Final = "api/ntwk/wandetect"
45
49
_URL_WLAN_FILTER : Final = "api/ntwk/wlanfilterenhance"
50
+ _URL_URL_FILTER : Final = "api/ntwk/urlfilter"
46
51
47
52
_STATUS_CONNECTED : Final = "Connected"
48
53
@@ -153,6 +158,12 @@ async def _is_device_topology_available(self) -> bool:
153
158
data = await self ._core_api .get (_URL_DEVICE_TOPOLOGY )
154
159
return data is not None
155
160
161
+ @log_feature (FEATURE_URL_FILTER )
162
+ @unauthorized_as_false
163
+ async def _is_url_filter_available (self ) -> bool :
164
+ data = await self ._core_api .get (_URL_URL_FILTER )
165
+ return data is not None
166
+
156
167
async def update (self ) -> None :
157
168
"""Update the available features list."""
158
169
if await self ._is_nfc_available ():
@@ -170,14 +181,14 @@ async def update(self) -> None:
170
181
if await self ._is_device_topology_available ():
171
182
self ._available_features .add (FEATURE_DEVICE_TOPOLOGY )
172
183
184
+ if await self ._is_url_filter_available ():
185
+ self ._available_features .add (FEATURE_URL_FILTER )
186
+
173
187
def is_available (self , feature : str ) -> bool :
174
188
"""Return true if feature is available."""
175
189
return feature in self ._available_features
176
190
177
191
178
- # ---------------------------
179
- # HuaweiApi
180
- # ---------------------------
181
192
class HuaweiApi :
182
193
def __init__ (
183
194
self ,
@@ -530,6 +541,27 @@ async def _get_filter_states(self):
530
541
state_5g = state
531
542
return state_2g , state_5g
532
543
544
+ async def apply_url_filter_info (self , url_filter_info : HuaweiUrlFilterInfo ) -> None :
545
+ actual = await self .get_url_filter_info ()
546
+ existing_item = next ((item for item in actual if item .filter_id == url_filter_info .filter_id ))
547
+
548
+ action : str = "update" if existing_item else "create"
549
+
550
+ data : dict [str , Any ] = {
551
+ "Devices" : [
552
+ {"MACAddress" : device .mac_address } for device in url_filter_info .devices
553
+ ],
554
+ "DeviceNames" : [
555
+ {"HostName" : device .name } for device in url_filter_info .devices
556
+ ],
557
+ "DevManual" : url_filter_info .dev_manual ,
558
+ "URL" : url_filter_info .url ,
559
+ "Status" : 2 if url_filter_info .enabled else 0 ,
560
+ "ID" : url_filter_info .filter_id ,
561
+ }
562
+
563
+ await self ._core_api .post (_URL_URL_FILTER , data , extra_data = {"action" : action })
564
+
533
565
async def _process_access_lists (
534
566
self ,
535
567
state : dict [str , Any ],
@@ -660,3 +692,22 @@ async def get_is_repeater(self) -> bool:
660
692
repeater_enable = data .get ("RepeaterEnable" , False )
661
693
662
694
return isinstance (repeater_enable , bool ) and repeater_enable
695
+
696
+ @staticmethod
697
+ def _to_url_filter_info (data : dict [str , Any ]) -> HuaweiUrlFilterInfo :
698
+ result = HuaweiUrlFilterInfo (
699
+ filter_id = data ["ID" ],
700
+ url = data ["URL" ],
701
+ enabled = data .get ("Status" , - 1 ) == 2 ,
702
+ dev_manual = data .get ("DevManual" ) is True ,
703
+ devices = [
704
+ HuaweiFilterItem (item [0 ].get ("MACAddress" ), item [1 ].get ("HostName" ))
705
+ for item in zip (data ["Devices" ], data ["DeviceNames" ])
706
+ ],
707
+ )
708
+
709
+ return result
710
+
711
+ async def get_url_filter_info (self ) -> Iterable [HuaweiUrlFilterInfo ]:
712
+ data = await self ._core_api .get (_URL_URL_FILTER )
713
+ return [self ._to_url_filter_info (item ) for item in data ]
0 commit comments