summaryrefslogtreecommitdiff
path: root/python-geniushub-client.spec
blob: cb09c8075c1c9091e86c0971c3b85cc04bda602a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
%global _empty_manifest_terminate_build 0
Name:		python-geniushub-client
Version:	0.7.0
Release:	1
Summary:	An aiohttp-based client for Genius Hub systems
License:	MIT
URL:		https://github.com/manzanotti/geniushub-client
Source0:	https://mirrors.aliyun.com/pypi/web/packages/1c/53/d2d6039613284865d9e938a1dbeafe61bc7dc472072d49353013475f2a8b/geniushub-client-0.7.0.tar.gz
BuildArch:	noarch

Requires:	python3-aiohttp

%description
[![CircleCI](https://circleci.com/gh/manzanotti/geniushub-client.svg?style=svg)](https://circleci.com/gh/manzanotti/geniushub-client) [![Join the chat at https://gitter.im/geniushub-client/community](https://badges.gitter.im/geniushub-client/community.svg)](https://gitter.im/geniushub-client/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/geniushub-client)

# geniushub-client
This is a Python library to provide access to a **Genius Hub** by abstracting its [RESTful API](https://my.geniushub.co.uk/docs). It uses **aiohttp** and is therefore async-friendly.

This library can use either the **_offical_ v1 API** with a [hub token](https://my.geniushub.co.uk/tokens), or the **_latest_ v3 API** (using your own [username and password](https://www.geniushub.co.uk/app)). In either case, the library will return v1-compatible results wherever possible (this is not a trivial task).

If you use the v3 API, you can interrogate the hub directly, rather than via Heat Genius' own servers. Note that the v3 API is undocumented, and so this functionality may break at any time. In fact, the v3 to v1 conversion if best efforts and may even be broken as is for some edge cases - it was tested with HW, on/off (i.e. smart plugs), and radiators only.

It is a WIP, and may be missing some functionality. In addition, there are some other limitations (see below).

It is based upon work by [@GeoffAtHome](https://github.com/manzanotti/geniushub-client/commits?author=GeoffAtHome) and [@zxdavb]](https://github.com/manzanotti/geniushub-client/commits?author=zxdavb) - thanks!

## Current limitations
Current limitations & to-dos include:
 - **ghclient.py** is not complete
 - schedules are read-only
 - when using the v3 API, zones sometimes have the wrong value for `occupied`

 The library will return v1 API responses wherever possible, however:
  1. the only code available to reverse-engineer is from the web app, but
  2. the Web app does not correlate completely with the v1 API (e.g. issue messages, occupied state)

Thus, always check your output against the corresponding v1 API response rather than the web app.

## Installation
Either clone this repository and run `python setup.py install`, or install from pip using `pip install geniushub-client`.

## Using the Library
See `ghclient.py` for example code. You can also use `ghclient.py` for ad-hoc queries:
```bash
python ghclient.py -?
```
There are two distinct options for accessing a Genius Hub:

Option 1: **hub token** only:
  - requires a hub token obtained from https://my.geniushub.co.uk/tokens
  - uses the v1 API - which is well-documented
  - interrogates Heat Genius' own servers (so is slower)

Option 2: hub **hostname/address** with **user credentials**:
  - requires your `username` & `password`, as used with https://www.geniushub.co.uk/app
  - uses the v3 API - results are WIP and may not be what you expect
  - interrogates the hub directly (so is faster), via port :1223

```bash
HUB_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsInZlc..."
HUB_ADDRESS="my-geniushub.dyndns.com"
USERNAME="my-username"
PASSWORD="my-password"

python ghclient.py ${HUB_TOKEN} issues

python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} zones -v
```

You can compare any output to the 'official' API (v1 response):
```bash
curl -H "authorization: Bearer ${HUB_TOKEN}" -X GET https://my.geniushub.co.uk/v1/zones/summary
python ghclient.py ${HUB_TOKEN} zones

curl -H "authorization: Bearer ${HUB_TOKEN}" -X GET https://my.geniushub.co.uk/v1/devices
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} devices -v

curl -H "authorization: Bearer ${HUB_TOKEN}" -X GET https://my.geniushub.co.uk/v1/issues
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} issues
```

You can obtain the 'raw' v3 API responses (i.e. the JSON is not converted to the v1 schema):
```bash
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} zones -vvv
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} devices -vvv
```

To obtain the 'official' v3 API responses takes a little work.  First, use python to obtain a `HASH`:
```python
>>> from hashlib import sha256
>>> hash = sha256()
>>> hash.update(("my_username" + "my_password").encode('utf-8'))
>>> print(hash.hexdigest())
001b24f45b...
```
Then you can use **curl**:
```bash
curl --user ${USERNAME}:${HASH} -X GET http://${HUB_ADDRESS}:1223/v3/zones
```

## Advanced Features
 When used as a library, there is the option to utilize the referencing module's own `aiohttp.ClientSession()` (recommended).

 Here is an example, but see **ghclient.py** for a more complete example:
 ```python
import asyncio
import aiohttp
from geniushubclient import GeniusHub

my_session = aiohttp.ClientSession()

...

if not (username or password):
    hub = GeniusHub(hub_id=hub_address, username=username, password=password, session=my_session)
else:
    hub = GeniusHub(hub_id=hub_token, session=my_session)

await hub.update()  # enumerate all zones, devices and issues

hub.verbosity = 0  # same as v1/zones/summary, v1/devices/summary
print(hub.zones)

hub.verbosity = 1  # default, same as v1/zones, v1/devices, v1/issues
print(hub.devices)

print(hub.zone_by_id[3].data["temperature"])
print(hub.device_by_id["2-2"].data)

await my_session.close()
```

### Unit tests

Please see the README.md file in the tests folder for more details on unit tests protocol.

### QA/CI via CircleCI
QA includes comparing JSON from **cURL** with output from this app using **diff**, for example:
```bash
(venv) root@hostname:~/$ curl -X GET https://my.geniushub.co.uk/v1/zones -H "authorization: Bearer ${HUB_TOKEN}" | \
    python -c "import sys, json; print(json.dumps(json.load(sys.stdin, parse_float=lambda x: int(float(x))), indent=4, sort_keys=True))" > a.out

(venv) root@hostname:~/$ python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} zones -v | \
    python -c "import sys, json; print(json.dumps(json.load(sys.stdin, parse_float=lambda x: int(float(x))), indent=4, sort_keys=True))" > b.out

(venv) root@hostname:~/$ diff a.out b.out
```


%package -n python3-geniushub-client
Summary:	An aiohttp-based client for Genius Hub systems
Provides:	python-geniushub-client
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-geniushub-client
[![CircleCI](https://circleci.com/gh/manzanotti/geniushub-client.svg?style=svg)](https://circleci.com/gh/manzanotti/geniushub-client) [![Join the chat at https://gitter.im/geniushub-client/community](https://badges.gitter.im/geniushub-client/community.svg)](https://gitter.im/geniushub-client/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/geniushub-client)

# geniushub-client
This is a Python library to provide access to a **Genius Hub** by abstracting its [RESTful API](https://my.geniushub.co.uk/docs). It uses **aiohttp** and is therefore async-friendly.

This library can use either the **_offical_ v1 API** with a [hub token](https://my.geniushub.co.uk/tokens), or the **_latest_ v3 API** (using your own [username and password](https://www.geniushub.co.uk/app)). In either case, the library will return v1-compatible results wherever possible (this is not a trivial task).

If you use the v3 API, you can interrogate the hub directly, rather than via Heat Genius' own servers. Note that the v3 API is undocumented, and so this functionality may break at any time. In fact, the v3 to v1 conversion if best efforts and may even be broken as is for some edge cases - it was tested with HW, on/off (i.e. smart plugs), and radiators only.

It is a WIP, and may be missing some functionality. In addition, there are some other limitations (see below).

It is based upon work by [@GeoffAtHome](https://github.com/manzanotti/geniushub-client/commits?author=GeoffAtHome) and [@zxdavb]](https://github.com/manzanotti/geniushub-client/commits?author=zxdavb) - thanks!

## Current limitations
Current limitations & to-dos include:
 - **ghclient.py** is not complete
 - schedules are read-only
 - when using the v3 API, zones sometimes have the wrong value for `occupied`

 The library will return v1 API responses wherever possible, however:
  1. the only code available to reverse-engineer is from the web app, but
  2. the Web app does not correlate completely with the v1 API (e.g. issue messages, occupied state)

Thus, always check your output against the corresponding v1 API response rather than the web app.

## Installation
Either clone this repository and run `python setup.py install`, or install from pip using `pip install geniushub-client`.

## Using the Library
See `ghclient.py` for example code. You can also use `ghclient.py` for ad-hoc queries:
```bash
python ghclient.py -?
```
There are two distinct options for accessing a Genius Hub:

Option 1: **hub token** only:
  - requires a hub token obtained from https://my.geniushub.co.uk/tokens
  - uses the v1 API - which is well-documented
  - interrogates Heat Genius' own servers (so is slower)

Option 2: hub **hostname/address** with **user credentials**:
  - requires your `username` & `password`, as used with https://www.geniushub.co.uk/app
  - uses the v3 API - results are WIP and may not be what you expect
  - interrogates the hub directly (so is faster), via port :1223

```bash
HUB_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsInZlc..."
HUB_ADDRESS="my-geniushub.dyndns.com"
USERNAME="my-username"
PASSWORD="my-password"

python ghclient.py ${HUB_TOKEN} issues

python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} zones -v
```

You can compare any output to the 'official' API (v1 response):
```bash
curl -H "authorization: Bearer ${HUB_TOKEN}" -X GET https://my.geniushub.co.uk/v1/zones/summary
python ghclient.py ${HUB_TOKEN} zones

curl -H "authorization: Bearer ${HUB_TOKEN}" -X GET https://my.geniushub.co.uk/v1/devices
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} devices -v

curl -H "authorization: Bearer ${HUB_TOKEN}" -X GET https://my.geniushub.co.uk/v1/issues
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} issues
```

You can obtain the 'raw' v3 API responses (i.e. the JSON is not converted to the v1 schema):
```bash
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} zones -vvv
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} devices -vvv
```

To obtain the 'official' v3 API responses takes a little work.  First, use python to obtain a `HASH`:
```python
>>> from hashlib import sha256
>>> hash = sha256()
>>> hash.update(("my_username" + "my_password").encode('utf-8'))
>>> print(hash.hexdigest())
001b24f45b...
```
Then you can use **curl**:
```bash
curl --user ${USERNAME}:${HASH} -X GET http://${HUB_ADDRESS}:1223/v3/zones
```

## Advanced Features
 When used as a library, there is the option to utilize the referencing module's own `aiohttp.ClientSession()` (recommended).

 Here is an example, but see **ghclient.py** for a more complete example:
 ```python
import asyncio
import aiohttp
from geniushubclient import GeniusHub

my_session = aiohttp.ClientSession()

...

if not (username or password):
    hub = GeniusHub(hub_id=hub_address, username=username, password=password, session=my_session)
else:
    hub = GeniusHub(hub_id=hub_token, session=my_session)

await hub.update()  # enumerate all zones, devices and issues

hub.verbosity = 0  # same as v1/zones/summary, v1/devices/summary
print(hub.zones)

hub.verbosity = 1  # default, same as v1/zones, v1/devices, v1/issues
print(hub.devices)

print(hub.zone_by_id[3].data["temperature"])
print(hub.device_by_id["2-2"].data)

await my_session.close()
```

### Unit tests

Please see the README.md file in the tests folder for more details on unit tests protocol.

### QA/CI via CircleCI
QA includes comparing JSON from **cURL** with output from this app using **diff**, for example:
```bash
(venv) root@hostname:~/$ curl -X GET https://my.geniushub.co.uk/v1/zones -H "authorization: Bearer ${HUB_TOKEN}" | \
    python -c "import sys, json; print(json.dumps(json.load(sys.stdin, parse_float=lambda x: int(float(x))), indent=4, sort_keys=True))" > a.out

(venv) root@hostname:~/$ python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} zones -v | \
    python -c "import sys, json; print(json.dumps(json.load(sys.stdin, parse_float=lambda x: int(float(x))), indent=4, sort_keys=True))" > b.out

(venv) root@hostname:~/$ diff a.out b.out
```


%package help
Summary:	Development documents and examples for geniushub-client
Provides:	python3-geniushub-client-doc
%description help
[![CircleCI](https://circleci.com/gh/manzanotti/geniushub-client.svg?style=svg)](https://circleci.com/gh/manzanotti/geniushub-client) [![Join the chat at https://gitter.im/geniushub-client/community](https://badges.gitter.im/geniushub-client/community.svg)](https://gitter.im/geniushub-client/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/geniushub-client)

# geniushub-client
This is a Python library to provide access to a **Genius Hub** by abstracting its [RESTful API](https://my.geniushub.co.uk/docs). It uses **aiohttp** and is therefore async-friendly.

This library can use either the **_offical_ v1 API** with a [hub token](https://my.geniushub.co.uk/tokens), or the **_latest_ v3 API** (using your own [username and password](https://www.geniushub.co.uk/app)). In either case, the library will return v1-compatible results wherever possible (this is not a trivial task).

If you use the v3 API, you can interrogate the hub directly, rather than via Heat Genius' own servers. Note that the v3 API is undocumented, and so this functionality may break at any time. In fact, the v3 to v1 conversion if best efforts and may even be broken as is for some edge cases - it was tested with HW, on/off (i.e. smart plugs), and radiators only.

It is a WIP, and may be missing some functionality. In addition, there are some other limitations (see below).

It is based upon work by [@GeoffAtHome](https://github.com/manzanotti/geniushub-client/commits?author=GeoffAtHome) and [@zxdavb]](https://github.com/manzanotti/geniushub-client/commits?author=zxdavb) - thanks!

## Current limitations
Current limitations & to-dos include:
 - **ghclient.py** is not complete
 - schedules are read-only
 - when using the v3 API, zones sometimes have the wrong value for `occupied`

 The library will return v1 API responses wherever possible, however:
  1. the only code available to reverse-engineer is from the web app, but
  2. the Web app does not correlate completely with the v1 API (e.g. issue messages, occupied state)

Thus, always check your output against the corresponding v1 API response rather than the web app.

## Installation
Either clone this repository and run `python setup.py install`, or install from pip using `pip install geniushub-client`.

## Using the Library
See `ghclient.py` for example code. You can also use `ghclient.py` for ad-hoc queries:
```bash
python ghclient.py -?
```
There are two distinct options for accessing a Genius Hub:

Option 1: **hub token** only:
  - requires a hub token obtained from https://my.geniushub.co.uk/tokens
  - uses the v1 API - which is well-documented
  - interrogates Heat Genius' own servers (so is slower)

Option 2: hub **hostname/address** with **user credentials**:
  - requires your `username` & `password`, as used with https://www.geniushub.co.uk/app
  - uses the v3 API - results are WIP and may not be what you expect
  - interrogates the hub directly (so is faster), via port :1223

```bash
HUB_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsInZlc..."
HUB_ADDRESS="my-geniushub.dyndns.com"
USERNAME="my-username"
PASSWORD="my-password"

python ghclient.py ${HUB_TOKEN} issues

python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} zones -v
```

You can compare any output to the 'official' API (v1 response):
```bash
curl -H "authorization: Bearer ${HUB_TOKEN}" -X GET https://my.geniushub.co.uk/v1/zones/summary
python ghclient.py ${HUB_TOKEN} zones

curl -H "authorization: Bearer ${HUB_TOKEN}" -X GET https://my.geniushub.co.uk/v1/devices
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} devices -v

curl -H "authorization: Bearer ${HUB_TOKEN}" -X GET https://my.geniushub.co.uk/v1/issues
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} issues
```

You can obtain the 'raw' v3 API responses (i.e. the JSON is not converted to the v1 schema):
```bash
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} zones -vvv
python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} devices -vvv
```

To obtain the 'official' v3 API responses takes a little work.  First, use python to obtain a `HASH`:
```python
>>> from hashlib import sha256
>>> hash = sha256()
>>> hash.update(("my_username" + "my_password").encode('utf-8'))
>>> print(hash.hexdigest())
001b24f45b...
```
Then you can use **curl**:
```bash
curl --user ${USERNAME}:${HASH} -X GET http://${HUB_ADDRESS}:1223/v3/zones
```

## Advanced Features
 When used as a library, there is the option to utilize the referencing module's own `aiohttp.ClientSession()` (recommended).

 Here is an example, but see **ghclient.py** for a more complete example:
 ```python
import asyncio
import aiohttp
from geniushubclient import GeniusHub

my_session = aiohttp.ClientSession()

...

if not (username or password):
    hub = GeniusHub(hub_id=hub_address, username=username, password=password, session=my_session)
else:
    hub = GeniusHub(hub_id=hub_token, session=my_session)

await hub.update()  # enumerate all zones, devices and issues

hub.verbosity = 0  # same as v1/zones/summary, v1/devices/summary
print(hub.zones)

hub.verbosity = 1  # default, same as v1/zones, v1/devices, v1/issues
print(hub.devices)

print(hub.zone_by_id[3].data["temperature"])
print(hub.device_by_id["2-2"].data)

await my_session.close()
```

### Unit tests

Please see the README.md file in the tests folder for more details on unit tests protocol.

### QA/CI via CircleCI
QA includes comparing JSON from **cURL** with output from this app using **diff**, for example:
```bash
(venv) root@hostname:~/$ curl -X GET https://my.geniushub.co.uk/v1/zones -H "authorization: Bearer ${HUB_TOKEN}" | \
    python -c "import sys, json; print(json.dumps(json.load(sys.stdin, parse_float=lambda x: int(float(x))), indent=4, sort_keys=True))" > a.out

(venv) root@hostname:~/$ python ghclient.py ${HUB_ADDRESS} -u ${USERNAME} -p ${PASSWORD} zones -v | \
    python -c "import sys, json; print(json.dumps(json.load(sys.stdin, parse_float=lambda x: int(float(x))), indent=4, sort_keys=True))" > b.out

(venv) root@hostname:~/$ diff a.out b.out
```


%prep
%autosetup -n geniushub-client-0.7.0

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "\"/%h/%f.gz\"\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-geniushub-client -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Fri Jun 09 2023 Python_Bot <Python_Bot@openeuler.org> - 0.7.0-1
- Package Spec generated