Skip to content

Commit

Permalink
Merge pull request #66 from andrey-git/fix-mypy-and-lints
Browse files Browse the repository at this point in the history
Fixes mypy and linting issues
  • Loading branch information
gjohansson-ST authored Jul 5, 2024
2 parents c6b5353 + c211761 commit 27683b4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 47 deletions.
1 change: 1 addition & 0 deletions examples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test-files."""
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,9 @@ ignore = [
"ANN401", # Dynamically typed expressions (typing.Any) are disallowed
"N818", # Exception name {name} should be named with an Error suffix
"S101", # Use of assert detected
"TCH001" # Move application import {} into a type-checking block
"TCH001", # Move application import {} into a type-checking block
"N999", # Invalid module name
"FBT003" # Boolean positional value in function call
]
select = ["ALL"]

Expand Down
49 changes: 24 additions & 25 deletions pysensibo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Python API for Sensibo."""

from __future__ import annotations
import asyncio
from datetime import datetime, timezone

import asyncio
import json
import logging
from datetime import datetime, timezone
from typing import Any

from aiohttp import ClientResponse, ClientSession
Expand Down Expand Up @@ -51,16 +52,15 @@ async def async_get_devices(self, fields: str = "*") -> dict[str, Any]:
params = {"apiKey": self.api_key, "fields": fields}
return await self._get(APIV2 + "/users/me/pods", params)

async def async_get_devices_data(self) -> SensiboData:
async def async_get_devices_data(self) -> SensiboData: # noqa: C901
"""Return dataclass with Sensibo Devices."""
devices: list[dict[str, Any]] = []
data = await self.async_get_devices()
if "result" not in data:
LOGGER.warning("No result in data from devices")
LOGGER.debug("Data without result: %s", data)
raise SensiboError("No result in data")
for device in data["result"]:
devices.append(device)
devices = list(data["result"])

