Skip to content

Commit c9bc062

Browse files
authored
Add a context manager to TahomaClient (#16)
1 parent fa8c443 commit c9bc062

File tree

3 files changed

+38
-49
lines changed

3 files changed

+38
-49
lines changed

.github/workflows/main.yaml

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,9 @@ on:
99
- master
1010

1111
jobs:
12-
tests:
13-
name: "Python ${{ matrix.python-version }}"
14-
runs-on: "ubuntu-latest"
15-
16-
strategy:
17-
matrix:
18-
python-version: ["3.6", "3.7", "3.8"]
19-
12+
pre-commit:
13+
runs-on: ubuntu-latest
2014
steps:
21-
- uses: "actions/checkout@v2"
22-
- uses: "actions/setup-python@v1"
23-
with:
24-
python-version: "${{ matrix.python-version }}"
25-
26-
- name: Install and set up Poetry
27-
run: |
28-
curl -o get-poetry.py https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py
29-
python get-poetry.py -y
30-
31-
- name: "Install tox"
32-
run: |
33-
python -m pip install --upgrade tox tox-gh-actions
34-
- name: "Run tox targets for ${{ matrix.python-version }}"
35-
run: |
36-
source $HOME/.poetry/env
37-
python -m tox
15+
- uses: actions/checkout@v1
16+
- uses: actions/setup-python@v1
17+
- uses: pre-commit/action@v2.0.0

tahoma_api/client.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
""" Python wrapper for the Tahoma API """
2+
from __future__ import annotations
3+
24
import urllib.parse
3-
from typing import Any, Dict, List, Optional, Union
5+
from types import TracebackType
6+
from typing import Any, Dict, List, Optional, Type, Union
47

58
import aiohttp
69
import humps
@@ -34,6 +37,17 @@ def __init__(self, username: str, password: str, api_url: str = API_URL) -> None
3437

3538
self.session = aiohttp.ClientSession()
3639

40+
async def __aenter__(self) -> TahomaClient:
41+
return self
42+
43+
async def __aexit__(
44+
self,
45+
exc_type: Optional[Type[BaseException]],
46+
exc_value: Optional[BaseException],
47+
traceback: Optional[TracebackType],
48+
) -> None:
49+
await self.close()
50+
3751
async def close(self) -> None:
3852
"""Close the session."""
3953
await self.session.close()

test.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,34 @@
33

44
from tahoma_api.client import TahomaClient
55

6-
# TODO use .env file
76
USERNAME = ""
87
PASSWORD = ""
98

109

1110
async def main() -> None:
12-
client = TahomaClient(USERNAME, PASSWORD)
11+
async with TahomaClient(USERNAME, PASSWORD) as client:
12+
try:
13+
await client.login()
14+
except Exception as exception: # pylint: disable=broad-except
15+
print(exception)
16+
return await client.close()
1317

14-
try:
15-
await client.login()
16-
except Exception as exception: # pylint: disable=broad-except
17-
print(exception)
18-
return await client.close()
18+
devices = await client.get_devices()
1919

20-
devices = await client.get_devices()
20+
for device in devices:
21+
print(f"{device.label} ({device.id}) - {device.controllable_name}")
22+
print(f"{device.widget} - {device.ui_class}")
23+
# print(device.states)
24+
# print(device.definition)
2125

22-
for device in devices:
23-
print(f"{device.label} ({device.id}) - {device.controllable_name}")
24-
print(f"{device.widget} - {device.ui_class}")
25-
# print(device.states)
26-
# print(device.definition)
26+
# Create an event listener and poll it
27+
listener_id = await client.register_event_listener()
2728

28-
# Create an event listener and poll it
29-
listener_id = await client.register_event_listener()
29+
while True:
30+
events = await client.fetch_event_listener(listener_id)
31+
print(events)
3032

31-
while True:
32-
events = await client.fetch_event_listener(listener_id)
33-
print(events)
34-
35-
time.sleep(2)
36-
37-
# Make sure you call client.close() when you are done
38-
await client.close()
33+
time.sleep(2)
3934

4035

4136
asyncio.run(main())

0 commit comments

Comments
 (0)