34 lines
830 B
Python
34 lines
830 B
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from dataclasses import dataclass
|
|
|
|
import requests
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class HaClientSettings:
|
|
url: str
|
|
token: str
|
|
timeout_seconds: int = 10
|
|
|
|
|
|
class HaClient:
|
|
def __init__(self, settings: HaClientSettings) -> None:
|
|
self._settings = settings
|
|
self._session = requests.Session()
|
|
self._session.headers.update({
|
|
"Authorization": f"Bearer {settings.token}",
|
|
"Content-Type": "application/json",
|
|
})
|
|
|
|
def list_entities(self) -> list[dict[str, object]]:
|
|
response = self._session.get(
|
|
f"{self._settings.url}/api/states",
|
|
timeout=self._settings.timeout_seconds,
|
|
)
|
|
response.raise_for_status()
|
|
return response.json()
|