File: api.py

package info (click to toggle)
python-homeconnect 0.8.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 120 kB
  • sloc: python: 556; sh: 6; makefile: 5
file content (441 lines) | stat: -rw-r--r-- 14,613 bytes parent folder | download
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
import json
import logging
import os
import time
from threading import Thread
from typing import Callable, Dict, Optional, Union

from oauthlib.oauth2 import TokenExpiredError
from requests import Response
from requests_oauthlib import OAuth2Session
from requests.exceptions import RetryError
from requests.adapters import HTTPAdapter, Retry

from .sseclient import SSEClient

URL_API = "https://api.home-connect.com"
ENDPOINT_AUTHORIZE = "/security/oauth/authorize"
ENDPOINT_TOKEN = "/security/oauth/token"
ENDPOINT_APPLIANCES = "/api/homeappliances"
TIMEOUT_S = 120
TOTAL_RETRIES = 1


LOGGER = logging.getLogger("homeconnect")


class HomeConnectError(Exception):
    pass


class HomeConnectAPI:
    def __init__(
        self,
        token: Optional[Dict[str, str]] = None,
        client_id: str = None,
        client_secret: str = None,
        redirect_uri: str = None,
        api_url: Optional[str] = None,
        token_updater: Optional[Callable[[str], None]] = None,
    ):
        self.host = api_url or URL_API
        self.client_id = client_id
        self.client_secret = client_secret
        self.redirect_uri = redirect_uri
        self.token_updater = token_updater

        self._appliances = {}
        self.listening_events = False

        extra = {"client_id": self.client_id, "client_secret": self.client_secret}

        self._oauth = OAuth2Session(
            client_id=client_id,
            redirect_uri=redirect_uri,
            auto_refresh_kwargs=extra,
            token=token,
            token_updater=token_updater,
        )
        self.retry = Retry(TOTAL_RETRIES, status_forcelist=[429])
        self._oauth.mount("https://", HTTPAdapter(max_retries=self.retry))
        self._oauth.mount("http://", HTTPAdapter(max_retries=self.retry))

    def refresh_tokens(self) -> Dict[str, Union[str, int]]:
        """Refresh and return new tokens."""
        LOGGER.info("Refreshing tokens ...")
        token = self._oauth.refresh_token(f"{self.host}{ENDPOINT_TOKEN}")

        if self.token_updater is not None:
            self.token_updater(token)

        return token

    def request(self, method: str, path: str, **kwargs) -> Response:
        """Make a request.

        We don't use the built-in token refresh mechanism of OAuth2 session because
        we want to allow overriding the token refresh logic.
        """
        url = f"{self.host}/{path.lstrip('/')}"
        try:
            return getattr(self._oauth, method)(url, **kwargs)
        except TokenExpiredError:
            LOGGER.warning("Token expired.")
            self._oauth.token = self.refresh_tokens()

            return getattr(self._oauth, method)(url, **kwargs)
        except RetryError as e:
            LOGGER.warning("Retry failed: %s", e)
            return e.response

    def get(self, endpoint):
        """Get data as dictionary from an endpoint."""
        res = self.request("get", endpoint)
        if not res.content:
            return {}
        try:
            res = res.json()
        except:
            raise ValueError("Cannot parse {} as JSON".format(res))
        if "error" in res:
            raise HomeConnectError(res["error"])
        elif "data" not in res:
            raise HomeConnectError("Unexpected error")
        return res["data"]

    def put(self, endpoint, data):
        """Send (PUT) data to an endpoint."""
        res = self.request(
            "put",
            endpoint,
            data=json.dumps(data),
            headers={
                "Content-Type": "application/vnd.bsh.sdk.v1+json",
                "accept": "application/vnd.bsh.sdk.v1+json",
            },
        )
        if not res.content:
            return {}
        try:
            res = res.json()
        except:
            raise ValueError("Cannot parse {} as JSON".format(res))
        if "error" in res:
            raise HomeConnectError(res["error"])
        return res

    def delete(self, endpoint):
        """Delete an endpoint."""
        res = self.request("delete", endpoint)
        if not res.content:
            return {}
        try:
            res = res.json()
        except:
            raise ValueError("Cannot parse {} as JSON".format(res))
        if "error" in res:
            raise HomeConnectError(res["error"])
        return res

    def get_appliances(self):
        """Return a list of `HomeConnectAppliance` instances for all
        appliances."""

        appliances = {}

        data = self.get(ENDPOINT_APPLIANCES)
        for home_appliance in data["homeappliances"]:
            haId = home_appliance["haId"]

            if haId in self._appliances:
                appliances[haId] = self._appliances[haId]
                appliances[haId].connected = home_appliance["connected"]
                continue

            appliances[haId] = HomeConnectAppliance(self, **home_appliance)

        self._appliances = appliances
        return list(self._appliances.values())

    def get_authurl(self):
        """Get the URL needed for the authorization code grant flow."""
        authorization_url, _ = self._oauth.authorization_url(
            f"{self.host}{ENDPOINT_AUTHORIZE}"
        )
        return authorization_url

    def listen_events(self):
        """Spawn a thread with an event listener that updates the status."""
        self.listening_events = True
        uri = f"{self.host}/api/homeappliances/events"
        sse = SSEClient(uri, session=self._oauth, retry=1000, timeout=TIMEOUT_S)
        Thread(target=self._listen, args=[sse]).start()

    def _listen(self, sse):
        """Worker function for listener."""
        LOGGER.info("Listening to event stream for all devices")
        try:
            for event in sse:
                try:
                    for appliance in self._appliances.values():
                        if appliance.haId == event.id:
                            self.handle_event(event, appliance)
                            break
                except ValueError:
                    pass
        except TokenExpiredError:
            LOGGER.info("Token expired in event stream.")
            self._oauth.token = self.refresh_tokens()
            uri = f"{self.host}/api/homeappliances/events"
            sse = SSEClient(uri, session=self._oauth, retry=1000, timeout=TIMEOUT_S)
            self._listen(sse)

    def handle_event(self, event, appliance):
        """Handle a new event.

        Updates the status with the event data and executes any callback
        function."""
        event_data = json.loads(event.data)
        items = event_data.get("items")
        if items is not None:
            data_dict = self.json2dict(items)
        else:
            data_dict = {event_data.pop("key"): event_data}

        if event.event in ("NOTIFY", "STATUS", "EVENT"):
            appliance.status.update(data_dict)

        elif event.event == "CONNECTED":
            appliance.connected = True
        elif event.event == "DISCONNECTED":
            appliance.connected = False

        if appliance.event_callback is not None:
            try:
                appliance.event_callback(appliance, event.event, data_dict)
            except TypeError:
                appliance.event_callback(appliance)

    @staticmethod
    def json2dict(lst):
        """Turn a list of dictionaries where one key is called 'key'
        into a dictionary with the value of 'key' as key."""
        return {d.pop("key"): d for d in lst}


