Skip to content

Commit 19e7d55

Browse files
committed
Add sync functions
1 parent f245593 commit 19e7d55

11 files changed

+1494
-670
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.vscode/

README.md

+60-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,35 @@ Use pip3 to install the latest version of this module.
1212
pip3 install ariston
1313
```
1414

15-
## Basic functions
15+
## The easy way (recommended for testing the module)
16+
First, open Python 3 and import ariston module.
17+
```
18+
python3
19+
```
20+
```python3
21+
import ariston
22+
```
23+
### Syncronous
24+
Discover devices if you dont know your gateway id. You can skip this step.
25+
```python3
26+
raw_devices = ariston.discover("username", "password")
27+
```
28+
For example the gateway id for your first device.
29+
```python3
30+
raw_devices[0]['gw']
31+
```
32+
Get your device
33+
```python3
34+
device = ariston.hello("username", "password", "gateway", is_metric, "location")
35+
```
36+
[Go use your device section](https://github.com/fustom/python-ariston-api/edit/main/README.md#Use-your-device)
37+
### Asyncronous
38+
```python3
39+
raw_devices = await ariston.async_discover("username", "password")
40+
device = await ariston.async_hello("username", "password", "gateway", is_metric, "location")
41+
```
42+
[Go use your device section](https://github.com/fustom/python-ariston-api/edit/main/README.md#Use-your-device)
43+
## The ariston class way (recommended for integrate the module)
1644
First, open Python 3 and import Ariston class from this module.
1745
```
1846
python3
@@ -26,7 +54,7 @@ ariston = Ariston()
2654
```
2755
Now let's try some functions
2856

29-
## Connect
57+
### Connect
3058
The cloud requests are asynchronous, so if you call them from a synchronous function or not even from function, you should use asyncio.
3159
```python3
3260
import asyncio
@@ -43,7 +71,7 @@ await ariston.async_connect("username", "password")
4371
- username: Your ariston cloud username.
4472
- password: Your ariston cloud password.
4573

46-
## Discovery
74+
### Discovery
4775
Use this function to discover devices. You can skip this step if you already know the gateway id.
4876

4977
Sync
@@ -69,3 +97,32 @@ device = await ariston.async_hello("gateway", is_metric, "location")
6997
- gateway: You can find the value in the returned discover dictionary name 'gw'
7098
- is_metric: Optional. True or False. True means metric, False means imperial. Only works with Galevo (Alteas One, Genus One, etc) system. Default is True.
7199
- language_tag: Optional. Check https://en.wikipedia.org/wiki/IETF_language_tag Only works with Galevo (Alteas One, Genus One, etc) system. Default is "en-US".
100+
101+
## Use your device
102+
### Get device features
103+
Sync
104+
```python3
105+
device.get_features()
106+
```
107+
Async
108+
```python3
109+
await device.async_get_features()
110+
```
111+
### Get device data
112+
Sync
113+
```python3
114+
device.update_state()
115+
```
116+
Async
117+
```python3
118+
await device.async_update_state()
119+
```
120+
### Get device energy
121+
Sync
122+
```python3
123+
device.update_energy()
124+
```
125+
Async
126+
```python3
127+
await device.async_update_energy()
128+
```

ariston/__init__.py

+143-46
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
"""Ariston module"""
2+
import asyncio
23
import logging
3-
from asyncio import gather
4+
from typing import Any, Optional
45

5-
from .ariston import (
6-
AristonAPI,
6+
from .ariston_api import AristonAPI, ConnectionException
7+
from .const import (
78
DeviceAttribute,
89
SystemType,
910
VelisDeviceAttribute,
1011
WheType,
12+
PlantMode,
13+
ZoneMode,
14+
ConsumptionProperties,
15+
ConsumptionType,
16+
CustomDeviceFeatures,
17+
MedDeviceSettings,
18+
VelisDeviceProperties,
19+
EvoDeviceProperties,
20+
DeviceProperties,
21+
DeviceFeatures,
22+
ThermostatProperties,
1123
)
1224
from .evo_device import AristonEvoDevice
1325
from .galevo_device import AristonGalevoDevice
1426
from .lydos_hybrid_device import AristonLydosHybridDevice
27+
from .velis_device import AristonVelisDevice
28+
from .device import AristonDevice
1529

1630
_LOGGER = logging.getLogger(__name__)
1731

@@ -21,70 +35,153 @@ class Ariston:
2135

2236
def __init__(self) -> None:
2337
self.api = None
24-
self.cloud_devices: list[dict] = []
38+
self.cloud_devices: list[dict[str, Any]] = []
2539

2640
async def async_connect(self, username: str, password: str) -> bool:
2741
"""Connect to the ariston cloud"""
2842
self.api = AristonAPI(username, password)
2943
return await self.api.async_connect()
3044

31-
async def async_discover(self):
45+
async def async_discover(self) -> Optional[list[dict[str, Any]]]:
3246
"""Retreive ariston devices from the cloud"""
3347
if self.api is None:
3448
_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)
4251
self.cloud_devices = cloud_devices
4352
return cloud_devices
4453

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]:
4657
"""Get ariston device"""
4758
if self.api is None:
4859
_LOGGER.exception("Call async_connect() first")
49-
return
60+
return None
5061

5162
if len(self.cloud_devices) == 0:
5263
await self.async_discover()
5364

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
6167
)
62-
if device is None:
63-
_LOGGER.exception(f'No device "{gateway}" found.')
64-
return None
6568

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,
7099
device,
71-
is_metric,
72-
language_tag,
73100
)
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}")
90107
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

Comments
 (0)