From 1b5a036061c51c28924ae57b569c877ebd222196 Mon Sep 17 00:00:00 2001 From: Richie Cahill Date: Wed, 18 Mar 2026 19:32:16 -0400 Subject: [PATCH] httpx conversion --- overlays/default.nix | 2 -- pyproject.toml | 2 -- python/signal_bot/commands/location.py | 6 +++--- python/van_weather/main.py | 12 ++++++------ 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/overlays/default.nix b/overlays/default.nix index a8d0568..b069d6a 100644 --- a/overlays/default.nix +++ b/overlays/default.nix @@ -34,7 +34,6 @@ pytest-mock pytest-xdist python-multipart - requests ruff scalene sqlalchemy @@ -43,7 +42,6 @@ textual tinytuya typer - types-requests websockets ] ); diff --git a/pyproject.toml b/pyproject.toml index 72afef0..17d17fb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,6 @@ dependencies = [ "psycopg[binary]", "pydantic", "pyyaml", - "requests", "sqlalchemy", "typer", "websockets", @@ -37,7 +36,6 @@ dev = [ "pytest-xdist", "pytest", "ruff", - "types-requests", ] [tool.ruff] diff --git a/python/signal_bot/commands/location.py b/python/signal_bot/commands/location.py index be9e132..bb79c96 100644 --- a/python/signal_bot/commands/location.py +++ b/python/signal_bot/commands/location.py @@ -5,7 +5,7 @@ from __future__ import annotations import logging from typing import TYPE_CHECKING, Any -import requests +import httpx if TYPE_CHECKING: from python.signal_bot.models import SignalMessage @@ -18,7 +18,7 @@ def _get_entity_state(ha_url: str, ha_token: str, entity_id: str) -> dict[str, A """Fetch an entity's state from Home Assistant.""" entity_url = f"{ha_url}/api/states/{entity_id}" logger.debug(f"Fetching {entity_url=}") - response = requests.get( + response = httpx.get( entity_url, headers={"Authorization": f"Bearer {ha_token}"}, timeout=30, @@ -48,7 +48,7 @@ def handle_location_request( try: lat_payload = _get_entity_state(ha_url, ha_token, "sensor.van_last_known_latitude") lon_payload = _get_entity_state(ha_url, ha_token, "sensor.van_last_known_longitude") - except requests.RequestException: + except httpx.HTTPError: logger.exception("Couldn't fetch van location from Home Assistant right now.") logger.debug(f"{ha_url=} {lat_payload=} {lon_payload=}") signal.reply(message, "Couldn't fetch van location from Home Assistant right now.") diff --git a/python/van_weather/main.py b/python/van_weather/main.py index 4177df9..ef2952e 100644 --- a/python/van_weather/main.py +++ b/python/van_weather/main.py @@ -4,7 +4,7 @@ import logging from datetime import UTC, datetime from typing import Annotated, Any -import requests +import httpx import typer from apscheduler.schedulers.blocking import BlockingScheduler from tenacity import before_sleep_log, retry, stop_after_attempt, wait_fixed @@ -37,7 +37,7 @@ logger = logging.getLogger(__name__) ) def get_ha_state(url: str, token: str, entity_id: str) -> float: """Get numeric state from Home Asasistant entity.""" - response = requests.get( + response = httpx.get( f"{url}/api/states/{entity_id}", headers={"Authorization": f"Bearer {token}"}, timeout=30, @@ -102,7 +102,7 @@ def parse_hourly_forecast(data: dict[str, dict[str, Any]]) -> list[HourlyForecas def fetch_weather(api_key: str, lat: float, lon: float) -> Weather: """Fetch weather from Pirate Weather API.""" url = f"https://api.pirateweather.net/forecast/{api_key}/{lat},{lon}" - response = requests.get(url, params={"units": "us"}, timeout=30) + response = httpx.get(url, params={"units": "us"}, timeout=30) response.raise_for_status() data = response.json() @@ -204,7 +204,7 @@ def post_to_ha(url: str, token: str, weather: Weather) -> None: for entity_id, data in sensors.items(): if data["state"] is not None: - response = requests.post(f"{url}/api/states/{entity_id}", headers=headers, json=data, timeout=30) + response = httpx.post(f"{url}/api/states/{entity_id}", headers=headers, json=data, timeout=30) response.raise_for_status() # Post daily forecast as JSON attribute sensor @@ -219,7 +219,7 @@ def post_to_ha(url: str, token: str, weather: Weather) -> None: for daily_forecast in weather.daily_forecasts ] - response = requests.post( + response = httpx.post( f"{url}/api/states/sensor.van_weather_forecast_daily", headers=headers, json={"state": len(daily_forecast), "attributes": {"forecast": daily_forecast}}, @@ -238,7 +238,7 @@ def post_to_ha(url: str, token: str, weather: Weather) -> None: for hourly_forecast in weather.hourly_forecasts ] - response = requests.post( + response = httpx.post( f"{url}/api/states/sensor.van_weather_forecast_hourly", headers=headers, json={"state": len(hourly_forecast), "attributes": {"forecast": hourly_forecast}},