class HomeConnect(HomeConnectAPI):
    """Connection to the HomeConnect OAuth API."""

    def __init__(
        self,
        client_id,
        client_secret="",
        redirect_uri="",
        api_url: Optional[str] = None,
        token_cache=None,
    ):
        """Initialize the connection."""
        self.token_cache = token_cache or "homeconnect_oauth_token.json"
        super().__init__(
            None, client_id, client_secret, redirect_uri, api_url, self.token_dump
        )

    def token_dump(self, token):
        """Dump the token to a JSON file."""
        with open(self.token_cache, "w") as f:
            json.dump(token, f)

    def token_load(self):
        """Load the token from the cache if exists it and is not expired,
        otherwise return None."""
        if not os.path.exists(self.token_cache):
            return None
        with open(self.token_cache, "r") as f:
            token = json.load(f)
        now = int(time.time())
        token["expires_in"] = token.get("expires_at", now - 1) - now
        self._oauth = OAuth2Session(
            client_id=self.client_id,
            redirect_uri=self.redirect_uri,
            auto_refresh_kwargs={"client_id": self.client_id, "client_secret": self.client_secret},
            token=token,
            token_updater=self.token_updater,
        )
        return token

    def token_expired(self, token):
        """Check if the token is expired."""
        now = int(time.time())
        return token["expires_at"] - now < 60

    def get_token(self, authorization_response):
        """Get the token given the redirect URL obtained from the
        authorization."""
        LOGGER.info("Fetching token ...")
        token = self._oauth.fetch_token(
            f"{self.host}{ENDPOINT_TOKEN}",
            authorization_response=authorization_response,
            client_secret=self.client_secret,
        )
        self.token_dump(token)


