Skip to content

Commit d9adbd6

Browse files
committed
✨ Add GET/PUT device
1 parent e1ae79d commit d9adbd6

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ The client currently supports the following methods:
1414
- `get_site_consumption_breakdown_time_ago(self, site_id, aggregation_level = "HOUR", time_ago_unit = "DAY", time_ago_value = 1)` Retrieves the consumption breakdown data for a specific site, based on the provided parameters.
1515
- `get_devices(self, site_id)`: Retrieves a list of devices for the specified site.
1616
- `get_device_ts_time_ago(self, device_id, measure_kind = "FLOW", aggregation_level = "HOUR", aggregation_type = "MAX", time_ago_unit = "DAY", time_ago_value = "1")`: Retrieves the time series data for a specific device, based on the provided parameters.
17-
17+
- `get_device(self, device_id)`: Retrieves information about a specific device.
18+
- `put_device(self, device_id, payload)`: Updates a specific device with the provided payload.
1819

1920
## Installation
2021
You can install the Comwatt Python Client using pip. Run the following command:
@@ -58,6 +59,11 @@ print(devices)
5859
# Get time series data for a specific device
5960
time_series_data = client.get_device_ts_time_ago(devices[0]['id'])
6061
print(time_series_data)
62+
63+
# Set the control mode of a specific device to MANUL
64+
device = client.get_device(devices[0]['id'])
65+
device['configuration']['controlMode'] = 'MANUAL'
66+
client.put_device(device['id'], device)
6167
```
6268

6369
Make sure to replace `'username'`, `'password'` with the actual values for your Comwatt account.

comwatt_client/client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,46 @@ def get_devices(self, site_id):
187187
else:
188188
raise Exception(f'Error retrieving sites: {response.status_code}')
189189

190+
def get_device(self, device_id):
191+
"""
192+
Retrieves information about a specific device.
193+
194+
Args:
195+
device_id (str): The ID of the device.
196+
197+
Returns:
198+
dict: A dictionary containing the device information.
199+
200+
"""
201+
url = f'{self.base_url}/devices/{device_id}'
202+
203+
response = self.session.get(url)
204+
if response.status_code == 200:
205+
return response.json()
206+
else:
207+
raise Exception(f'Error retrieving device {device_id}: {response.status_code}')
208+
209+
def put_device(self, device_id, payload):
210+
"""
211+
Updates a specific device with the provided payload.
212+
213+
Args:
214+
device_id (str): The ID of the device.
215+
payload (dict): The payload to update the device.
216+
217+
Returns:
218+
dict: A dictionary containing the response from the API.
219+
220+
"""
221+
url = f'{self.base_url}/devices/{device_id}'
222+
223+
response = self.session.put(url, json=payload)
224+
if response.status_code == 200:
225+
return response.json()
226+
else:
227+
raise Exception(f'Error retrieving sites: {response.status_code}')
228+
229+
190230
def get_device_ts_time_ago(self, device_id,
191231
measure_kind = "FLOW",
192232
aggregation_level = "HOUR",

0 commit comments

Comments
 (0)