1
1
"""Python API for Sensibo."""
2
+
2
3
from __future__ import annotations
3
- import asyncio
4
- from datetime import datetime , timezone
5
4
5
+ import asyncio
6
6
import json
7
7
import logging
8
+ from datetime import datetime , timezone
8
9
from typing import Any
9
10
10
11
from aiohttp import ClientResponse , ClientSession
@@ -51,16 +52,15 @@ async def async_get_devices(self, fields: str = "*") -> dict[str, Any]:
51
52
params = {"apiKey" : self .api_key , "fields" : fields }
52
53
return await self ._get (APIV2 + "/users/me/pods" , params )
53
54
54
- async def async_get_devices_data (self ) -> SensiboData :
55
+ async def async_get_devices_data (self ) -> SensiboData : # noqa: C901
55
56
"""Return dataclass with Sensibo Devices."""
56
57
devices : list [dict [str , Any ]] = []
57
58
data = await self .async_get_devices ()
58
59
if "result" not in data :
59
60
LOGGER .warning ("No result in data from devices" )
60
61
LOGGER .debug ("Data without result: %s" , data )
61
62
raise SensiboError ("No result in data" )
62
- for device in data ["result" ]:
63
- devices .append (device )
63
+ devices = list (data ["result" ])
64
64
65
65
device_data : dict [str , SensiboDevice ] = {}
66
66
dev : dict [str , Any ]
@@ -126,14 +126,14 @@ async def async_get_devices_data(self) -> SensiboData:
126
126
ac_states .get ("mode" ), {}
127
127
)
128
128
fan_modes : list [str ] | None = current_capabilities .get ("fanLevels" )
129
- fan_modes_translated : dict | None = None
129
+ fan_modes_translated : dict [ str , str ] | None = None
130
130
if fan_modes :
131
131
fan_modes_translated = {
132
132
_fan_mode .lower (): _fan_mode for _fan_mode in fan_modes
133
133
}
134
134
fan_modes = [_fan_mode .lower () for _fan_mode in fan_modes ]
135
135
swing_modes : list [str ] | None = current_capabilities .get ("swing" )
136
- swing_modes_translated : dict | None = None
136
+ swing_modes_translated : dict [ str , str ] | None = None
137
137
if swing_modes :
138
138
swing_modes_translated = {
139
139
_swing_mode .lower (): _swing_mode for _swing_mode in swing_modes
@@ -142,7 +142,7 @@ async def async_get_devices_data(self) -> SensiboData:
142
142
horizontal_swing_modes : list [str ] | None = current_capabilities .get (
143
143
"horizontalSwing"
144
144
)
145
- horizontal_swing_modes_translated : dict | None = None
145
+ horizontal_swing_modes_translated : dict [ str , str ] | None = None
146
146
if horizontal_swing_modes :
147
147
horizontal_swing_modes_translated = {
148
148
_horizontal_mode .lower (): _horizontal_mode
@@ -153,7 +153,7 @@ async def async_get_devices_data(self) -> SensiboData:
153
153
for _horizontal_mode in horizontal_swing_modes
154
154
]
155
155
light_modes : list [str ] | None = current_capabilities .get ("light" )
156
- light_modes_translated : dict | None = None
156
+ light_modes_translated : dict [ str , str ] | None = None
157
157
if light_modes :
158
158
light_modes_translated = {
159
159
_light_mode .lower (): _light_mode for _light_mode in light_modes
@@ -170,8 +170,7 @@ async def async_get_devices_data(self) -> SensiboData:
170
170
if temperatures_list :
171
171
diff = MAX_POSSIBLE_STEP
172
172
for i in range (len (temperatures_list ) - 1 ):
173
- if temperatures_list [i + 1 ] - temperatures_list [i ] < diff :
174
- diff = temperatures_list [i + 1 ] - temperatures_list [i ]
173
+ diff = min (diff , temperatures_list [i + 1 ] - temperatures_list [i ])
175
174
temperature_step = diff
176
175
177
176
active_features = list (ac_states )
@@ -298,10 +297,10 @@ async def async_get_devices_data(self) -> SensiboData:
298
297
smart_type = smart_type .lower ()
299
298
smart_low_temp_threshold = smart .get ("lowTemperatureThreshold" )
300
299
smart_high_temp_threshold = smart .get ("highTemperatureThreshold" )
301
- _smart_low_state : dict [str | Any ] = smart .get ("lowTemperatureState" , {})
302
- _smart_high_state : dict [str | Any ] = smart .get ("highTemperatureState" , {})
303
- smart_low_state : dict [str | Any ] = {}
304
- smart_high_state : dict [str | Any ] = {}
300
+ _smart_low_state : dict [str , Any ] = smart .get ("lowTemperatureState" , {})
301
+ _smart_high_state : dict [str , Any ] = smart .get ("highTemperatureState" , {})
302
+ smart_low_state : dict [str , Any ] = {}
303
+ smart_high_state : dict [str , Any ] = {}
305
304
if _smart_low_state :
306
305
for key , value in _smart_low_state .items ():
307
306
smart_low_state [key .lower ()] = (
@@ -615,12 +614,12 @@ async def _get(
615
614
path , params = params , timeout = self .timeout
616
615
) as resp :
617
616
return await self ._response (resp )
618
- except Exception as error :
617
+ except Exception :
619
618
LOGGER .debug ("Retry %d on path %s" , 4 - retry , path )
620
619
if retry > 0 :
621
620
await asyncio .sleep (7 )
622
621
return await self ._get (path , params , retry - 1 )
623
- raise error
622
+ raise
624
623
625
624
async def _put (
626
625
self ,
@@ -635,11 +634,11 @@ async def _put(
635
634
path , params = params , data = json .dumps (data ), timeout = self .timeout
636
635
) as resp :
637
636
return await self ._response (resp )
638
- except Exception as error :
637
+ except Exception :
639
638
if retry is False :
640
639
await asyncio .sleep (5 )
641
640
return await self ._put (path , params , data , True )
642
- raise error
641
+ raise
643
642
644
643
async def _post (
645
644
self ,
@@ -655,11 +654,11 @@ async def _post(
655
654
path , params = params , data = json .dumps (data ), timeout = self .timeout
656
655
) as resp :
657
656
return await self ._response (resp )
658
- except Exception as error :
657
+ except Exception :
659
658
if retry is False :
660
659
await asyncio .sleep (5 )
661
660
return await self ._post (path , params , data , True )
662
- raise error
661
+ raise
663
662
664
663
async def _patch (
665
664
self ,
@@ -675,11 +674,11 @@ async def _patch(
675
674
path , params = params , data = json .dumps (data ), timeout = self .timeout
676
675
) as resp :
677
676
return await self ._response (resp )
678
- except Exception as error :
677
+ except Exception :
679
678
if retry is False :
680
679
await asyncio .sleep (5 )
681
680
return await self ._patch (path , params , data , True )
682
- raise error
681
+ raise
683
682
684
683
async def _delete (
685
684
self , path : str , params : dict [str , Any ], retry : bool = False
@@ -691,11 +690,11 @@ async def _delete(
691
690
path , params = params , timeout = self .timeout
692
691
) as resp :
693
692
return await self ._response (resp )
694
- except Exception as error :
693
+ except Exception :
695
694
if retry is False :
696
695
await asyncio .sleep (5 )
697
696
return await self ._delete (path , params , True )
698
- raise error
697
+ raise
699
698
700
699
async def _response (self , resp : ClientResponse ) -> dict [str , Any ]:
701
700
"""Return response from call."""
0 commit comments