|
1 | | -"""A Python Client to interact with Dingz devices.""" |
| 1 | +"""Base details for the dingz Python bindings.""" |
2 | 2 | import asyncio |
3 | | -import logging |
| 3 | +import json |
| 4 | +import socket |
| 5 | +from typing import Any, Mapping, Optional |
4 | 6 |
|
5 | 7 | import aiohttp |
6 | 8 | import async_timeout |
7 | 9 |
|
8 | | -from . import exceptions |
9 | | -from .constants import TEMPERATURE, MOTION, LIGHT, PUCK, INFO |
10 | | - |
11 | | -_LOGGER = logging.getLogger(__name__) |
12 | | - |
13 | | - |
14 | | -class Dingz: |
15 | | - """A class for handling the communication with a Dingz device.""" |
16 | | - |
17 | | - def __init__(self, device, loop, session): |
18 | | - """Initialize the connection.""" |
19 | | - self._loop = loop |
20 | | - self._session = session |
21 | | - self.token = None |
22 | | - self.device = device |
23 | | - self.data = None |
24 | | - |
25 | | - async def get_data(self, endpoint): |
26 | | - """Retrieve the data from a given endpoint.""" |
27 | | - try: |
28 | | - with async_timeout.timeout(5, loop=self._loop): |
29 | | - response = await self._session.get(f"{endpoint}") |
30 | | - |
31 | | - _LOGGER.debug("Response from Dingz device: %s", response.status) |
32 | | - self.data = await response.json() |
33 | | - _LOGGER.debug(self.data) |
34 | | - except (asyncio.TimeoutError, aiohttp.ClientError): |
35 | | - _LOGGER.error("Can not load data from Dingz device") |
36 | | - self.data = None |
37 | | - raise exceptions.DingzConnectionError() |
38 | | - |
39 | | - @property |
40 | | - def info(self): |
41 | | - """Get the state of the motion sensor.""" |
42 | | - data = await self.get_data(INFO) |
43 | | - return data['motion'] |
44 | | - |
45 | | - @property |
46 | | - def puck(self): |
47 | | - """Get the state of the hardware.""" |
48 | | - data = await self.get_data(PUCK) |
49 | | - if self.data is not None: |
50 | | - return {'firmware': data['fw']['version'], 'hardware': data['hw']['version']} |
51 | | - |
52 | | - @property |
53 | | - def temperature(self): |
54 | | - """Get the temperature.""" |
55 | | - data = await self.get_data(TEMPERATURE) |
56 | | - return round(data['temperature'], 2) |
57 | | - |
58 | | - @property |
59 | | - def light(self): |
60 | | - """Get the brightness.""" |
61 | | - data = await self.get_data(LIGHT) |
62 | | - return round(data['intensity'], 2) |
63 | | - |
64 | | - @property |
65 | | - def day(self): |
66 | | - """Get the state if daytime or not.""" |
67 | | - data = await self.get_data(LIGHT) |
68 | | - return data['day'] |
69 | | - |
70 | | - @property |
71 | | - def motion(self): |
72 | | - """Get the state of the motion sensor.""" |
73 | | - data = await self.get_data(MOTION) |
74 | | - return data['motion'] |
75 | | - |
76 | | - |
77 | | - |
| 10 | +from .constants import TIMEOUT, USER_AGENT |
| 11 | +from .exceptions import DingzConnectionError, DingzError |
| 12 | + |
| 13 | + |
| 14 | +async def _request( |
| 15 | + self, |
| 16 | + uri: str, |
| 17 | + method: str = "GET", |
| 18 | + data: Optional[Any] = None, |
| 19 | + json_data: Optional[dict] = None, |
| 20 | + params: Optional[Mapping[str, str]] = None, |
| 21 | +) -> Any: |
| 22 | + """Handle a request to the dingz unit.""" |
| 23 | + headers = { |
| 24 | + "User-Agent": USER_AGENT, |
| 25 | + "Accept": "application/json, text/plain, */*", |
| 26 | + } |
| 27 | + |
| 28 | + if self._session is None: |
| 29 | + self._session = aiohttp.ClientSession() |
| 30 | + self._close_session = True |
| 31 | + |
| 32 | + try: |
| 33 | + with async_timeout.timeout(TIMEOUT): |
| 34 | + response = await self._session.request( |
| 35 | + method, uri, data=data, json=json_data, params=params, headers=headers, |
| 36 | + ) |
| 37 | + except asyncio.TimeoutError as exception: |
| 38 | + raise DingzConnectionError( |
| 39 | + "Timeout occurred while connecting to dingz unit." |
| 40 | + ) from exception |
| 41 | + except (aiohttp.ClientError, socket.gaierror) as exception: |
| 42 | + raise DingzConnectionError( |
| 43 | + "Error occurred while communicating with dingz." |
| 44 | + ) from exception |
| 45 | + |
| 46 | + content_type = response.headers.get("Content-Type", "") |
| 47 | + if (response.status // 100) in [4, 5]: |
| 48 | + contents = await response.read() |
| 49 | + response.close() |
| 50 | + |
| 51 | + if content_type == "application/json": |
| 52 | + raise DingzError(response.status, json.loads(contents.decode("utf8"))) |
| 53 | + raise DingzError(response.status, {"message": contents.decode("utf8")}) |
| 54 | + if "application/json" in content_type: |
| 55 | + response_json = await response.json() |
| 56 | + return response_json |
| 57 | + |
| 58 | + return response.text |
0 commit comments