pyhearth 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyhearth-1.0.0/CHANGELOG.md +17 -0
- pyhearth-1.0.0/LICENSE +21 -0
- pyhearth-1.0.0/MANIFEST.in +3 -0
- pyhearth-1.0.0/PKG-INFO +94 -0
- pyhearth-1.0.0/README.md +58 -0
- pyhearth-1.0.0/pyhearth/__init__.py +3 -0
- pyhearth-1.0.0/pyhearth/client.py +148 -0
- pyhearth-1.0.0/pyhearth/exception.py +53 -0
- pyhearth-1.0.0/pyhearth/py.typed +0 -0
- pyhearth-1.0.0/pyhearth.egg-info/PKG-INFO +94 -0
- pyhearth-1.0.0/pyhearth.egg-info/SOURCES.txt +16 -0
- pyhearth-1.0.0/pyhearth.egg-info/dependency_links.txt +1 -0
- pyhearth-1.0.0/pyhearth.egg-info/requires.txt +8 -0
- pyhearth-1.0.0/pyhearth.egg-info/top_level.txt +1 -0
- pyhearth-1.0.0/pyproject.toml +82 -0
- pyhearth-1.0.0/setup.cfg +4 -0
- pyhearth-1.0.0/tests/test_client.py +226 -0
- pyhearth-1.0.0/tests/testcase.py +50 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased](https://github.com/alexdlaird/pyhearth/compare/1.0.0...HEAD)
|
|
8
|
+
|
|
9
|
+
## [1.0.0](https://github.com/alexdlaird/pyhearth/compare/0.0.0...1.0.0) - 2026-10-26
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- First release of `pyhearth`: a client for Hearth's home API (`/v1/bridge` and `/v1/readings`).
|
|
14
|
+
|
|
15
|
+
## [0.0.0](https://github.com/alexdlaird/pyhearth/tree/0.0.0) - 2026-10-26
|
|
16
|
+
|
|
17
|
+
- Initial commit.
|
pyhearth-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex Laird
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pyhearth-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyhearth
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A Python client for Hearth's home API: post a thermostat and room sensor readings to a Hearth home
|
|
5
|
+
Author-email: Alex Laird <contact@alexlaird.com>
|
|
6
|
+
Maintainer-email: Alex Laird <contact@alexlaird.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Changelog, https://github.com/alexdlaird/pyhearth/blob/main/CHANGELOG.md
|
|
9
|
+
Project-URL: Documentation, https://github.com/alexdlaird/pyhearth#readme
|
|
10
|
+
Project-URL: Sponsor, https://github.com/sponsors/alexdlaird
|
|
11
|
+
Project-URL: Source Code, https://github.com/alexdlaird/pyhearth
|
|
12
|
+
Keywords: hearth,thermostat,climate,room-sensors,home-automation,daikin,asyncio
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Framework :: AsyncIO
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Topic :: Home Automation
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: aiohttp>=3.9
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: coverage[toml]; extra == "dev"
|
|
31
|
+
Requires-Dist: flake8; extra == "dev"
|
|
32
|
+
Requires-Dist: flake8-pyproject; extra == "dev"
|
|
33
|
+
Requires-Dist: pep8-naming; extra == "dev"
|
|
34
|
+
Requires-Dist: mypy; extra == "dev"
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
[](https://pypi.org/project/pyhearth)
|
|
38
|
+
[](https://pypi.org/project/pyhearth)
|
|
39
|
+
[](https://codecov.io/gh/alexdlaird/pyhearth)
|
|
40
|
+
[](https://github.com/alexdlaird/pyhearth/actions/workflows/build.yml)
|
|
41
|
+
[](https://github.com/alexdlaird/pyhearth/blob/main/LICENSE)
|
|
42
|
+
|
|
43
|
+
`pyhearth` is a Python client for [Hearth](https://www.meethearth.app)'s home API. It posts room sensor readings, and
|
|
44
|
+
optionally a thermostat, to a Hearth home with the home's API key, and returns the thermostat setting Hearth answers
|
|
45
|
+
with. It's what [`ha-hearth`](https://github.com/alexdlaird/ha-hearth), Hearth's Home Assistant integration, uses to
|
|
46
|
+
talk to Hearth.
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
`pyhearth` is available on [PyPI](https://pypi.org/project/pyhearth/) and can be installed using `pip`:
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
pip install pyhearth
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Basic Usage
|
|
57
|
+
|
|
58
|
+
In the Hearth app, create the home's API key in Settings, then Manage home, then Sensors, then "Other sensors (Hearth
|
|
59
|
+
API)". Then post readings:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import asyncio
|
|
63
|
+
|
|
64
|
+
import aiohttp
|
|
65
|
+
|
|
66
|
+
from pyhearth.client import HearthClient
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
async def main():
|
|
70
|
+
async with aiohttp.ClientSession() as session:
|
|
71
|
+
client = HearthClient(session, "<your API key>", source="my-hub", source_name="My hub")
|
|
72
|
+
result = await client.post_readings([
|
|
73
|
+
{"id": "office", "name": "Office", "temperature": 20.8, "humidity": 41}
|
|
74
|
+
])
|
|
75
|
+
print(result.not_added)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
asyncio.run(main())
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
A sensor's readings are kept once it's added to a room in the Hearth app; until then, it's listed in `not_added`. The
|
|
82
|
+
fields each endpoint takes are in [Hearth's API docs](https://www.meethearth.app/support.html#hearth-api).
|
|
83
|
+
|
|
84
|
+
Errors are raised as subclasses of `HearthError` (in `pyhearth.exception`): `HearthAuthError` when Hearth doesn't take
|
|
85
|
+
the key, `HearthRateLimitedError` when it asks for fewer posts (`meta["retry_after"]` says how many seconds to wait),
|
|
86
|
+
`HearthRejectedError` when it can't read a post, and `HearthConnectionError` when it can't be reached.
|
|
87
|
+
|
|
88
|
+
## Contributing
|
|
89
|
+
|
|
90
|
+
If you would like to get involved, be sure to review
|
|
91
|
+
the [Contribution Guide](https://github.com/alexdlaird/pyhearth/blob/main/CONTRIBUTING.rst).
|
|
92
|
+
|
|
93
|
+
Want to contribute financially? If you've found `pyhearth` useful, [sponsorship](https://github.com/sponsors/alexdlaird)
|
|
94
|
+
would also be greatly appreciated!
|
pyhearth-1.0.0/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
[](https://pypi.org/project/pyhearth)
|
|
2
|
+
[](https://pypi.org/project/pyhearth)
|
|
3
|
+
[](https://codecov.io/gh/alexdlaird/pyhearth)
|
|
4
|
+
[](https://github.com/alexdlaird/pyhearth/actions/workflows/build.yml)
|
|
5
|
+
[](https://github.com/alexdlaird/pyhearth/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
`pyhearth` is a Python client for [Hearth](https://www.meethearth.app)'s home API. It posts room sensor readings, and
|
|
8
|
+
optionally a thermostat, to a Hearth home with the home's API key, and returns the thermostat setting Hearth answers
|
|
9
|
+
with. It's what [`ha-hearth`](https://github.com/alexdlaird/ha-hearth), Hearth's Home Assistant integration, uses to
|
|
10
|
+
talk to Hearth.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
`pyhearth` is available on [PyPI](https://pypi.org/project/pyhearth/) and can be installed using `pip`:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
pip install pyhearth
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Basic Usage
|
|
21
|
+
|
|
22
|
+
In the Hearth app, create the home's API key in Settings, then Manage home, then Sensors, then "Other sensors (Hearth
|
|
23
|
+
API)". Then post readings:
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import asyncio
|
|
27
|
+
|
|
28
|
+
import aiohttp
|
|
29
|
+
|
|
30
|
+
from pyhearth.client import HearthClient
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
async def main():
|
|
34
|
+
async with aiohttp.ClientSession() as session:
|
|
35
|
+
client = HearthClient(session, "<your API key>", source="my-hub", source_name="My hub")
|
|
36
|
+
result = await client.post_readings([
|
|
37
|
+
{"id": "office", "name": "Office", "temperature": 20.8, "humidity": 41}
|
|
38
|
+
])
|
|
39
|
+
print(result.not_added)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
asyncio.run(main())
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
A sensor's readings are kept once it's added to a room in the Hearth app; until then, it's listed in `not_added`. The
|
|
46
|
+
fields each endpoint takes are in [Hearth's API docs](https://www.meethearth.app/support.html#hearth-api).
|
|
47
|
+
|
|
48
|
+
Errors are raised as subclasses of `HearthError` (in `pyhearth.exception`): `HearthAuthError` when Hearth doesn't take
|
|
49
|
+
the key, `HearthRateLimitedError` when it asks for fewer posts (`meta["retry_after"]` says how many seconds to wait),
|
|
50
|
+
`HearthRejectedError` when it can't read a post, and `HearthConnectionError` when it can't be reached.
|
|
51
|
+
|
|
52
|
+
## Contributing
|
|
53
|
+
|
|
54
|
+
If you would like to get involved, be sure to review
|
|
55
|
+
the [Contribution Guide](https://github.com/alexdlaird/pyhearth/blob/main/CONTRIBUTING.rst).
|
|
56
|
+
|
|
57
|
+
Want to contribute financially? If you've found `pyhearth` useful, [sponsorship](https://github.com/sponsors/alexdlaird)
|
|
58
|
+
would also be greatly appreciated!
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
__copyright__ = "Copyright (c) 2026 Alex Laird"
|
|
2
|
+
__license__ = "MIT"
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from typing import Any, Final
|
|
7
|
+
|
|
8
|
+
import aiohttp
|
|
9
|
+
|
|
10
|
+
from pyhearth.exception import (
|
|
11
|
+
HearthAuthError,
|
|
12
|
+
HearthConnectionError,
|
|
13
|
+
HearthDemoHomeError,
|
|
14
|
+
HearthRateLimitedError,
|
|
15
|
+
HearthRejectedError,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
#: Where Hearth's API is.
|
|
19
|
+
DEFAULT_API_URL: Final = "https://api.meethearth.app"
|
|
20
|
+
#: The version of the bridge protocol Hearth's ``/v1/bridge`` endpoint speaks.
|
|
21
|
+
PROTOCOL: Final = 1
|
|
22
|
+
#: How long one request to Hearth may take, in seconds.
|
|
23
|
+
REQUEST_TIMEOUT: Final = 15
|
|
24
|
+
#: How many seconds to wait when Hearth asks to slow down without saying for how long.
|
|
25
|
+
DEFAULT_RETRY_AFTER: Final = 60
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class ReadingsResult:
|
|
30
|
+
"""
|
|
31
|
+
What Hearth said about a readings post.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
#: The posted sensors that aren't added to a room in the Hearth app yet (Hearth drops their readings).
|
|
35
|
+
not_added: list[str] = field(default_factory=list)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class HearthClient:
|
|
39
|
+
"""
|
|
40
|
+
A connection to one Hearth home through its API key: ``/v1/bridge`` for the thermostat and ``/v1/readings`` for
|
|
41
|
+
room sensors.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def __init__(self,
|
|
45
|
+
session: aiohttp.ClientSession,
|
|
46
|
+
api_key: str,
|
|
47
|
+
base_url: str = DEFAULT_API_URL,
|
|
48
|
+
*,
|
|
49
|
+
source: str | None = None,
|
|
50
|
+
source_name: str | None = None) -> None:
|
|
51
|
+
self._session = session
|
|
52
|
+
self._api_key = api_key
|
|
53
|
+
|
|
54
|
+
#: Where Hearth's API is (Hearth's own unless pointed at another backend).
|
|
55
|
+
self.base_url: str = base_url.rstrip("/")
|
|
56
|
+
#: The readings source this client posts as (Hearth's default, ``api``, if not given): Hearth keeps each
|
|
57
|
+
#: source's sensors and outages apart.
|
|
58
|
+
self.source: str | None = source
|
|
59
|
+
#: How the Hearth app names the source, if given.
|
|
60
|
+
self.source_name: str | None = source_name
|
|
61
|
+
|
|
62
|
+
async def check_key(self) -> None:
|
|
63
|
+
"""
|
|
64
|
+
Check that Hearth takes the API key, with an empty readings post (which changes nothing).
|
|
65
|
+
|
|
66
|
+
:raises: :class:`~pyhearth.exception.HearthError`: When Hearth doesn't take the key or can't be reached.
|
|
67
|
+
"""
|
|
68
|
+
await self._post("/v1/readings", self._readings_payload([], named=False))
|
|
69
|
+
|
|
70
|
+
async def sync_thermostat(self,
|
|
71
|
+
thermostat: dict[str, Any],
|
|
72
|
+
refused: dict[str, Any] | None = None) -> dict[str, Any] | None:
|
|
73
|
+
"""
|
|
74
|
+
Post the thermostat to Hearth.
|
|
75
|
+
|
|
76
|
+
:param thermostat: The thermostat, as ``/v1/bridge`` takes it.
|
|
77
|
+
:param refused: The last setting that couldn't be applied (as Hearth answered it), why, and who refused it.
|
|
78
|
+
:return: The setting Hearth wants applied, if any.
|
|
79
|
+
:raises: :class:`~pyhearth.exception.HearthError`: When Hearth refuses the post or can't be reached.
|
|
80
|
+
"""
|
|
81
|
+
payload: dict[str, Any] = {"protocol": PROTOCOL, "thermostat": thermostat}
|
|
82
|
+
if refused is not None:
|
|
83
|
+
payload["refused"] = refused
|
|
84
|
+
status, body = await self._post("/v1/bridge", payload)
|
|
85
|
+
desired = body.get("desired") if status == 200 and isinstance(body, dict) else None
|
|
86
|
+
return desired if isinstance(desired, dict) else None
|
|
87
|
+
|
|
88
|
+
async def post_readings(self, sensors: list[dict[str, Any]]) -> ReadingsResult:
|
|
89
|
+
"""
|
|
90
|
+
Post room sensors' readings to Hearth, which keeps only those of sensors added in its app.
|
|
91
|
+
|
|
92
|
+
:param sensors: The readings, as ``/v1/readings`` takes them.
|
|
93
|
+
:return: What Hearth said about them.
|
|
94
|
+
:raises: :class:`~pyhearth.exception.HearthError`: When Hearth refuses the post or can't be reached.
|
|
95
|
+
"""
|
|
96
|
+
status, body = await self._post("/v1/readings", self._readings_payload(sensors, named=True))
|
|
97
|
+
if status == 422:
|
|
98
|
+
return ReadingsResult([str(sensor["id"]) for sensor in sensors])
|
|
99
|
+
not_added = body.get("not_added") if isinstance(body, dict) else None
|
|
100
|
+
return ReadingsResult([str(name) for name in not_added] if isinstance(not_added, list) else [])
|
|
101
|
+
|
|
102
|
+
def _readings_payload(self, sensors: list[dict[str, Any]], named: bool) -> dict[str, Any]:
|
|
103
|
+
payload: dict[str, Any] = {}
|
|
104
|
+
if self.source is not None:
|
|
105
|
+
payload["source"] = self.source
|
|
106
|
+
if named and self.source_name is not None:
|
|
107
|
+
payload["name"] = self.source_name
|
|
108
|
+
payload["sensors"] = sensors
|
|
109
|
+
return payload
|
|
110
|
+
|
|
111
|
+
async def _post(self, path: str, payload: dict[str, Any]) -> tuple[int, Any]:
|
|
112
|
+
try:
|
|
113
|
+
async with self._session.post(f"{self.base_url}{path}",
|
|
114
|
+
json=payload,
|
|
115
|
+
headers={"Authorization": f"Bearer {self._api_key}"},
|
|
116
|
+
timeout=aiohttp.ClientTimeout(total=REQUEST_TIMEOUT)) as response:
|
|
117
|
+
text = await response.text()
|
|
118
|
+
status = response.status
|
|
119
|
+
retry_after = response.headers.get("Retry-After")
|
|
120
|
+
except (aiohttp.ClientError, TimeoutError) as e:
|
|
121
|
+
raise HearthConnectionError(f"Couldn't reach Hearth: {e}") from e
|
|
122
|
+
|
|
123
|
+
if status in (200, 204, 422):
|
|
124
|
+
return status, _json(text) if text else None
|
|
125
|
+
elif status == 401:
|
|
126
|
+
raise HearthAuthError("Hearth didn't accept the API key.")
|
|
127
|
+
elif status == 403:
|
|
128
|
+
raise HearthDemoHomeError(text or "The API key belongs to a demo home.")
|
|
129
|
+
elif status == 429:
|
|
130
|
+
raise HearthRateLimitedError("Hearth asked for fewer posts.", {"retry_after": _seconds(retry_after)})
|
|
131
|
+
elif status >= 500:
|
|
132
|
+
raise HearthConnectionError(f"Hearth answered {status}.")
|
|
133
|
+
else:
|
|
134
|
+
raise HearthRejectedError(text[:300] or f"Hearth answered {status}.")
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _json(text: str) -> Any:
|
|
138
|
+
try:
|
|
139
|
+
return json.loads(text)
|
|
140
|
+
except ValueError:
|
|
141
|
+
return None
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _seconds(value: str | None) -> int:
|
|
145
|
+
try:
|
|
146
|
+
return max(1, int(value)) if value is not None else DEFAULT_RETRY_AFTER
|
|
147
|
+
except ValueError:
|
|
148
|
+
return DEFAULT_RETRY_AFTER
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
__copyright__ = "Copyright (c) 2026 Alex Laird"
|
|
2
|
+
__license__ = "MIT"
|
|
3
|
+
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class HearthError(Exception):
|
|
8
|
+
"""
|
|
9
|
+
Raised when a general Hearth API error has occurred.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
def __init__(self,
|
|
13
|
+
error: str | BaseException,
|
|
14
|
+
meta: dict[str, Any] | None = None) -> None:
|
|
15
|
+
super(HearthError, self).__init__(error)
|
|
16
|
+
|
|
17
|
+
#: Metadata for context around the error that was raised.
|
|
18
|
+
self.meta: dict[str, Any] | None = meta
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class HearthAuthError(HearthError):
|
|
22
|
+
"""
|
|
23
|
+
Raised when Hearth doesn't accept the API key (it's wrong, it was replaced, or its home is gone).
|
|
24
|
+
"""
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class HearthDemoHomeError(HearthError):
|
|
29
|
+
"""
|
|
30
|
+
Raised when the API key belongs to a demo home, which runs on a simulated thermostat and takes no posts.
|
|
31
|
+
"""
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class HearthRateLimitedError(HearthError):
|
|
36
|
+
"""
|
|
37
|
+
Raised when Hearth asks for fewer posts. ``meta["retry_after"]`` is how many seconds to wait.
|
|
38
|
+
"""
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class HearthRejectedError(HearthError):
|
|
43
|
+
"""
|
|
44
|
+
Raised when Hearth can't read a post. The error's message is Hearth's reason.
|
|
45
|
+
"""
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class HearthConnectionError(HearthError):
|
|
50
|
+
"""
|
|
51
|
+
Raised when Hearth can't be reached (the network, a timeout, or an error on Hearth's side).
|
|
52
|
+
"""
|
|
53
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyhearth
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A Python client for Hearth's home API: post a thermostat and room sensor readings to a Hearth home
|
|
5
|
+
Author-email: Alex Laird <contact@alexlaird.com>
|
|
6
|
+
Maintainer-email: Alex Laird <contact@alexlaird.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Changelog, https://github.com/alexdlaird/pyhearth/blob/main/CHANGELOG.md
|
|
9
|
+
Project-URL: Documentation, https://github.com/alexdlaird/pyhearth#readme
|
|
10
|
+
Project-URL: Sponsor, https://github.com/sponsors/alexdlaird
|
|
11
|
+
Project-URL: Source Code, https://github.com/alexdlaird/pyhearth
|
|
12
|
+
Keywords: hearth,thermostat,climate,room-sensors,home-automation,daikin,asyncio
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Framework :: AsyncIO
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Topic :: Home Automation
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: aiohttp>=3.9
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: coverage[toml]; extra == "dev"
|
|
31
|
+
Requires-Dist: flake8; extra == "dev"
|
|
32
|
+
Requires-Dist: flake8-pyproject; extra == "dev"
|
|
33
|
+
Requires-Dist: pep8-naming; extra == "dev"
|
|
34
|
+
Requires-Dist: mypy; extra == "dev"
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
[](https://pypi.org/project/pyhearth)
|
|
38
|
+
[](https://pypi.org/project/pyhearth)
|
|
39
|
+
[](https://codecov.io/gh/alexdlaird/pyhearth)
|
|
40
|
+
[](https://github.com/alexdlaird/pyhearth/actions/workflows/build.yml)
|
|
41
|
+
[](https://github.com/alexdlaird/pyhearth/blob/main/LICENSE)
|
|
42
|
+
|
|
43
|
+
`pyhearth` is a Python client for [Hearth](https://www.meethearth.app)'s home API. It posts room sensor readings, and
|
|
44
|
+
optionally a thermostat, to a Hearth home with the home's API key, and returns the thermostat setting Hearth answers
|
|
45
|
+
with. It's what [`ha-hearth`](https://github.com/alexdlaird/ha-hearth), Hearth's Home Assistant integration, uses to
|
|
46
|
+
talk to Hearth.
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
`pyhearth` is available on [PyPI](https://pypi.org/project/pyhearth/) and can be installed using `pip`:
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
pip install pyhearth
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Basic Usage
|
|
57
|
+
|
|
58
|
+
In the Hearth app, create the home's API key in Settings, then Manage home, then Sensors, then "Other sensors (Hearth
|
|
59
|
+
API)". Then post readings:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import asyncio
|
|
63
|
+
|
|
64
|
+
import aiohttp
|
|
65
|
+
|
|
66
|
+
from pyhearth.client import HearthClient
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
async def main():
|
|
70
|
+
async with aiohttp.ClientSession() as session:
|
|
71
|
+
client = HearthClient(session, "<your API key>", source="my-hub", source_name="My hub")
|
|
72
|
+
result = await client.post_readings([
|
|
73
|
+
{"id": "office", "name": "Office", "temperature": 20.8, "humidity": 41}
|
|
74
|
+
])
|
|
75
|
+
print(result.not_added)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
asyncio.run(main())
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
A sensor's readings are kept once it's added to a room in the Hearth app; until then, it's listed in `not_added`. The
|
|
82
|
+
fields each endpoint takes are in [Hearth's API docs](https://www.meethearth.app/support.html#hearth-api).
|
|
83
|
+
|
|
84
|
+
Errors are raised as subclasses of `HearthError` (in `pyhearth.exception`): `HearthAuthError` when Hearth doesn't take
|
|
85
|
+
the key, `HearthRateLimitedError` when it asks for fewer posts (`meta["retry_after"]` says how many seconds to wait),
|
|
86
|
+
`HearthRejectedError` when it can't read a post, and `HearthConnectionError` when it can't be reached.
|
|
87
|
+
|
|
88
|
+
## Contributing
|
|
89
|
+
|
|
90
|
+
If you would like to get involved, be sure to review
|
|
91
|
+
the [Contribution Guide](https://github.com/alexdlaird/pyhearth/blob/main/CONTRIBUTING.rst).
|
|
92
|
+
|
|
93
|
+
Want to contribute financially? If you've found `pyhearth` useful, [sponsorship](https://github.com/sponsors/alexdlaird)
|
|
94
|
+
would also be greatly appreciated!
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
CHANGELOG.md
|
|
2
|
+
LICENSE
|
|
3
|
+
MANIFEST.in
|
|
4
|
+
README.md
|
|
5
|
+
pyproject.toml
|
|
6
|
+
pyhearth/__init__.py
|
|
7
|
+
pyhearth/client.py
|
|
8
|
+
pyhearth/exception.py
|
|
9
|
+
pyhearth/py.typed
|
|
10
|
+
pyhearth.egg-info/PKG-INFO
|
|
11
|
+
pyhearth.egg-info/SOURCES.txt
|
|
12
|
+
pyhearth.egg-info/dependency_links.txt
|
|
13
|
+
pyhearth.egg-info/requires.txt
|
|
14
|
+
pyhearth.egg-info/top_level.txt
|
|
15
|
+
tests/test_client.py
|
|
16
|
+
tests/testcase.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyhearth
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pyhearth"
|
|
3
|
+
dynamic = ["version"]
|
|
4
|
+
description = "A Python client for Hearth's home API: post a thermostat and room sensor readings to a Hearth home"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
keywords = ["hearth", "thermostat", "climate", "room-sensors", "home-automation", "daikin", "asyncio"]
|
|
8
|
+
authors = [{ name = "Alex Laird", email = "contact@alexlaird.com" }]
|
|
9
|
+
maintainers = [{ name = "Alex Laird", email = "contact@alexlaird.com" }]
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"aiohttp>=3.9"
|
|
13
|
+
]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 5 - Production/Stable",
|
|
16
|
+
"Framework :: AsyncIO",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Programming Language :: Python :: 3.14",
|
|
25
|
+
"Topic :: Home Automation",
|
|
26
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
dev = [
|
|
31
|
+
"coverage[toml]",
|
|
32
|
+
"flake8",
|
|
33
|
+
"flake8-pyproject",
|
|
34
|
+
"pep8-naming",
|
|
35
|
+
"mypy"
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Changelog = "https://github.com/alexdlaird/pyhearth/blob/main/CHANGELOG.md"
|
|
40
|
+
Documentation = "https://github.com/alexdlaird/pyhearth#readme"
|
|
41
|
+
Sponsor = "https://github.com/sponsors/alexdlaird"
|
|
42
|
+
"Source Code" = "https://github.com/alexdlaird/pyhearth"
|
|
43
|
+
|
|
44
|
+
[tool.setuptools]
|
|
45
|
+
package-dir = { pyhearth = "pyhearth" }
|
|
46
|
+
|
|
47
|
+
[tool.setuptools.package-data]
|
|
48
|
+
pyhearth = ["py.typed"]
|
|
49
|
+
|
|
50
|
+
[tool.setuptools.dynamic]
|
|
51
|
+
version = { attr = "pyhearth.__version__" }
|
|
52
|
+
|
|
53
|
+
[tool.coverage.run]
|
|
54
|
+
omit = [
|
|
55
|
+
"venv/**",
|
|
56
|
+
"*/tests/**"
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
[tool.coverage.report]
|
|
60
|
+
precision = 2
|
|
61
|
+
exclude_lines = [
|
|
62
|
+
"pragma: no cover",
|
|
63
|
+
"def __repr__",
|
|
64
|
+
"raise AssertionError",
|
|
65
|
+
"raise NotImplementedError",
|
|
66
|
+
"if __name__ == .__main__.:",
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
[tool.coverage.xml]
|
|
70
|
+
output = "build/coverage/coverage.xml"
|
|
71
|
+
|
|
72
|
+
[tool.coverage.html]
|
|
73
|
+
title = "Coverage Report"
|
|
74
|
+
directory = "build/coverage"
|
|
75
|
+
|
|
76
|
+
[tool.mypy]
|
|
77
|
+
strict = true
|
|
78
|
+
|
|
79
|
+
[tool.flake8]
|
|
80
|
+
max-line-length = 119
|
|
81
|
+
statistics = true
|
|
82
|
+
exclude = "venv/*,build/*,dist/*,.egg-info/*"
|
pyhearth-1.0.0/setup.cfg
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
__copyright__ = "Copyright (c) 2026 Alex Laird"
|
|
2
|
+
__license__ = "MIT"
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
from pyhearth.client import DEFAULT_RETRY_AFTER, PROTOCOL, HearthClient
|
|
7
|
+
from pyhearth.exception import (
|
|
8
|
+
HearthAuthError,
|
|
9
|
+
HearthConnectionError,
|
|
10
|
+
HearthDemoHomeError,
|
|
11
|
+
HearthRateLimitedError,
|
|
12
|
+
HearthRejectedError,
|
|
13
|
+
)
|
|
14
|
+
from tests.testcase import HearthTestCase
|
|
15
|
+
|
|
16
|
+
THERMOSTAT = {"entity_id": "climate.hallway", "hvac_mode": "heat", "current_temperature": 20.5}
|
|
17
|
+
DESIRED = {"hvac_mode": "heat", "temperature": 21.0}
|
|
18
|
+
READINGS = [{"id": "sensor.office", "temperature": 20.8}, {"id": "sensor.den", "temperature": 19.9}]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class TestCheckKey(HearthTestCase):
|
|
22
|
+
async def test_check_key(self) -> None:
|
|
23
|
+
# GIVEN
|
|
24
|
+
client = self.given_client(source="home-assistant")
|
|
25
|
+
|
|
26
|
+
# WHEN
|
|
27
|
+
await client.check_key()
|
|
28
|
+
|
|
29
|
+
# THEN
|
|
30
|
+
self.assertEqual([{"source": "home-assistant", "sensors": []}], self.posted("readings"))
|
|
31
|
+
self.assertEqual([f"Bearer {self.API_KEY}"], self.authorizations)
|
|
32
|
+
|
|
33
|
+
async def test_check_key_refused(self) -> None:
|
|
34
|
+
# GIVEN
|
|
35
|
+
self.given_hearth_answers("readings", 401)
|
|
36
|
+
client = self.given_client()
|
|
37
|
+
|
|
38
|
+
# WHEN / THEN
|
|
39
|
+
with self.assertRaises(HearthAuthError):
|
|
40
|
+
await client.check_key()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class TestSyncThermostat(HearthTestCase):
|
|
44
|
+
async def test_sync_thermostat_desired(self) -> None:
|
|
45
|
+
# GIVEN
|
|
46
|
+
self.given_hearth_answers("bridge", 200, json.dumps({"desired": DESIRED}))
|
|
47
|
+
client = self.given_client()
|
|
48
|
+
|
|
49
|
+
# WHEN
|
|
50
|
+
desired = await client.sync_thermostat(THERMOSTAT)
|
|
51
|
+
|
|
52
|
+
# THEN
|
|
53
|
+
self.assertEqual(DESIRED, desired)
|
|
54
|
+
self.assertEqual([{"protocol": PROTOCOL, "thermostat": THERMOSTAT}], self.posted("bridge"))
|
|
55
|
+
|
|
56
|
+
async def test_sync_thermostat_refused_setting(self) -> None:
|
|
57
|
+
# GIVEN
|
|
58
|
+
refused = {"setting": DESIRED, "reason": "out of range", "by": "thermostat"}
|
|
59
|
+
client = self.given_client()
|
|
60
|
+
|
|
61
|
+
# WHEN
|
|
62
|
+
await client.sync_thermostat(THERMOSTAT, refused)
|
|
63
|
+
|
|
64
|
+
# THEN
|
|
65
|
+
self.assertEqual(refused, self.posted("bridge")[0]["refused"])
|
|
66
|
+
|
|
67
|
+
async def test_sync_thermostat_nothing_desired(self) -> None:
|
|
68
|
+
# GIVEN
|
|
69
|
+
client = self.given_client()
|
|
70
|
+
|
|
71
|
+
# WHEN
|
|
72
|
+
desired = await client.sync_thermostat(THERMOSTAT)
|
|
73
|
+
|
|
74
|
+
# THEN
|
|
75
|
+
self.assertIsNone(desired)
|
|
76
|
+
|
|
77
|
+
async def test_sync_thermostat_desired_not_a_setting(self) -> None:
|
|
78
|
+
# GIVEN
|
|
79
|
+
self.given_hearth_answers("bridge", 200, json.dumps({"desired": "heat"}))
|
|
80
|
+
client = self.given_client()
|
|
81
|
+
|
|
82
|
+
# WHEN
|
|
83
|
+
desired = await client.sync_thermostat(THERMOSTAT)
|
|
84
|
+
|
|
85
|
+
# THEN
|
|
86
|
+
self.assertIsNone(desired)
|
|
87
|
+
|
|
88
|
+
async def test_sync_thermostat_unreadable_answer(self) -> None:
|
|
89
|
+
# GIVEN
|
|
90
|
+
self.given_hearth_answers("bridge", 200, "not json")
|
|
91
|
+
client = self.given_client()
|
|
92
|
+
|
|
93
|
+
# WHEN
|
|
94
|
+
desired = await client.sync_thermostat(THERMOSTAT)
|
|
95
|
+
|
|
96
|
+
# THEN
|
|
97
|
+
self.assertIsNone(desired)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class TestPostReadings(HearthTestCase):
|
|
101
|
+
async def test_post_readings(self) -> None:
|
|
102
|
+
# GIVEN
|
|
103
|
+
self.given_hearth_answers("readings", 200, json.dumps({"not_added": ["sensor.den"]}))
|
|
104
|
+
client = self.given_client(source="home-assistant", source_name="Home Assistant")
|
|
105
|
+
|
|
106
|
+
# WHEN
|
|
107
|
+
result = await client.post_readings(READINGS)
|
|
108
|
+
|
|
109
|
+
# THEN
|
|
110
|
+
self.assertEqual(["sensor.den"], result.not_added)
|
|
111
|
+
self.assertEqual([{"source": "home-assistant", "name": "Home Assistant", "sensors": READINGS}],
|
|
112
|
+
self.posted("readings"))
|
|
113
|
+
|
|
114
|
+
async def test_post_readings_default_source(self) -> None:
|
|
115
|
+
# GIVEN
|
|
116
|
+
client = self.given_client()
|
|
117
|
+
|
|
118
|
+
# WHEN
|
|
119
|
+
result = await client.post_readings(READINGS)
|
|
120
|
+
|
|
121
|
+
# THEN
|
|
122
|
+
self.assertEqual([], result.not_added)
|
|
123
|
+
self.assertEqual([{"sensors": READINGS}], self.posted("readings"), "Hearth's own default source applies")
|
|
124
|
+
|
|
125
|
+
async def test_post_readings_none_added(self) -> None:
|
|
126
|
+
# GIVEN
|
|
127
|
+
self.given_hearth_answers("readings", 422, json.dumps({"error": "no sensor of this home"}))
|
|
128
|
+
client = self.given_client()
|
|
129
|
+
|
|
130
|
+
# WHEN
|
|
131
|
+
result = await client.post_readings(READINGS)
|
|
132
|
+
|
|
133
|
+
# THEN
|
|
134
|
+
self.assertEqual(["sensor.office", "sensor.den"], result.not_added)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class TestErrors(HearthTestCase):
|
|
138
|
+
async def test_demo_home(self) -> None:
|
|
139
|
+
# GIVEN
|
|
140
|
+
self.given_hearth_answers("bridge", 403, "This is a demo home.")
|
|
141
|
+
client = self.given_client()
|
|
142
|
+
|
|
143
|
+
# WHEN / THEN
|
|
144
|
+
with self.assertRaises(HearthDemoHomeError) as raised:
|
|
145
|
+
await client.sync_thermostat(THERMOSTAT)
|
|
146
|
+
self.assertEqual("This is a demo home.", str(raised.exception))
|
|
147
|
+
|
|
148
|
+
async def test_rate_limited(self) -> None:
|
|
149
|
+
# GIVEN
|
|
150
|
+
self.given_hearth_answers("bridge", 429, headers={"Retry-After": "30"})
|
|
151
|
+
client = self.given_client()
|
|
152
|
+
|
|
153
|
+
# WHEN / THEN
|
|
154
|
+
with self.assertRaises(HearthRateLimitedError) as raised:
|
|
155
|
+
await client.sync_thermostat(THERMOSTAT)
|
|
156
|
+
self.assertEqual({"retry_after": 30}, raised.exception.meta)
|
|
157
|
+
|
|
158
|
+
async def test_rate_limited_without_retry_after(self) -> None:
|
|
159
|
+
# GIVEN
|
|
160
|
+
self.given_hearth_answers("bridge", 429)
|
|
161
|
+
client = self.given_client()
|
|
162
|
+
|
|
163
|
+
# WHEN / THEN
|
|
164
|
+
with self.assertRaises(HearthRateLimitedError) as raised:
|
|
165
|
+
await client.sync_thermostat(THERMOSTAT)
|
|
166
|
+
self.assertEqual({"retry_after": DEFAULT_RETRY_AFTER}, raised.exception.meta)
|
|
167
|
+
|
|
168
|
+
async def test_rate_limited_unreadable_retry_after(self) -> None:
|
|
169
|
+
# GIVEN
|
|
170
|
+
self.given_hearth_answers("bridge", 429, headers={"Retry-After": "soon"})
|
|
171
|
+
client = self.given_client()
|
|
172
|
+
|
|
173
|
+
# WHEN / THEN
|
|
174
|
+
with self.assertRaises(HearthRateLimitedError) as raised:
|
|
175
|
+
await client.sync_thermostat(THERMOSTAT)
|
|
176
|
+
self.assertEqual({"retry_after": DEFAULT_RETRY_AFTER}, raised.exception.meta)
|
|
177
|
+
|
|
178
|
+
async def test_rejected(self) -> None:
|
|
179
|
+
# GIVEN
|
|
180
|
+
self.given_hearth_answers("readings", 400, "sensors must be a list")
|
|
181
|
+
client = self.given_client()
|
|
182
|
+
|
|
183
|
+
# WHEN / THEN
|
|
184
|
+
with self.assertRaises(HearthRejectedError) as raised:
|
|
185
|
+
await client.post_readings(READINGS)
|
|
186
|
+
self.assertEqual("sensors must be a list", str(raised.exception))
|
|
187
|
+
|
|
188
|
+
async def test_rejected_without_reason(self) -> None:
|
|
189
|
+
# GIVEN
|
|
190
|
+
self.given_hearth_answers("readings", 413)
|
|
191
|
+
client = self.given_client()
|
|
192
|
+
|
|
193
|
+
# WHEN / THEN
|
|
194
|
+
with self.assertRaises(HearthRejectedError) as raised:
|
|
195
|
+
await client.post_readings(READINGS)
|
|
196
|
+
self.assertEqual("Hearth answered 413.", str(raised.exception))
|
|
197
|
+
|
|
198
|
+
async def test_server_error(self) -> None:
|
|
199
|
+
# GIVEN
|
|
200
|
+
self.given_hearth_answers("bridge", 503)
|
|
201
|
+
client = self.given_client()
|
|
202
|
+
|
|
203
|
+
# WHEN / THEN
|
|
204
|
+
with self.assertRaises(HearthConnectionError):
|
|
205
|
+
await client.sync_thermostat(THERMOSTAT)
|
|
206
|
+
|
|
207
|
+
async def test_unreachable(self) -> None:
|
|
208
|
+
# GIVEN
|
|
209
|
+
url = str(self.server.make_url("/"))
|
|
210
|
+
await self.server.close()
|
|
211
|
+
client = HearthClient(self.session, self.API_KEY, url)
|
|
212
|
+
|
|
213
|
+
# WHEN / THEN
|
|
214
|
+
with self.assertRaises(HearthConnectionError):
|
|
215
|
+
await client.check_key()
|
|
216
|
+
|
|
217
|
+
async def test_base_url_trailing_slash(self) -> None:
|
|
218
|
+
# GIVEN
|
|
219
|
+
client = HearthClient(self.session, self.API_KEY, f"{self.server.make_url('/')}/")
|
|
220
|
+
|
|
221
|
+
# WHEN
|
|
222
|
+
await client.check_key()
|
|
223
|
+
|
|
224
|
+
# THEN
|
|
225
|
+
self.assertEqual(str(self.server.make_url("")).rstrip("/"), client.base_url)
|
|
226
|
+
self.assertEqual(1, len(self.posted("readings")))
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
__copyright__ = "Copyright (c) 2026 Alex Laird"
|
|
2
|
+
__license__ = "MIT"
|
|
3
|
+
|
|
4
|
+
import unittest
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
import aiohttp
|
|
8
|
+
from aiohttp import web
|
|
9
|
+
from aiohttp.test_utils import TestServer
|
|
10
|
+
|
|
11
|
+
from pyhearth.client import HearthClient
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class HearthTestCase(unittest.IsolatedAsyncioTestCase):
|
|
15
|
+
API_KEY = "test-hearth-api-key"
|
|
16
|
+
|
|
17
|
+
async def asyncSetUp(self) -> None:
|
|
18
|
+
self.answers: dict[str, tuple[int, str, dict[str, str]]] = {}
|
|
19
|
+
self.posts: dict[str, list[Any]] = {}
|
|
20
|
+
self.authorizations: list[str | None] = []
|
|
21
|
+
|
|
22
|
+
app = web.Application()
|
|
23
|
+
app.router.add_post("/v1/{endpoint}", self._answer)
|
|
24
|
+
self.server = TestServer(app)
|
|
25
|
+
await self.server.start_server()
|
|
26
|
+
self.session = aiohttp.ClientSession()
|
|
27
|
+
|
|
28
|
+
async def asyncTearDown(self) -> None:
|
|
29
|
+
await self.session.close()
|
|
30
|
+
await self.server.close()
|
|
31
|
+
|
|
32
|
+
def given_client(self, **options: Any) -> HearthClient:
|
|
33
|
+
return HearthClient(self.session, self.API_KEY, str(self.server.make_url("/")), **options)
|
|
34
|
+
|
|
35
|
+
def given_hearth_answers(self,
|
|
36
|
+
endpoint: str,
|
|
37
|
+
status: int = 200,
|
|
38
|
+
body: str = "",
|
|
39
|
+
headers: dict[str, str] | None = None) -> None:
|
|
40
|
+
self.answers[endpoint] = (status, body, headers or {})
|
|
41
|
+
|
|
42
|
+
def posted(self, endpoint: str) -> list[Any]:
|
|
43
|
+
return self.posts.get(endpoint, [])
|
|
44
|
+
|
|
45
|
+
async def _answer(self, request: web.Request) -> web.Response:
|
|
46
|
+
endpoint = request.match_info["endpoint"]
|
|
47
|
+
self.posts.setdefault(endpoint, []).append(await request.json())
|
|
48
|
+
self.authorizations.append(request.headers.get("Authorization"))
|
|
49
|
+
status, body, headers = self.answers.get(endpoint, (204, "", {}))
|
|
50
|
+
return web.Response(status=status, text=body, headers=headers)
|