Skip to content

Commit

Permalink
Format using black
Browse files Browse the repository at this point in the history
  • Loading branch information
agners authored and siku2 committed Jul 9, 2021
1 parent 22cf459 commit 9d79d91
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 36 deletions.
8 changes: 6 additions & 2 deletions custom_components/srf_weather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:


async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry) -> bool:
hass.async_create_task(hass.config_entries.async_forward_entry_setup(config_entry, WEATHER_DOMAIN))
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, WEATHER_DOMAIN)
)
return True


async def async_unload_entry(hass: HomeAssistantType, config_entry: ConfigEntry) -> bool:
async def async_unload_entry(
hass: HomeAssistantType, config_entry: ConfigEntry
) -> bool:
await hass.config_entries.async_forward_entry_unload(config_entry, WEATHER_DOMAIN)
return True
55 changes: 37 additions & 18 deletions custom_components/srf_weather/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
from homeassistant.helpers.typing import HomeAssistantType
from typing import Dict

from .const import CONF_CONSUMER_KEY, CONF_CONSUMER_SECRET, CONF_GEOLOCATION_ID, DOMAIN, ERROR_INVALID_CREDENTIALS, ERROR_GEOLOCATION_EXISTS, \
ERROR_NO_GEOLOCATION_FOUND, HOME_LOCATION_NAME
from .const import (
CONF_CONSUMER_KEY,
CONF_CONSUMER_SECRET,
CONF_GEOLOCATION_ID,
DOMAIN,
ERROR_INVALID_CREDENTIALS,
ERROR_GEOLOCATION_EXISTS,
ERROR_NO_GEOLOCATION_FOUND,
HOME_LOCATION_NAME,
)
from .weather import request_access_token, get_geolocation_ids

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -48,10 +56,12 @@ async def async_step_credentials(self, user_input: dict = None) -> dict:

return self.async_show_form(
step_id="credentials",
data_schema=vol.Schema({
vol.Required(CONF_CONSUMER_KEY): str,
vol.Required(CONF_CONSUMER_SECRET): str,
}),
data_schema=vol.Schema(
{
vol.Required(CONF_CONSUMER_KEY): str,
vol.Required(CONF_CONSUMER_SECRET): str,
}
),
errors=errors,
)

Expand All @@ -61,10 +71,14 @@ async def async_step_location(self, user_input: dict = None) -> dict:
if user_input is not None:
latitude = user_input[CONF_LATITUDE]
longitude = user_input[CONF_LONGITUDE]
geolocations = await get_geolocation_ids(self.hass, self._credentials, latitude, longitude)
geolocations = await get_geolocation_ids(
self.hass, self._credentials, latitude, longitude
)

if geolocations is None or len(geolocations) == 0:
logger.debug("No geolocation found for coordinates %f, %f", latitude, longitude)
logger.debug(
"No geolocation found for coordinates %f, %f", latitude, longitude
)
errors[CONF_BASE] = ERROR_NO_GEOLOCATION_FOUND
else:
self._location = user_input
Expand All @@ -75,11 +89,17 @@ async def async_step_location(self, user_input: dict = None) -> dict:
logger.debug("Show again, with errors %s", errors)
return self.async_show_form(
step_id="location",
data_schema=vol.Schema({
vol.Required(CONF_NAME, default=HOME_LOCATION_NAME): str,
vol.Required(CONF_LATITUDE, default=hass_config.latitude): cv.latitude,
vol.Required(CONF_LONGITUDE, default=hass_config.longitude): cv.longitude,
}),
data_schema=vol.Schema(
{
vol.Required(CONF_NAME, default=HOME_LOCATION_NAME): str,
vol.Required(
CONF_LATITUDE, default=hass_config.latitude
): cv.latitude,
vol.Required(
CONF_LONGITUDE, default=hass_config.longitude
): cv.longitude,
}
),
errors=errors,
)

