-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added auto-detection of PoE function availability
- Loading branch information
Showing
11 changed files
with
200 additions
and
43 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,11 @@ | ||
from typing import Final | ||
|
||
URL_DEVICE_INFO: Final = "SystemInfoRpm.htm" | ||
URL_PORTS_SETTINGS_GET: Final = "PortSettingRpm.htm" | ||
URL_POE_SETTINGS_GET: Final = "PoeConfigRpm.htm" | ||
|
||
URL_PORT_SETTINGS_SET: Final = "port_setting.cgi" | ||
URL_POE_SETTINGS_SET: Final = "poe_global_config.cgi" | ||
URL_POE_PORT_SETTINGS_SET: Final = "poe_port_config.cgi" | ||
|
||
FEATURE_POE: Final = "feature_poe" |
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
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,80 @@ | ||
import logging | ||
from functools import wraps | ||
|
||
from .const import FEATURE_POE, URL_POE_SETTINGS_GET | ||
from .coreapi import ( | ||
ApiCallError, | ||
TpLinkWebApi, | ||
VariableType, | ||
APICALL_ERRCAT_DISCONNECTED, | ||
) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
# --------------------------- | ||
# TpLinkFeaturesDetector | ||
# --------------------------- | ||
class TpLinkFeaturesDetector: | ||
def __init__(self, core_api: TpLinkWebApi): | ||
"""Initialize.""" | ||
self._core_api = core_api | ||
self._available_features = set() | ||
self._is_initialized = False | ||
|
||
@staticmethod | ||
def disconnected_as_false(func): | ||
@wraps(func) | ||
async def wrapper(*args, **kwargs) -> bool: | ||
try: | ||
return await func(*args, **kwargs) | ||
except ApiCallError as ace: | ||
if ace.category == APICALL_ERRCAT_DISCONNECTED: | ||
return False | ||
raise | ||
|
||
return wrapper | ||
|
||
@staticmethod | ||
def log_feature(feature_name: str): | ||
def decorator(func): | ||
@wraps(func) | ||
async def wrapper(*args, **kwargs): | ||
try: | ||
_LOGGER.debug("Check feature '%s' availability", feature_name) | ||
result = await func(*args, **kwargs) | ||
if result: | ||
_LOGGER.debug("Feature '%s' is available", feature_name) | ||
else: | ||
_LOGGER.debug("Feature '%s' is not available", feature_name) | ||
return result | ||
except Exception: | ||
_LOGGER.debug( | ||
"Feature availability check failed on %s", feature_name | ||
) | ||
raise | ||
|
||
return wrapper | ||
|
||
return decorator | ||
|
||
@log_feature(FEATURE_POE) | ||
@disconnected_as_false | ||
async def _is_poe_available(self) -> bool: | ||
data = await self._core_api.get_variables( | ||
URL_POE_SETTINGS_GET, | ||
[ | ||
("portConfig", VariableType.Dict), | ||
("poe_port_num", VariableType.Int), | ||
], | ||
) | ||
return data.get("portConfig") is not None and data.get("poe_port_num") > 0 | ||
|
||
async def update(self) -> None: | ||
"""Update the available features list.""" | ||
if await self._is_poe_available(): | ||
self._available_features.add(FEATURE_POE) | ||
|
||
def is_available(self, feature: str) -> bool: | ||
"""Return true if feature is available.""" | ||
return feature in self._available_features |
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
Oops, something went wrong.