mirror of
https://github.com/RichieCahill/dotfiles.git
synced 2026-04-15 04:28:18 -04:00
httpx conversion
This commit is contained in:
@@ -34,7 +34,6 @@
|
||||
pytest-mock
|
||||
pytest-xdist
|
||||
python-multipart
|
||||
requests
|
||||
ruff
|
||||
scalene
|
||||
sqlalchemy
|
||||
@@ -43,7 +42,6 @@
|
||||
textual
|
||||
tinytuya
|
||||
typer
|
||||
types-requests
|
||||
websockets
|
||||
]
|
||||
);
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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.")
|
||||
|
||||
@@ -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}},
|
||||
|
||||
Reference in New Issue
Block a user