mirror of
https://github.com/Andre0512/pyhOn.git
synced 2024-01-19 02:50:44 +00:00
Enable more pylint checks
This commit is contained in:
parent
e1c8bc5835
commit
5a778373b6
|
@ -1,7 +1,9 @@
|
||||||
[MESSAGES CONTROL]
|
[MESSAGES CONTROL]
|
||||||
|
|
||||||
disable=C,R
|
disable=missing-docstring
|
||||||
|
|
||||||
[FORMAT]
|
[FORMAT]
|
||||||
|
|
||||||
|
max-args=6
|
||||||
|
max-attributes=8
|
||||||
max-line-length=88
|
max-line-length=88
|
||||||
|
|
|
@ -11,6 +11,7 @@ from typing import Tuple, Dict, Any
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||||
|
|
||||||
|
# pylint: disable=wrong-import-position
|
||||||
from pyhon import Hon, HonAPI, diagnose, printer
|
from pyhon import Hon, HonAPI, diagnose, printer
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -91,11 +92,9 @@ async def main() -> None:
|
||||||
data = device.data.copy()
|
data = device.data.copy()
|
||||||
attr = "get" if args.get("all") else "pop"
|
attr = "get" if args.get("all") else "pop"
|
||||||
print(
|
print(
|
||||||
printer.key_print(
|
printer.key_print(getattr(data["attributes"], attr)("parameters"))
|
||||||
data["attributes"].__getattribute__(attr)("parameters")
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
print(printer.key_print(data.__getattribute__(attr)("appliance")))
|
print(printer.key_print(getattr(data, attr)("appliance")))
|
||||||
print(printer.key_print(data))
|
print(printer.key_print(data))
|
||||||
print(
|
print(
|
||||||
printer.pretty_print(
|
printer.pretty_print(
|
||||||
|
|
|
@ -21,6 +21,7 @@ if TYPE_CHECKING:
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=too-many-public-methods,too-many-instance-attributes
|
||||||
class HonAppliance:
|
class HonAppliance:
|
||||||
_MINIMAL_UPDATE_INTERVAL = 5 # seconds
|
_MINIMAL_UPDATE_INTERVAL = 5 # seconds
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# pylint: disable=duplicate-code
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
from pyhon.appliances.base import ApplianceBase
|
from pyhon.appliances.base import ApplianceBase
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# pylint: disable=duplicate-code
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
from pyhon.appliances.base import ApplianceBase
|
from pyhon.appliances.base import ApplianceBase
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# pylint: disable=duplicate-code
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
|
|
||||||
from pyhon.appliances.base import ApplianceBase
|
from pyhon.appliances.base import ApplianceBase
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# pylint: disable=duplicate-code
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
from pyhon.appliances.base import ApplianceBase
|
from pyhon.appliances.base import ApplianceBase
|
||||||
|
|
|
@ -17,12 +17,12 @@ class HonCommandLoader:
|
||||||
"""Loads and parses hOn command data"""
|
"""Loads and parses hOn command data"""
|
||||||
|
|
||||||
def __init__(self, api: "HonAPI", appliance: "HonAppliance") -> None:
|
def __init__(self, api: "HonAPI", appliance: "HonAppliance") -> None:
|
||||||
|
self._api: "HonAPI" = api
|
||||||
|
self._appliance: "HonAppliance" = appliance
|
||||||
self._api_commands: Dict[str, Any] = {}
|
self._api_commands: Dict[str, Any] = {}
|
||||||
self._favourites: List[Dict[str, Any]] = []
|
self._favourites: List[Dict[str, Any]] = []
|
||||||
self._command_history: List[Dict[str, Any]] = []
|
self._command_history: List[Dict[str, Any]] = []
|
||||||
self._commands: Dict[str, HonCommand] = {}
|
self._commands: Dict[str, HonCommand] = {}
|
||||||
self._api: "HonAPI" = api
|
|
||||||
self._appliance: "HonAppliance" = appliance
|
|
||||||
self._appliance_data: Dict[str, Any] = {}
|
self._appliance_data: Dict[str, Any] = {}
|
||||||
self._additional_data: Dict[str, Any] = {}
|
self._additional_data: Dict[str, Any] = {}
|
||||||
|
|
||||||
|
|
|
@ -27,17 +27,16 @@ class HonCommand:
|
||||||
categories: Optional[Dict[str, "HonCommand"]] = None,
|
categories: Optional[Dict[str, "HonCommand"]] = None,
|
||||||
category_name: str = "",
|
category_name: str = "",
|
||||||
):
|
):
|
||||||
|
self._name: str = name
|
||||||
self._api: Optional[HonAPI] = appliance.api
|
self._api: Optional[HonAPI] = appliance.api
|
||||||
self._appliance: "HonAppliance" = appliance
|
self._appliance: "HonAppliance" = appliance
|
||||||
self._name: str = name
|
|
||||||
self._categories: Optional[Dict[str, "HonCommand"]] = categories
|
self._categories: Optional[Dict[str, "HonCommand"]] = categories
|
||||||
self._category_name: str = category_name
|
self._category_name: str = category_name
|
||||||
self._description: str = attributes.pop("description", "")
|
self._parameters: Dict[str, Parameter] = {}
|
||||||
self._protocol_type: str = attributes.pop("protocolType", "")
|
|
||||||
self._parameters: Dict[str, HonParameter] = {}
|
|
||||||
self._data: Dict[str, Any] = {}
|
self._data: Dict[str, Any] = {}
|
||||||
self._available_settings: Dict[str, HonParameter] = {}
|
|
||||||
self._rules: List[HonRuleSet] = []
|
self._rules: List[HonRuleSet] = []
|
||||||
|
attributes.pop("description", "")
|
||||||
|
attributes.pop("protocolType", "")
|
||||||
self._load_parameters(attributes)
|
self._load_parameters(attributes)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
|
|
|
@ -30,6 +30,14 @@ class HonLoginData:
|
||||||
loaded: Optional[Dict[str, Any]] = None
|
loaded: Optional[Dict[str, Any]] = None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class HonAuthData:
|
||||||
|
access_token: str = ""
|
||||||
|
refresh_token: str = ""
|
||||||
|
cognito_token: str = ""
|
||||||
|
id_token: str = ""
|
||||||
|
|
||||||
|
|
||||||
class HonAuth:
|
class HonAuth:
|
||||||
_TOKEN_EXPIRES_AFTER_HOURS = 8
|
_TOKEN_EXPIRES_AFTER_HOURS = 8
|
||||||
_TOKEN_EXPIRE_WARNING_HOURS = 7
|
_TOKEN_EXPIRE_WARNING_HOURS = 7
|
||||||
|
@ -46,28 +54,25 @@ class HonAuth:
|
||||||
self._login_data = HonLoginData()
|
self._login_data = HonLoginData()
|
||||||
self._login_data.email = email
|
self._login_data.email = email
|
||||||
self._login_data.password = password
|
self._login_data.password = password
|
||||||
self._access_token = ""
|
|
||||||
self._refresh_token = ""
|
|
||||||
self._cognito_token = ""
|
|
||||||
self._id_token = ""
|
|
||||||
self._device = device
|
self._device = device
|
||||||
self._expires: datetime = datetime.utcnow()
|
self._expires: datetime = datetime.utcnow()
|
||||||
|
self._auth = HonAuthData()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cognito_token(self) -> str:
|
def cognito_token(self) -> str:
|
||||||
return self._cognito_token
|
return self._auth.cognito_token
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id_token(self) -> str:
|
def id_token(self) -> str:
|
||||||
return self._id_token
|
return self._auth.id_token
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def access_token(self) -> str:
|
def access_token(self) -> str:
|
||||||
return self._access_token
|
return self._auth.access_token
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def refresh_token(self) -> str:
|
def refresh_token(self) -> str:
|
||||||
return self._refresh_token
|
return self._auth.refresh_token
|
||||||
|
|
||||||
def _check_token_expiration(self, hours: int) -> bool:
|
def _check_token_expiration(self, hours: int) -> bool:
|
||||||
return datetime.utcnow() >= self._expires + timedelta(hours=hours)
|
return datetime.utcnow() >= self._expires + timedelta(hours=hours)
|
||||||
|
@ -192,12 +197,12 @@ class HonAuth:
|
||||||
|
|
||||||
def _parse_token_data(self, text: str) -> bool:
|
def _parse_token_data(self, text: str) -> bool:
|
||||||
if access_token := re.findall("access_token=(.*?)&", text):
|
if access_token := re.findall("access_token=(.*?)&", text):
|
||||||
self._access_token = access_token[0]
|
self._auth.access_token = access_token[0]
|
||||||
if refresh_token := re.findall("refresh_token=(.*?)&", text):
|
if refresh_token := re.findall("refresh_token=(.*?)&", text):
|
||||||
self._refresh_token = refresh_token[0]
|
self._auth.refresh_token = refresh_token[0]
|
||||||
if id_token := re.findall("id_token=(.*?)&", text):
|
if id_token := re.findall("id_token=(.*?)&", text):
|
||||||
self._id_token = id_token[0]
|
self._auth.id_token = id_token[0]
|
||||||
return True if access_token and refresh_token and id_token else False
|
return bool(access_token and refresh_token and id_token)
|
||||||
|
|
||||||
async def _get_token(self, url: str) -> bool:
|
async def _get_token(self, url: str) -> bool:
|
||||||
async with self._request.get(url) as response:
|
async with self._request.get(url) as response:
|
||||||
|
@ -229,7 +234,7 @@ class HonAuth:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def _api_auth(self) -> bool:
|
async def _api_auth(self) -> bool:
|
||||||
post_headers = {"id-token": self._id_token}
|
post_headers = {"id-token": self._auth.id_token}
|
||||||
data = self._device.get()
|
data = self._device.get()
|
||||||
async with self._request.post(
|
async with self._request.post(
|
||||||
f"{const.API_URL}/auth/v1/login", headers=post_headers, json=data
|
f"{const.API_URL}/auth/v1/login", headers=post_headers, json=data
|
||||||
|
@ -239,8 +244,8 @@ class HonAuth:
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
await self._error_logger(response)
|
await self._error_logger(response)
|
||||||
return False
|
return False
|
||||||
self._cognito_token = json_data.get("cognitoUser", {}).get("Token", "")
|
self._auth.cognito_token = json_data.get("cognitoUser", {}).get("Token", "")
|
||||||
if not self._cognito_token:
|
if not self._auth.cognito_token:
|
||||||
_LOGGER.error(json_data)
|
_LOGGER.error(json_data)
|
||||||
raise exceptions.HonAuthenticationError()
|
raise exceptions.HonAuthenticationError()
|
||||||
return True
|
return True
|
||||||
|
@ -262,7 +267,7 @@ class HonAuth:
|
||||||
async def refresh(self) -> bool:
|
async def refresh(self) -> bool:
|
||||||
params = {
|
params = {
|
||||||
"client_id": const.CLIENT_ID,
|
"client_id": const.CLIENT_ID,
|
||||||
"refresh_token": self._refresh_token,
|
"refresh_token": self._auth.refresh_token,
|
||||||
"grant_type": "refresh_token",
|
"grant_type": "refresh_token",
|
||||||
}
|
}
|
||||||
async with self._request.post(
|
async with self._request.post(
|
||||||
|
@ -273,14 +278,14 @@ class HonAuth:
|
||||||
return False
|
return False
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
self._expires = datetime.utcnow()
|
self._expires = datetime.utcnow()
|
||||||
self._id_token = data["id_token"]
|
self._auth.id_token = data["id_token"]
|
||||||
self._access_token = data["access_token"]
|
self._auth.access_token = data["access_token"]
|
||||||
return await self._api_auth()
|
return await self._api_auth()
|
||||||
|
|
||||||
def clear(self) -> None:
|
def clear(self) -> None:
|
||||||
self._session.cookie_jar.clear_domain(const.AUTH_API.split("/")[-2])
|
self._session.cookie_jar.clear_domain(const.AUTH_API.split("/")[-2])
|
||||||
self._request.called_urls = []
|
self._request.called_urls = []
|
||||||
self._cognito_token = ""
|
self._auth.cognito_token = ""
|
||||||
self._id_token = ""
|
self._auth.id_token = ""
|
||||||
self._access_token = ""
|
self._auth.access_token = ""
|
||||||
self._refresh_token = ""
|
self._auth.refresh_token = ""
|
||||||
|
|
|
@ -7,9 +7,10 @@ from typing import List, Optional, Dict, Any, Type
|
||||||
from aiohttp import ClientSession
|
from aiohttp import ClientSession
|
||||||
from typing_extensions import Self
|
from typing_extensions import Self
|
||||||
|
|
||||||
from pyhon import HonAPI, exceptions
|
|
||||||
from pyhon.appliance import HonAppliance
|
from pyhon.appliance import HonAppliance
|
||||||
|
from pyhon.connection.api import HonAPI
|
||||||
from pyhon.connection.api import TestAPI
|
from pyhon.connection.api import TestAPI
|
||||||
|
from pyhon.exceptions import NoAuthenticationException
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -43,7 +44,7 @@ class Hon:
|
||||||
@property
|
@property
|
||||||
def api(self) -> HonAPI:
|
def api(self) -> HonAPI:
|
||||||
if self._api is None:
|
if self._api is None:
|
||||||
raise exceptions.NoAuthenticationException
|
raise NoAuthenticationException
|
||||||
return self._api
|
return self._api
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -30,7 +30,7 @@ def pretty_print(
|
||||||
) -> str:
|
) -> str:
|
||||||
result = ""
|
result = ""
|
||||||
space = whitespace * intend
|
space = whitespace * intend
|
||||||
if (isinstance(data, list) or isinstance(data, dict)) and key:
|
if isinstance(data, (dict, list)) and key:
|
||||||
result += f"{space}{'- ' if is_list else ''}{key}:\n"
|
result += f"{space}{'- ' if is_list else ''}{key}:\n"
|
||||||
intend += 1
|
intend += 1
|
||||||
if isinstance(data, list):
|
if isinstance(data, list):
|
||||||
|
@ -39,10 +39,10 @@ def pretty_print(
|
||||||
value, intend=intend, is_list=True, whitespace=whitespace
|
value, intend=intend, is_list=True, whitespace=whitespace
|
||||||
)
|
)
|
||||||
elif isinstance(data, dict):
|
elif isinstance(data, dict):
|
||||||
for i, (key, value) in enumerate(sorted(data.items())):
|
for i, (list_key, value) in enumerate(sorted(data.items())):
|
||||||
result += pretty_print(
|
result += pretty_print(
|
||||||
value,
|
value,
|
||||||
key=key,
|
key=list_key,
|
||||||
intend=intend + (is_list if i else 0),
|
intend=intend + (is_list if i else 0),
|
||||||
is_list=is_list and not i,
|
is_list=is_list and not i,
|
||||||
whitespace=whitespace,
|
whitespace=whitespace,
|
||||||
|
|
|
@ -25,6 +25,10 @@ class HonRuleSet:
|
||||||
self._rules: Dict[str, List[HonRule]] = {}
|
self._rules: Dict[str, List[HonRule]] = {}
|
||||||
self._parse_rule(rule)
|
self._parse_rule(rule)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def rules(self) -> Dict[str, List[HonRule]]:
|
||||||
|
return self._rules
|
||||||
|
|
||||||
def _parse_rule(self, rule: Dict[str, Any]) -> None:
|
def _parse_rule(self, rule: Dict[str, Any]) -> None:
|
||||||
for param_key, params in rule.items():
|
for param_key, params in rule.items():
|
||||||
param_key = self._command.appliance.options.get(param_key, param_key)
|
param_key = self._command.appliance.options.get(param_key, param_key)
|
||||||
|
|
|
@ -11,7 +11,7 @@ if TYPE_CHECKING:
|
||||||
from pyhon.parameter.range import HonParameterRange
|
from pyhon.parameter.range import HonParameterRange
|
||||||
|
|
||||||
|
|
||||||
class Callback(Protocol):
|
class Callback(Protocol): # pylint: disable=too-few-public-methods
|
||||||
def __call__(
|
def __call__(
|
||||||
self, url: str | URL, *args: Any, **kwargs: Any
|
self, url: str | URL, *args: Any, **kwargs: Any
|
||||||
) -> aiohttp.client._RequestContextManager:
|
) -> aiohttp.client._RequestContextManager:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
black==23.3.0
|
black==23.7.0
|
||||||
flake8==6.0.0
|
flake8==6.0.0
|
||||||
mypy==1.2.0
|
mypy==1.4.1
|
||||||
pylint==2.17.2
|
pylint==2.17.4
|
||||||
|
|
Loading…
Reference in a new issue