Expand All @@ -97,16 +117,15 @@ async def async_step_geolocationid(self, user_input: dict = None) -> dict:
return self.async_create_entry(title=data[CONF_NAME], data=data)

geolocations = {
geoloc["id"]: geoloc["default_name"]
for geoloc in self._geolocations
geoloc["id"]: geoloc["default_name"] for geoloc in self._geolocations
}
logger.debug(geolocations)

return self.async_show_form(
step_id="geolocationid",
data_schema=vol.Schema({
vol.Required(CONF_GEOLOCATION_ID): vol.In(geolocations)
}),
data_schema=vol.Schema(
{vol.Required(CONF_GEOLOCATION_ID): vol.In(geolocations)}
),
errors=errors,
)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/srf_weather/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

ERROR_INVALID_CREDENTIALS = "invalid_credentials"
ERROR_NO_GEOLOCATION_FOUND = "no_geolocation_found"
ERROR_GEOLOCATION_EXISTS = "geolocation_exists"
ERROR_GEOLOCATION_EXISTS = "geolocation_exists"
38 changes: 23 additions & 15 deletions custom_components/srf_weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def async_setup_entry(
URL_OAUTH = API_URL + "/oauth/v1/accesstoken"

URL_FORECASTS = API_URL + "/srf-meteo/forecast/{geolocationId}"
URL_GEOLOCATION= API_URL + "/srf-meteo/geolocations"
URL_GEOLOCATION = API_URL + "/srf-meteo/geolocations"


def _check_client_credentials_response(d: dict) -> None:
Expand Down Expand Up @@ -132,10 +132,7 @@ async def _get(hass, api_data: dict, url: str, **kwargs) -> dict:


async def get_geolocation_ids(hass, api_data: dict, latitude: float, longitude: float):
coordinates = {
"latitude": latitude,
"longitude": longitude
}
coordinates = {"latitude": latitude, "longitude": longitude}
data = await _get(hass, api_data, URL_GEOLOCATION, params=coordinates)
logger.debug(data)
return data
Expand Down Expand Up @@ -237,7 +234,9 @@ async def __update(self) -> None:
try:
fdate, hour = parse_forecast_hour(raw_hour)
except Exception:
logger.warning(f"failed to parse hourly forecast: {raw_hour}", exc_info=True)
logger.warning(
f"failed to parse hourly forecast: {raw_hour}", exc_info=True
)
continue

# Don't care about the past...
Expand All @@ -254,7 +253,10 @@ async def __update(self) -> None:
try:
fdate, three_hour = parse_forecast_hour(raw_three_hour)
except Exception:
logger.warning(f"failed to parse triple-hourly forecast: {raw_three_hour}", exc_info=True)
logger.warning(
f"failed to parse triple-hourly forecast: {raw_three_hour}",
exc_info=True,
)
continue

if fdate <= hourly_split:
Expand All @@ -269,7 +271,9 @@ async def __update(self) -> None:
try:
fdate, day = parse_forecast_day(raw_day)
except Exception:
logger.warning(f"failed to parse daily forecast: {raw_day}", exc_info=True)
logger.warning(
f"failed to parse daily forecast: {raw_day}", exc_info=True
)
continue

if fdate <= triple_hour_split:
Expand Down Expand Up @@ -337,10 +341,12 @@ def parse_forecast_day(day: dict) -> Tuple[datetime, dict]:
temp_high = float(day["TX_C"])
temp_low = float(day["TN_C"])

data.update({
"temperature": temp_high,
"templow": temp_low,
})
data.update(
{
"temperature": temp_high,
"templow": temp_low,
}
)

return (date, data)

Expand All @@ -350,9 +356,11 @@ def parse_forecast_hour(hour: dict) -> Tuple[datetime, dict]:

temperature = float(hour["TTT_C"])

data.update({
"temperature": temperature,
})
data.update(
{
"temperature": temperature,
}
)

return (date, data)

Expand Down

0 comments on commit 9d79d91

Please sign in to comment.