Skip to content

Commit fa8c443

Browse files
authored
Add more properties to Device (#15)
1 parent 3ebdd64 commit fa8c443

File tree

2 files changed

+105
-27
lines changed

2 files changed

+105
-27
lines changed

tahoma_api/client.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from aiohttp import ClientResponse
88

99
from tahoma_api.exceptions import BadCredentialsException, TooManyRequestsException
10-
from tahoma_api.models import Command, Device, Scenario, State
10+
from tahoma_api.models import Command, Device, Event, Execution, Scenario, State
1111

1212
JSON = Union[Dict[str, Any], List[Dict[str, Any]]]
1313

@@ -36,7 +36,7 @@ def __init__(self, username: str, password: str, api_url: str = API_URL) -> None
3636

3737
async def close(self) -> None:
3838
"""Close the session."""
39-
return await self.session.close()
39+
await self.session.close()
4040

4141
async def login(self) -> bool:
4242
"""
@@ -92,30 +92,31 @@ async def register_event_listener(self) -> str:
9292

9393
return listener_id
9494

95-
async def fetch_event_listener(self, listener_id: str) -> List[Any]:
95+
async def fetch_event_listener(self, listener_id: str) -> List[Event]:
9696
"""
9797
Fetch new events from a registered event listener. Fetched events are removed
9898
from the listener buffer. Return an empty response if no event is available.
9999
Per-session rate-limit : 1 calls per 1 SECONDS period for this particular
100100
operation (polling)
101101
"""
102102
response = await self.__post(f"events/{listener_id}/fetch")
103+
events = [Event(**e) for e in humps.decamelize(response)]
103104

104-
return response
105+
return events
105106

106-
async def get_current_execution(self, exec_id: str) -> List[Any]:
107+
async def get_current_execution(self, exec_id: str) -> List[Execution]:
107108
""" Get an action group execution currently running """
108-
response = await self.__get(f"/exec/current/{exec_id}")
109-
# TODO Strongly type executions
109+
response = await self.__get(f"exec/current/{exec_id}")
110+
executions = [Execution(**e) for e in humps.decamelize(response)]
110111

111-
return response
112+
return executions
112113

113-
async def get_current_executions(self) -> List[Any]:
114+
async def get_current_executions(self) -> List[Execution]:
114115
""" Get all action groups executions currently running """
115-
response = await self.__get("/exec/current")
116-
# TODO Strongly type executions
116+
response = await self.__get("exec/current")
117+
executions = [Execution(**e) for e in humps.decamelize(response)]
117118

118-
return response
119+
return executions
119120

120121
async def execute_command(
121122
self,

tahoma_api/models.py

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Device:
88
__slots__ = (
99
"id",
10+
"attributes",
1011
"controllable_name",
1112
"creation_time",
1213
"last_update_time",
@@ -28,7 +29,9 @@ class Device:
2829
def __init__(
2930
self,
3031
*,
32+
attributes: Optional[List[Dict[str, Any]]] = None,
3133
available: bool,
34+
enabled: bool,
3235
label: str,
3336
deviceurl: str,
3437
controllable_name: str,
@@ -42,32 +45,39 @@ def __init__(
4245
**_: Any
4346
) -> None:
4447
self.id = deviceurl
48+
self.attributes = [State(**a) for a in attributes] if attributes else None
4549
self.available = available
46-
self.definition = definition
50+
self.definition = Definition(**definition)
4751
self.deviceurl = deviceurl
52+
self.enabled = enabled
4853
self.label = label
4954
self.controllable_name = controllable_name
50-
self.states = states
51-
# self.states = [State(**s) for s in states]
55+
self.states = [State(**s) for s in states] if states else None
5256
self.data_properties = data_properties
5357
self.widget = widget
5458
self.ui_class = ui_class
5559
self.qualified_name = qualified_name
5660
self.type = type
5761

5862

59-
# class Definition:
60-
# __slots__ = (
61-
# "commands",
62-
# "states",
63-
# "widget_name",
64-
# "ui_class",
65-
# "ui_classifiers" "type",
66-
# )
63+
class Definition:
64+
__slots__ = ("commands", "states", "widget_name", "ui_class", "qualified_name")
6765

68-
# def __init__(self, command_name: str, nparams: int, **_: Any) -> None:
69-
# self.command_name = command_name
70-
# self.nparams = nparams
66+
def __init__(
67+
self,
68+
*,
69+
commands: List[Dict[str, Any]],
70+
states: Optional[List[Dict[str, Any]]] = None,
71+
widget_name: str,
72+
ui_class: str,
73+
qualified_name: str,
74+
**_: Any
75+
) -> None:
76+
self.commands = [CommandDefinition(**cd) for cd in commands]
77+
self.states = [StateDefinition(**sd) for sd in states] if states else None
78+
self.widget_name = widget_name
79+
self.ui_class = ui_class
80+
self.qualified_name = qualified_name
7181

7282

7383
class StateDefinition:
@@ -78,7 +88,11 @@ class StateDefinition:
7888
)
7989

8090
def __init__(
81-
self, qualified_name: str, type: str, values: Optional[str], **_: Any
91+
self,
92+
qualified_name: str,
93+
type: str,
94+
values: Optional[List[str]] = None,
95+
**_: Any
8296
) -> None:
8397
self.qualified_name = qualified_name
8498
self.type = type
@@ -125,6 +139,69 @@ class CommandMode(Enum):
125139
internal = "internal"
126140

127141

142+
class Event:
143+
__slots__ = (
144+
"timestamp",
145+
"name",
146+
"gateway_id",
147+
"exec_id",
148+
"deviceurl",
149+
"device_states",
150+
"old_state",
151+
"new_state",
152+
"owner_key",
153+
)
154+
155+
def __init__(
156+
self,
157+
timestamp: int,
158+
name: str,
159+
gateway_id: Optional[str] = None,
160+
exec_id: Optional[str] = None,
161+
deviceurl: Optional[str] = None,
162+
device_states: Optional[List[Dict[str, Any]]] = None,
163+
old_state: Optional[str] = None,
164+
new_state: Optional[str] = None,
165+
**_: Any
166+
):
167+
self.timestamp = timestamp
168+
self.name = name
169+
self.gateway_id = gateway_id
170+
self.exec_id = exec_id
171+
self.deviceurl = deviceurl
172+
self.device_states = (
173+
[State(**s) for s in device_states] if device_states else None
174+
)
175+
self.old_state = old_state
176+
self.new_state = new_state
177+
178+
179+
class Execution:
180+
181+
__slots__ = (
182+
"id",
183+
"description",
184+
"owner",
185+
"state",
186+
"action_group",
187+
)
188+
189+
def __init__(
190+
self,
191+
id: str,
192+
description: str,
193+
owner: str,
194+
state: str,
195+
action_group: List[Dict[str, Any]],
196+
**_: Any
197+
):
198+
self.id = id
199+
self.description = description
200+
self.owner = owner
201+
self.state = state
202+
self.action_group = action_group
203+
204+
128205
class Scenario:
129206
__slots__ = ("label", "oid")
130207

0 commit comments

Comments
 (0)