Skip to content

Commit

Permalink
Clear method names and arguments (#183)
Browse files Browse the repository at this point in the history
* Rename async_get_forecast method to async_get_daily_forecast

* Rename async_get_forecast_hourly method to async_get_hourly_forecast

* Update tests
  • Loading branch information
bieniu committed May 15, 2023
1 parent 8b77ecc commit 73c5087
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
22 changes: 13 additions & 9 deletions accuweather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

from .const import (
ATTR_CURRENT_CONDITIONS,
ATTR_FORECAST_DAILY_5,
ATTR_FORECAST_HOURLY_12,
ATTR_FORECAST_DAILY,
ATTR_FORECAST_HOURLY,
ATTR_GEOPOSITION,
ENDPOINT,
HTTP_HEADERS,
Expand Down Expand Up @@ -190,7 +190,9 @@ async def async_get_current_conditions(self) -> dict[str, Any]:
data = await self._async_get_data(url)
return self._clean_current_condition(data, REMOVE_FROM_CURRENT_CONDITION)

async def async_get_forecast(self, metric: bool = True) -> list[dict[str, Any]]:
async def async_get_daily_forecast(
self, days: int = 5, metric: bool = True
) -> list[dict[str, Any]]:
"""Retrieve daily forecast data from AccuWeather."""
if not self._location_key:
await self.async_get_location()
Expand All @@ -199,16 +201,17 @@ async def async_get_forecast(self, metric: bool = True) -> list[dict[str, Any]]:
assert self._location_key is not None

url = self._construct_url(
ATTR_FORECAST_DAILY_5,
ATTR_FORECAST_DAILY,
api_key=self._api_key,
location_key=self._location_key,
metric=str(metric),
days=str(days),
metric=str(metric).lower(),
)
data = await self._async_get_data(url)
return self._parse_forecast_daily(data, REMOVE_FROM_FORECAST)

async def async_get_forecast_hourly(
self, metric: bool = True
async def async_get_hourly_forecast(
self, hours: int = 12, metric: bool = True
) -> list[dict[str, Any]]:
"""Retrieve hourly forecast data from AccuWeather."""
if not self._location_key:
Expand All @@ -218,10 +221,11 @@ async def async_get_forecast_hourly(
assert self._location_key is not None

url = self._construct_url(
ATTR_FORECAST_HOURLY_12,
ATTR_FORECAST_HOURLY,
api_key=self._api_key,
location_key=self._location_key,
metric=str(metric),
hours=str(hours),
metric=str(metric).lower(),
)
data = await self._async_get_data(url)
return self._parse_forecast_hourly(data, REMOVE_FROM_FORECAST)
Expand Down
8 changes: 4 additions & 4 deletions accuweather/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from __future__ import annotations

ATTR_CURRENT_CONDITIONS: str = "currentconditions"
ATTR_FORECAST_DAILY_5: str = "forecasts"
ATTR_FORECAST_HOURLY_12: str = "forecasts_hourly"
ATTR_FORECAST_DAILY: str = "forecasts"
ATTR_FORECAST_HOURLY: str = "forecasts_hourly"
ATTR_GEOPOSITION: str = "geoposition"

MAX_API_KEY_LENGTH = 32
Expand Down Expand Up @@ -32,6 +32,6 @@
URLS: dict[str, str] = {
ATTR_GEOPOSITION: "locations/v1/cities/geoposition/search?apikey={api_key}&q={lat}%2C{lon}",
ATTR_CURRENT_CONDITIONS: "currentconditions/v1/{location_key}?apikey={api_key}&details=true",
ATTR_FORECAST_DAILY_5: "forecasts/v1/daily/5day/{location_key}?apikey={api_key}&details=true&metric={metric}",
ATTR_FORECAST_HOURLY_12: "forecasts/v1/hourly/12hour/{location_key}?apikey={api_key}&details=true&metric={metric}",
ATTR_FORECAST_DAILY: "forecasts/v1/daily/{days}day/{location_key}?apikey={api_key}&details=true&metric={metric}",
ATTR_FORECAST_HOURLY: "forecasts/v1/hourly/{hours}hour/{location_key}?apikey={api_key}&details=true&metric={metric}",
}
10 changes: 7 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ async def main():
API_KEY, websession, latitude=LATITUDE, longitude=LONGITUDE
)
current_conditions = await accuweather.async_get_current_conditions()
forecast = await accuweather.async_get_forecast(metric=True)
forecast_hourly = await accuweather.async_get_forecast_hourly(metric=True)
forecast_daily = await accuweather.async_get_daily_forecast(
days=5, metric=True
)
forecast_hourly = await accuweather.async_get_hourly_forecast(
hours=12, metric=True
)
except (
ApiError,
InvalidApiKeyError,
Expand All @@ -42,7 +46,7 @@ async def main():
print(f"Location: {accuweather.location_name} ({accuweather.location_key})")
print(f"Requests remaining: {accuweather.requests_remaining}")
print(f"Current: {current_conditions}")
print(f"Forecast: {forecast}")
print(f"Forecast: {forecast_daily}")
print(f"Forecast hourly: {forecast_hourly}")


Expand Down
14 changes: 7 additions & 7 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ async def test_get_current_conditions():


@pytest.mark.asyncio
async def test_get_forecast():
"""Test with valid forecast data."""
async def test_get_daily_forecast():
"""Test with valid daily forecast data."""
with open("tests/fixtures/forecast_data.json", encoding="utf-8") as file:
forecast_data = json.load(file)
with open("tests/fixtures/location_data.json", encoding="utf-8") as file:
Expand All @@ -100,7 +100,7 @@ async def test_get_forecast():

with aioresponses() as session_mock:
session_mock.get(
"https://dataservice.accuweather.com/forecasts/v1/daily/5day/268068?apikey=32-character-string-1234567890qw&details=true&metric=True",
"https://dataservice.accuweather.com/forecasts/v1/daily/5day/268068?apikey=32-character-string-1234567890qw&details=true&metric=true",
payload=forecast_data,
headers=HEADERS,
)
Expand All @@ -113,7 +113,7 @@ async def test_get_forecast():
accuweather = AccuWeather(
VALID_API_KEY, session, latitude=LATITUDE, longitude=LONGITUDE
)
forecast = await accuweather.async_get_forecast()
forecast = await accuweather.async_get_daily_forecast()

await session.close()

Expand All @@ -127,7 +127,7 @@ async def test_get_forecast():

@pytest.mark.asyncio
async def test_get_hourly_forecast():
"""Test with valid hourly_forecast data."""
"""Test with valid hourly forecast data."""
with open("tests/fixtures/hourly_forecast_data.json", encoding="utf-8") as file:
hourly_forecast_data = json.load(file)
with open("tests/fixtures/location_data.json", encoding="utf-8") as file:
Expand All @@ -137,7 +137,7 @@ async def test_get_hourly_forecast():

with aioresponses() as session_mock:
session_mock.get(
"https://dataservice.accuweather.com/forecasts/v1/hourly/12hour/268068?apikey=32-character-string-1234567890qw&details=true&metric=True",
"https://dataservice.accuweather.com/forecasts/v1/hourly/12hour/268068?apikey=32-character-string-1234567890qw&details=true&metric=true",
payload=hourly_forecast_data,
headers=HEADERS,
)
Expand All @@ -150,7 +150,7 @@ async def test_get_hourly_forecast():
accuweather = AccuWeather(
VALID_API_KEY, session, latitude=LATITUDE, longitude=LONGITUDE
)
forecast = await accuweather.async_get_forecast_hourly()
forecast = await accuweather.async_get_hourly_forecast()

await session.close()

Expand Down

0 comments on commit 73c5087

Please sign in to comment.