device_data: dict[str, SensiboDevice] = {}
dev: dict[str, Any]
Expand Down Expand Up @@ -126,14 +126,14 @@ async def async_get_devices_data(self) -> SensiboData:
ac_states.get("mode"), {}
)
fan_modes: list[str] | None = current_capabilities.get("fanLevels")
fan_modes_translated: dict | None = None
fan_modes_translated: dict[str, str] | None = None
if fan_modes:
fan_modes_translated = {
_fan_mode.lower(): _fan_mode for _fan_mode in fan_modes
}
fan_modes = [_fan_mode.lower() for _fan_mode in fan_modes]
swing_modes: list[str] | None = current_capabilities.get("swing")
swing_modes_translated: dict | None = None
swing_modes_translated: dict[str, str] | None = None
if swing_modes:
swing_modes_translated = {
_swing_mode.lower(): _swing_mode for _swing_mode in swing_modes
Expand All @@ -142,7 +142,7 @@ async def async_get_devices_data(self) -> SensiboData:
horizontal_swing_modes: list[str] | None = current_capabilities.get(
"horizontalSwing"
)
horizontal_swing_modes_translated: dict | None = None
horizontal_swing_modes_translated: dict[str, str] | None = None
if horizontal_swing_modes:
horizontal_swing_modes_translated = {
_horizontal_mode.lower(): _horizontal_mode
Expand All @@ -153,7 +153,7 @@ async def async_get_devices_data(self) -> SensiboData:
for _horizontal_mode in horizontal_swing_modes
]
light_modes: list[str] | None = current_capabilities.get("light")
light_modes_translated: dict | None = None
light_modes_translated: dict[str, str] | None = None
if light_modes:
light_modes_translated = {
_light_mode.lower(): _light_mode for _light_mode in light_modes
Expand All @@ -170,8 +170,7 @@ async def async_get_devices_data(self) -> SensiboData:
if temperatures_list:
diff = MAX_POSSIBLE_STEP
for i in range(len(temperatures_list) - 1):
if temperatures_list[i + 1] - temperatures_list[i] < diff:
diff = temperatures_list[i + 1] - temperatures_list[i]
diff = min(diff, temperatures_list[i + 1] - temperatures_list[i])
temperature_step = diff

active_features = list(ac_states)
Expand Down Expand Up @@ -298,10 +297,10 @@ async def async_get_devices_data(self) -> SensiboData:
smart_type = smart_type.lower()
smart_low_temp_threshold = smart.get("lowTemperatureThreshold")
smart_high_temp_threshold = smart.get("highTemperatureThreshold")
_smart_low_state: dict[str | Any] = smart.get("lowTemperatureState", {})
_smart_high_state: dict[str | Any] = smart.get("highTemperatureState", {})
smart_low_state: dict[str | Any] = {}
smart_high_state: dict[str | Any] = {}
_smart_low_state: dict[str, Any] = smart.get("lowTemperatureState", {})
_smart_high_state: dict[str, Any] = smart.get("highTemperatureState", {})
smart_low_state: dict[str, Any] = {}
smart_high_state: dict[str, Any] = {}
if _smart_low_state:
for key, value in _smart_low_state.items():
smart_low_state[key.lower()] = (
Expand Down Expand Up @@ -615,12 +614,12 @@ async def _get(
path, params=params, timeout=self.timeout
) as resp:
return await self._response(resp)
except Exception as error:
except Exception:
LOGGER.debug("Retry %d on path %s", 4 - retry, path)
if retry > 0:
await asyncio.sleep(7)
return await self._get(path, params, retry - 1)
raise error
raise

async def _put(
self,
Expand All @@ -635,11 +634,11 @@ async def _put(
path, params=params, data=json.dumps(data), timeout=self.timeout
) as resp:
return await self._response(resp)
except Exception as error:
except Exception:
if retry is False:
await asyncio.sleep(5)
return await self._put(path, params, data, True)
raise error
raise

async def _post(
self,
Expand All @@ -655,11 +654,11 @@ async def _post(
path, params=params, data=json.dumps(data), timeout=self.timeout
) as resp:
return await self._response(resp)
except Exception as error:
except Exception:
if retry is False:
await asyncio.sleep(5)
return await self._post(path, params, data, True)
raise error
raise

async def _patch(
self,
Expand All @@ -675,11 +674,11 @@ async def _patch(
path, params=params, data=json.dumps(data), timeout=self.timeout
) as resp:
return await self._response(resp)
except Exception as error:
except Exception:
if retry is False:
await asyncio.sleep(5)
return await self._patch(path, params, data, True)
raise error
raise

async def _delete(
self, path: str, params: dict[str, Any], retry: bool = False
Expand All @@ -691,11 +690,11 @@ async def _delete(
path, params=params, timeout=self.timeout
) as resp:
return await self._response(resp)
except Exception as error:
except Exception:
if retry is False:
await asyncio.sleep(5)
return await self._delete(path, params, True)
raise error
raise

async def _response(self, resp: ClientResponse) -> dict[str, Any]:
"""Return response from call."""
Expand Down
9 changes: 5 additions & 4 deletions pysensibo/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Data classes for Sensibo."""

from __future__ import annotations

from dataclasses import dataclass
Expand Down Expand Up @@ -38,13 +39,13 @@ class SensiboDevice:
available: bool
hvac_modes: list[str] | None
fan_modes: list[str] | None
fan_modes_translated: dict | None
fan_modes_translated: dict[str, str] | None
swing_modes: list[str] | None
swing_modes_translated: dict | None
swing_modes_translated: dict[str, str] | None
horizontal_swing_modes: list[str] | None
horizontal_swing_modes_translated: dict | None
horizontal_swing_modes_translated: dict[str, str] | None
light_modes: list[str] | None
light_modes_translated: dict | None
light_modes_translated: dict[str, str] | None
temp_unit: str | None
temp_list: list[int]
temp_step: int
Expand Down
17 changes: 0 additions & 17 deletions test-files/test.py

This file was deleted.

0 comments on commit 27683b4

Please sign in to comment.