class HomeConnectAppliance:
    """Class representing a single appliance."""

    def __init__(
        self,
        hc,
        haId,
        vib=None,
        brand=None,
        type=None,
        name=None,
        enumber=None,
        connected=False,
    ):
        self.hc = hc
        self.haId = haId
        self.vib = vib or ""
        self.brand = brand or ""
        self.type = type or ""
        self.name = name or ""
        self.enumber = enumber or ""
        self.connected = connected
        self.status = {}

        self.event_callback = None

    def __repr__(self):
        return "HomeConnectAppliance(hc, haId='{}', vib='{}', brand='{}', type='{}', name='{}', enumber='{}', connected={})".format(
            self.haId,
            self.vib,
            self.brand,
            self.type,
            self.name,
            self.enumber,
            self.connected,
        )

    def listen_events(self, callback=None):
        """Register event callback method"""
        self.event_callback = callback

        if not self.hc.listening_events:
            self.hc.listen_events()

    @staticmethod
    def json2dict(lst):
        """Turn a list of dictionaries where one key is called 'key'
        into a dictionary with the value of 'key' as key."""
        return {d.pop("key"): d for d in lst}

    def get(self, endpoint):
        """Get data (as dictionary) from an endpoint."""
        return self.hc.get("{}/{}{}".format(ENDPOINT_APPLIANCES, self.haId, endpoint))

    def delete(self, endpoint):
        """Delete endpoint."""
        return self.hc.delete(
            "{}/{}{}".format(ENDPOINT_APPLIANCES, self.haId, endpoint)
        )

    def put(self, endpoint, data):
        """Send (PUT) data to an endpoint."""
        return self.hc.put(
            "{}/{}{}".format(ENDPOINT_APPLIANCES, self.haId, endpoint), data
        )

    def get_programs_active(self):
        """Get active programs."""
        return self.get("/programs/active")

    def get_programs_selected(self):
        """Get selected programs."""
        return self.get("/programs/selected")

    def get_programs_available(self):
        """Get available programs."""
        programs = self.get("/programs/available")
        if not programs or "programs" not in programs:
            return []
        return [p["key"] for p in programs["programs"]]

    def get_program_options(self, program_key):
        """Get program options."""
        options = self.get(f"/programs/available/{program_key}")
        if not options or "options" not in options:
            return []
        return [{p["key"]: p} for p in options["options"]]

    def start_program(self, program_key, options=None):
        """Start a program."""
        if options is not None:
            return self.put(
                "/programs/active", {"data": {"key": program_key, "options": options}}
            )
        return self.put("/programs/active", {"data": {"key": program_key}})

    def stop_program(self):
        """Stop a program."""
        return self.delete("/programs/active")

    def select_program(self, program, options=None):
        """Select a program."""
        if options is None:
            _options = {}
        else:
            _options = {"options": options}
        return self.put("/programs/selected", {"data": {"key": program, **_options}})

    def get_status(self):
        """Get the status (as dictionary) and update `self.status`."""
        status = self.get("/status")
        if not status or "status" not in status:
            return {}
        self.status = self.json2dict(status["status"])
        return self.status

    def get_settings(self):
        """Get the current settings."""
        settings = self.get("/settings")
        if not settings or "settings" not in settings:
            return {}
        self.status.update(self.json2dict(settings["settings"]))
        return self.status

    def set_setting(self, settingkey, value):
        """Change the current setting of `settingkey`."""
        return self.put(
            "/settings/{}".format(settingkey),
            {"data": {"key": settingkey, "value": value}},
        )

    def set_options_active_program(self, option_key, value, unit=None):
        """Change the option `option_key` of the currently active program."""
        if unit is None:
            _unit = {}
        else:
            _unit = {"unit": unit}
        return self.put(
            f"/programs/active/options/{option_key}",
            {"data": {"key": option_key, "value": value, **_unit}},
        )

    def set_options_selected_program(self, option_key, value, unit=None):
        """Change the option `option_key` of the currently selected program."""
        if unit is None:
            _unit = {}
        else:
            _unit = {"unit": unit}
        return self.put(
            f"/programs/selected/options/{option_key}",
            {"data": {"key": option_key, "value": value, **_unit}},
        )

    def execute_command(self, command):
        """Execute a command."""
        return self.put(
            f"/commands/{command}",
            {"data": {"key": command, "value": True}},
        )