1
1
"""Ariston module"""
2
+ import asyncio
2
3
import logging
3
- from asyncio import gather
4
+ from typing import Any , Optional
4
5
5
- from .ariston import (
6
- AristonAPI ,
6
+ from .ariston_api import AristonAPI , ConnectionException
7
+ from . const import (
7
8
DeviceAttribute ,
8
9
SystemType ,
9
10
VelisDeviceAttribute ,
10
11
WheType ,
12
+ PlantMode ,
13
+ ZoneMode ,
14
+ ConsumptionProperties ,
15
+ ConsumptionType ,
16
+ CustomDeviceFeatures ,
17
+ MedDeviceSettings ,
18
+ VelisDeviceProperties ,
19
+ EvoDeviceProperties ,
20
+ DeviceProperties ,
21
+ DeviceFeatures ,
22
+ ThermostatProperties ,
11
23
)
12
24
from .evo_device import AristonEvoDevice
13
25
from .galevo_device import AristonGalevoDevice
14
26
from .lydos_hybrid_device import AristonLydosHybridDevice
27
+ from .velis_device import AristonVelisDevice
28
+ from .device import AristonDevice
15
29
16
30
_LOGGER = logging .getLogger (__name__ )
17
31
@@ -21,70 +35,153 @@ class Ariston:
21
35
22
36
def __init__ (self ) -> None :
23
37
self .api = None
24
- self .cloud_devices : list [dict ] = []
38
+ self .cloud_devices : list [dict [ str , Any ] ] = []
25
39
26
40
async def async_connect (self , username : str , password : str ) -> bool :
27
41
"""Connect to the ariston cloud"""
28
42
self .api = AristonAPI (username , password )
29
43
return await self .api .async_connect ()
30
44
31
- async def async_discover (self ):
45
+ async def async_discover (self ) -> Optional [ list [ dict [ str , Any ]]] :
32
46
"""Retreive ariston devices from the cloud"""
33
47
if self .api is None :
34
48
_LOGGER .exception ("Call async_connect first" )
35
- return None
36
- cloud_devices : list [dict ] = []
37
- cloud_devices_tuple = await gather (self .api .async_get_detailed_devices (), self .api .async_get_detailed_velis_devices ())
38
-
39
- for devices in cloud_devices_tuple :
40
- cloud_devices .extend (devices )
41
-
49
+ return None
50
+ cloud_devices = await _async_discover (self .api )
42
51
self .cloud_devices = cloud_devices
43
52
return cloud_devices
44
53
45
- async def async_hello (self , gateway : str , is_metric = True , language_tag = "en-US" ):
54
+ async def async_hello (
55
+ self , gateway : str , is_metric : bool = True , language_tag : str = "en-US"
56
+ ) -> Optional [AristonDevice ]:
46
57
"""Get ariston device"""
47
58
if self .api is None :
48
59
_LOGGER .exception ("Call async_connect() first" )
49
- return
60
+ return None
50
61
51
62
if len (self .cloud_devices ) == 0 :
52
63
await self .async_discover ()
53
64
54
- device = next (
55
- (
56
- dev
57
- for dev in self .cloud_devices
58
- if dev .get (DeviceAttribute .GW ) == gateway
59
- ),
60
- None ,
65
+ return _get_device (
66
+ self .cloud_devices , self .api , gateway , is_metric , language_tag
61
67
)
62
- if device is None :
63
- _LOGGER .exception (f'No device "{ gateway } " found.' )
64
- return None
65
68
66
- system_type = device .get (DeviceAttribute .SYS )
67
- if system_type == SystemType .GALEVO :
68
- return AristonGalevoDevice (
69
- self .api ,
69
+
70
+ def _get_device (
71
+ cloud_devices : list [dict [str , Any ]],
72
+ api : AristonAPI ,
73
+ gateway : str ,
74
+ is_metric : bool = True ,
75
+ language_tag : str = "en-US" ,
76
+ ) -> Optional [AristonDevice ]:
77
+ """Get ariston device"""
78
+ device = next (
79
+ (dev for dev in cloud_devices if dev .get (DeviceAttribute .GW ) == gateway ),
80
+ None ,
81
+ )
82
+ if device is None :
83
+ _LOGGER .exception (f'No device "{ gateway } " found.' )
84
+ return None
85
+
86
+ system_type = device .get (DeviceAttribute .SYS )
87
+ if system_type == SystemType .GALEVO :
88
+ return AristonGalevoDevice (
89
+ api ,
90
+ device ,
91
+ is_metric ,
92
+ language_tag ,
93
+ )
94
+ if system_type == SystemType .VELIS :
95
+ whe_type = device .get (VelisDeviceAttribute .WHE_TYPE )
96
+ if whe_type == WheType .LydosHybrid :
97
+ return AristonLydosHybridDevice (
98
+ api ,
70
99
device ,
71
- is_metric ,
72
- language_tag ,
73
100
)
74
- if system_type == SystemType .VELIS :
75
- whe_type = device .get (VelisDeviceAttribute .WHE_TYPE )
76
- if whe_type == WheType .LydosHybrid :
77
- return AristonLydosHybridDevice (
78
- self .api ,
79
- device ,
80
- )
81
- if whe_type == WheType .Evo :
82
- return AristonEvoDevice (
83
- self .api ,
84
- device ,
85
- )
86
- _LOGGER .exception (f"Unsupported whe type { whe_type } " )
87
- return None
88
-
89
- _LOGGER .exception (f"Unsupported system type { system_type } " )
101
+ if whe_type == WheType .Evo :
102
+ return AristonEvoDevice (
103
+ api ,
104
+ device ,
105
+ )
106
+ _LOGGER .exception (f"Unsupported whe type { whe_type } " )
90
107
return None
108
+
109
+ _LOGGER .exception (f"Unsupported system type { system_type } " )
110
+ return None
111
+
112
+
113
+ def _connect (username : str , password : str ) -> AristonAPI :
114
+ """Connect to ariston api"""
115
+ api = AristonAPI (username , password )
116
+ api .connect ()
117
+ return api
118
+
119
+
120
+ def _discover (api : AristonAPI ) -> list [dict [str , Any ]]:
121
+ """Retreive ariston devices from the cloud"""
122
+ cloud_devices : list [dict [str , Any ]] = []
123
+ cloud_devices .extend (api .get_detailed_devices ())
124
+ cloud_devices .extend (api .get_detailed_velis_devices ())
125
+
126
+ return cloud_devices
127
+
128
+
129
+ def discover (username : str , password : str ) -> list [dict [str , Any ]]:
130
+ """Retreive ariston devices from the cloud"""
131
+ api = _connect (username , password )
132
+ return _discover (api )
133
+
134
+
135
+ def hello (
136
+ username : str ,
137
+ password : str ,
138
+ gateway : str ,
139
+ is_metric : bool = True ,
140
+ language_tag : str = "en-US" ,
141
+ ) -> Optional [AristonDevice ]:
142
+ """Get ariston device"""
143
+ api = _connect (username , password )
144
+ cloud_devices = _discover (api )
145
+ return _get_device (cloud_devices , api , gateway , is_metric , language_tag )
146
+
147
+
148
+ async def _async_connect (username : str , password : str ) -> AristonAPI :
149
+ """Async connect to ariston api"""
150
+ api = AristonAPI (username , password )
151
+ if not await api .async_connect ():
152
+ raise ConnectionException
153
+ return api
154
+
155
+
156
+ async def _async_discover (api : AristonAPI ) -> list [dict [str , Any ]]:
157
+ """Async retreive ariston devices from the cloud"""
158
+ cloud_devices : list [dict [str , Any ]] = []
159
+ cloud_devices_tuple : tuple [
160
+ list [dict [str , Any ]], list [dict [str , Any ]]
161
+ ] = await asyncio .gather (
162
+ api .async_get_detailed_devices (), api .async_get_detailed_velis_devices ()
163
+ )
164
+
165
+ for devices in cloud_devices_tuple :
166
+ cloud_devices .extend (devices )
167
+
168
+ return cloud_devices
169
+
170
+
171
+ async def async_discover (username : str , password : str ) -> list [dict [str , Any ]]:
172
+ """Retreive ariston devices from the cloud"""
173
+ api = await _async_connect (username , password )
174
+ return await _async_discover (api )
175
+
176
+
177
+ async def async_hello (
178
+ username : str ,
179
+ password : str ,
180
+ gateway : str ,
181
+ is_metric : bool = True ,
182
+ language_tag : str = "en-US" ,
183
+ ) -> Optional [AristonDevice ]:
184
+ """Get ariston device"""
185
+ api = await _async_connect (username , password )
186
+ cloud_devices = await _async_discover (api )
187
+ return _get_device (cloud_devices , api , gateway , is_metric , language_tag )
0 commit comments