Adopt Home Assistant's time zone for the API household clock (task 6351b36d) (#275)
Co-authored-by: Joshua <joshua@cnjmail.com> Co-committed-by: Joshua <joshua@cnjmail.com>
This commit was merged in pull request #275.
This commit is contained in:
@@ -6,6 +6,9 @@ speaks the API's panel plane — ``GET /api/v1/panel/snapshot`` (today's
|
||||
household snapshot) and ``POST /api/v1/panel/instances/{id}/complete``
|
||||
(completion as the tapped child profile) — authenticated with the
|
||||
static panel service token as a Bearer credential on every request.
|
||||
Every request also reports Home Assistant's configured ``time_zone`` in
|
||||
the ``X-NestQuest-Timezone`` header, so the API (which may run UTC)
|
||||
reads the household's clock until the admin chooses a zone there.
|
||||
|
||||
Transport-injected by construction: the constructor takes the base URL,
|
||||
the panel token, and the HTTP session to use. The session speaks the
|
||||
@@ -54,6 +57,7 @@ from .const import (
|
||||
CONF_PANEL_TOKEN,
|
||||
DEFAULT_API_BASE_URL,
|
||||
DEFAULT_PANEL_TOKEN,
|
||||
PANEL_TIMEZONE_HEADER,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -156,7 +160,8 @@ def client_from_entry(hass: Any, entry: Any) -> NestQuestApiClient:
|
||||
"""Build the production client for a config entry.
|
||||
|
||||
Reads the configured base URL and panel token off the entry
|
||||
(:func:`resolve_api_config`) and hands the client Home Assistant's
|
||||
(:func:`resolve_api_config`), reports ``hass.config.time_zone`` as
|
||||
the household zone, and hands the client Home Assistant's
|
||||
SHARED aiohttp session (``homeassistant.helpers.aiohttp_client
|
||||
.async_get_clientsession``) so panel calls ride HA's one connection
|
||||
pool. The helper import is deferred into this function on purpose:
|
||||
@@ -168,7 +173,10 @@ def client_from_entry(hass: Any, entry: Any) -> NestQuestApiClient:
|
||||
|
||||
base_url, panel_token = resolve_api_config(entry)
|
||||
return NestQuestApiClient(
|
||||
base_url, panel_token, async_get_clientsession(hass)
|
||||
base_url,
|
||||
panel_token,
|
||||
async_get_clientsession(hass),
|
||||
household_timezone=hass.config.time_zone,
|
||||
)
|
||||
|
||||
|
||||
@@ -189,9 +197,14 @@ class NestQuestApiClient:
|
||||
session: aiohttp.ClientSession,
|
||||
*,
|
||||
timeout_seconds: float = DEFAULT_TIMEOUT_SECONDS,
|
||||
household_timezone: str | None = None,
|
||||
) -> None:
|
||||
"""Store the connection settings; the session is injected.
|
||||
|
||||
``household_timezone`` is Home Assistant's configured IANA
|
||||
``time_zone``; when given it rides every request in the
|
||||
``X-NestQuest-Timezone`` header.
|
||||
|
||||
``session`` is an aiohttp ``ClientSession`` in production and a
|
||||
stub transport in tests. ``base_url`` must be a full http(s)
|
||||
URL and ``panel_token`` must be non-empty — a client built
|
||||
@@ -216,6 +229,14 @@ class NestQuestApiClient:
|
||||
self._panel_token = panel_token
|
||||
self._session = session
|
||||
self._timeout_seconds = float(timeout_seconds)
|
||||
self._household_timezone = household_timezone
|
||||
|
||||
def _headers(self) -> dict[str, str]:
|
||||
"""The Bearer credential plus the reported household zone."""
|
||||
headers = {"Authorization": f"Bearer {self._panel_token}"}
|
||||
if self._household_timezone:
|
||||
headers[PANEL_TIMEZONE_HEADER] = self._household_timezone
|
||||
return headers
|
||||
|
||||
async def get_snapshot(self) -> dict[str, Any]:
|
||||
"""Fetch and parse today's household snapshot.
|
||||
@@ -275,7 +296,7 @@ class NestQuestApiClient:
|
||||
cleanly and closes the underlying response in every path.
|
||||
"""
|
||||
url = f"{self._base_url}{EVENTS_PATH}"
|
||||
headers = {"Authorization": f"Bearer {self._panel_token}"}
|
||||
headers = self._headers()
|
||||
# Opening the response gets ONE request timeout, exactly like
|
||||
# ``_request_json``; the read loop below runs WITHOUT one — a
|
||||
# body-wide timeout would kill a healthy long-lived stream.
|
||||
@@ -379,7 +400,7 @@ class NestQuestApiClient:
|
||||
raises typed too, carrying the status it arrived with.
|
||||
"""
|
||||
url = f"{self._base_url}{path}"
|
||||
headers = {"Authorization": f"Bearer {self._panel_token}"}
|
||||
headers = self._headers()
|
||||
try:
|
||||
async with asyncio.timeout(self._timeout_seconds):
|
||||
async with self._session.request(
|
||||
|
||||
Reference in New Issue
Block a user