diff --git a/home_assistant_tools.py b/home_assistant_tools.py index 628394d..6c7e4aa 100644 --- a/home_assistant_tools.py +++ b/home_assistant_tools.py @@ -1,13 +1,25 @@ -ha_url: str = Field( - default="https://ha.nasweb.ru", - description="URL Home Assistant (например https://ha.nasweb.ru)", - ) - ha_token: str = Field( - default="", - description="Long-Lived Access Token из профиля HA", - ) - verify_ssl: bool = Field( - default=False, - description="Проверять SSL-сертификат HA (false для самоподписанных, например за reverse-proxy)", - ) - request_timeout: int = Field(default=10, description="Таймаут HTTP-запроса, сек") \ No newline at end of file +def _request(self, method: str, path: str, **kwargs) -> requests.Response: + """HTTP-запрос с ретраями и логированием.""" + url = self.valves.ha_url + path + last_exc: Optional[Exception] = None + # Прокидываем verify_ssl если не передан явно + kwargs.setdefault("verify", self.valves.verify_ssl) + for attempt in range(1, self.valves.max_retries + 2): + try: + resp = requests.request( + method, url, + headers=self._headers(), + timeout=self.valves.request_timeout, + **kwargs, + ) + resp.raise_for_status() + return resp + except requests.RequestException as e: + last_exc = e + logger.warning( + "HA %s %s failed (attempt %d): %s", + method, path, attempt, e, + ) + if attempt <= self.valves.max_retries: + time.sleep(0.5 * attempt) + raise RuntimeError("Home Assistant недоступен: " + str(last_exc)) \ No newline at end of file