From 9d79d91baa57268793dc0c0bd5225bd87f924e43 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Fri, 9 Jul 2021 18:35:57 +0200 Subject: [PATCH] Format using black --- custom_components/srf_weather/__init__.py | 8 ++- custom_components/srf_weather/config_flow.py | 55 +++++++++++++------- custom_components/srf_weather/const.py | 2 +- custom_components/srf_weather/weather.py | 38 ++++++++------ 4 files changed, 67 insertions(+), 36 deletions(-) diff --git a/custom_components/srf_weather/__init__.py b/custom_components/srf_weather/__init__.py index e094fbd..378017e 100644 --- a/custom_components/srf_weather/__init__.py +++ b/custom_components/srf_weather/__init__.py @@ -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 diff --git a/custom_components/srf_weather/config_flow.py b/custom_components/srf_weather/config_flow.py index d048526..1ecc7a4 100644 --- a/custom_components/srf_weather/config_flow.py +++ b/custom_components/srf_weather/config_flow.py @@ -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__) @@ -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, ) @@ -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 @@ -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, ) @@ -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, ) diff --git a/custom_components/srf_weather/const.py b/custom_components/srf_weather/const.py index e5bf546..bdce234 100644 --- a/custom_components/srf_weather/const.py +++ b/custom_components/srf_weather/const.py @@ -11,4 +11,4 @@ ERROR_INVALID_CREDENTIALS = "invalid_credentials" ERROR_NO_GEOLOCATION_FOUND = "no_geolocation_found" -ERROR_GEOLOCATION_EXISTS = "geolocation_exists" \ No newline at end of file +ERROR_GEOLOCATION_EXISTS = "geolocation_exists" diff --git a/custom_components/srf_weather/weather.py b/custom_components/srf_weather/weather.py index fabecbb..cb428b9 100644 --- a/custom_components/srf_weather/weather.py +++ b/custom_components/srf_weather/weather.py @@ -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: @@ -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 @@ -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... @@ -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: @@ -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: @@ -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) @@ -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)