From 7113ec6073094a59d4e30b79d524643f14e3e753 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Tue, 13 Nov 2018 10:01:14 +0100 Subject: [PATCH] Fix smhi docstrings (#18414) * Fix docstrings * Fix lint * Fix another typo * Fix mobile phone edit --- homeassistant/components/smhi/__init__.py | 17 +++-- homeassistant/components/smhi/config_flow.py | 32 +++------- homeassistant/components/smhi/const.py | 10 ++- homeassistant/components/weather/smhi.py | 66 +++++++++----------- 4 files changed, 51 insertions(+), 74 deletions(-) diff --git a/homeassistant/components/smhi/__init__.py b/homeassistant/components/smhi/__init__.py index 2421addfd0c5..0ca3bac3e35d 100644 --- a/homeassistant/components/smhi/__init__.py +++ b/homeassistant/components/smhi/__init__.py @@ -1,5 +1,5 @@ """ -Component for the swedish weather institute weather service. +Component for the Swedish weather institute weather service. For more details about this component, please refer to the documentation at https://home-assistant.io/components/smhi/ @@ -7,8 +7,7 @@ https://home-assistant.io/components/smhi/ from homeassistant.config_entries import ConfigEntry from homeassistant.core import Config, HomeAssistant -# Have to import for config_flow to work -# even if they are not used here +# Have to import for config_flow to work even if they are not used here from .config_flow import smhi_locations # noqa: F401 from .const import DOMAIN # noqa: F401 @@ -18,21 +17,21 @@ DEFAULT_NAME = 'smhi' async def async_setup(hass: HomeAssistant, config: Config) -> bool: - """Set up configured smhi.""" + """Set up configured SMHI.""" # We allow setup only through config flow type of config return True -async def async_setup_entry(hass: HomeAssistant, - config_entry: ConfigEntry) -> bool: - """Set up smhi forecast as config entry.""" +async def async_setup_entry( + hass: HomeAssistant, config_entry: ConfigEntry) -> bool: + """Set up SMHI forecast as config entry.""" hass.async_create_task(hass.config_entries.async_forward_entry_setup( config_entry, 'weather')) return True -async def async_unload_entry(hass: HomeAssistant, - config_entry: ConfigEntry) -> bool: +async def async_unload_entry( + hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Unload a config entry.""" await hass.config_entries.async_forward_entry_unload( config_entry, 'weather') diff --git a/homeassistant/components/smhi/config_flow.py b/homeassistant/components/smhi/config_flow.py index e461c6d195db..e1ebf81bac7f 100644 --- a/homeassistant/components/smhi/config_flow.py +++ b/homeassistant/components/smhi/config_flow.py @@ -1,21 +1,11 @@ -"""Config flow to configure smhi component. - -First time the user creates the configuration and -a valid location is set in the hass configuration yaml -it will use that location and use it as default values. - -Additional locations can be added in config form. -The input location will be checked by invoking -the API. Exception will be thrown if the location -is not supported by the API (Swedish locations only) -""" +"""Config flow to configure SMHI component.""" import voluptuous as vol -import homeassistant.helpers.config_validation as cv from homeassistant import config_entries, data_entry_flow -from homeassistant.const import (CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME) +from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import aiohttp_client +import homeassistant.helpers.config_validation as cv from homeassistant.util import slugify from .const import DOMAIN, HOME_LOCATION_NAME @@ -45,9 +35,7 @@ class SmhiFlowHandler(data_entry_flow.FlowHandler): if user_input is not None: is_ok = await self._check_location( - user_input[CONF_LONGITUDE], - user_input[CONF_LATITUDE] - ) + user_input[CONF_LONGITUDE], user_input[CONF_LATITUDE]) if is_ok: name = slugify(user_input[CONF_NAME]) if not self._name_in_configuration_exists(name): @@ -60,9 +48,8 @@ class SmhiFlowHandler(data_entry_flow.FlowHandler): else: self._errors['base'] = 'wrong_location' - # If hass config has the location set and - # is a valid coordinate the default location - # is set as default values in the form + # If hass config has the location set and is a valid coordinate the + # default location is set as default values in the form if not smhi_locations(self.hass): if await self._homeassistant_location_exists(): return await self._show_config_form( @@ -79,8 +66,7 @@ class SmhiFlowHandler(data_entry_flow.FlowHandler): self.hass.config.longitude != 0.0: # Return true if valid location if await self._check_location( - self.hass.config.longitude, - self.hass.config.latitude): + self.hass.config.longitude, self.hass.config.latitude): return True return False @@ -90,9 +76,7 @@ class SmhiFlowHandler(data_entry_flow.FlowHandler): return True return False - async def _show_config_form(self, - name: str = None, - latitude: str = None, + async def _show_config_form(self, name: str = None, latitude: str = None, longitude: str = None): """Show the configuration form to edit location data.""" return self.async_show_form( diff --git a/homeassistant/components/smhi/const.py b/homeassistant/components/smhi/const.py index 49e0f2958739..9689857e546f 100644 --- a/homeassistant/components/smhi/const.py +++ b/homeassistant/components/smhi/const.py @@ -1,12 +1,16 @@ """Constants in smhi component.""" import logging + from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN +ATTR_SMHI_CLOUDINESS = 'cloudiness' + +DOMAIN = 'smhi' + HOME_LOCATION_NAME = 'Home' -ATTR_SMHI_CLOUDINESS = 'cloudiness' -DOMAIN = 'smhi' -LOGGER = logging.getLogger('homeassistant.components.smhi') ENTITY_ID_SENSOR_FORMAT = WEATHER_DOMAIN + ".smhi_{}" ENTITY_ID_SENSOR_FORMAT_HOME = ENTITY_ID_SENSOR_FORMAT.format( HOME_LOCATION_NAME) + +LOGGER = logging.getLogger('homeassistant.components.smhi') diff --git a/homeassistant/components/weather/smhi.py b/homeassistant/components/weather/smhi.py index 41ac15713399..c686b5c90e90 100644 --- a/homeassistant/components/weather/smhi.py +++ b/homeassistant/components/weather/smhi.py @@ -1,33 +1,28 @@ -"""Support for the Swedish weather institute weather service. +""" +Support for the Swedish weather institute weather service. For more details about this platform, please refer to the documentation https://home-assistant.io/components/weather.smhi/ """ - - import asyncio -import logging from datetime import timedelta +import logging from typing import Dict, List import aiohttp import async_timeout +from homeassistant.components.smhi.const import ( + ATTR_SMHI_CLOUDINESS, ENTITY_ID_SENSOR_FORMAT) +from homeassistant.components.weather import ( + ATTR_FORECAST_CONDITION, ATTR_FORECAST_PRECIPITATION, ATTR_FORECAST_TEMP, + ATTR_FORECAST_TEMP_LOW, ATTR_FORECAST_TIME, WeatherEntity) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( - CONF_LATITUDE, CONF_LONGITUDE, - CONF_NAME, TEMP_CELSIUS) + CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, TEMP_CELSIUS) from homeassistant.core import HomeAssistant from homeassistant.helpers import aiohttp_client -from homeassistant.util import dt, slugify, Throttle - -from homeassistant.components.weather import ( - WeatherEntity, ATTR_FORECAST_CONDITION, ATTR_FORECAST_TEMP, - ATTR_FORECAST_TEMP_LOW, ATTR_FORECAST_TIME, - ATTR_FORECAST_PRECIPITATION) - -from homeassistant.components.smhi.const import ( - ENTITY_ID_SENSOR_FORMAT, ATTR_SMHI_CLOUDINESS) +from homeassistant.util import Throttle, dt, slugify DEPENDENCIES = ['smhi'] @@ -51,15 +46,14 @@ CONDITION_CLASSES = { 'exceptional': [], } - # 5 minutes between retrying connect to API again RETRY_TIMEOUT = 5*60 MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=31) -async def async_setup_platform(hass, config, async_add_entities, - discovery_info=None): +async def async_setup_platform( + hass, config, async_add_entities, discovery_info=None): """Old way of setting up components. Can only be called when a user accidentally mentions smhi in the @@ -68,18 +62,18 @@ async def async_setup_platform(hass, config, async_add_entities, pass -async def async_setup_entry(hass: HomeAssistant, - config_entry: ConfigEntry, - config_entries) -> bool: +async def async_setup_entry( + hass: HomeAssistant, config_entry: ConfigEntry, + config_entries) -> bool: """Add a weather entity from map location.""" location = config_entry.data name = slugify(location[CONF_NAME]) session = aiohttp_client.async_get_clientsession(hass) - entity = SmhiWeather(location[CONF_NAME], location[CONF_LATITUDE], - location[CONF_LONGITUDE], - session=session) + entity = SmhiWeather( + location[CONF_NAME], location[CONF_LATITUDE], location[CONF_LONGITUDE], + session=session) entity.entity_id = ENTITY_ID_SENSOR_FORMAT.format(name) config_entries([entity], True) @@ -100,8 +94,7 @@ class SmhiWeather(WeatherEntity): self._longitude = longitude self._forecasts = None self._fail_count = 0 - self._smhi_api = Smhi(self._longitude, self._latitude, - session=session) + self._smhi_api = Smhi(self._longitude, self._latitude, session=session) @Throttle(MIN_TIME_BETWEEN_UPDATES) async def async_update(self) -> None: @@ -109,6 +102,7 @@ class SmhiWeather(WeatherEntity): from smhi.smhi_lib import SmhiForecastException def fail(): + """Postpone updates.""" self._fail_count += 1 if self._fail_count < 3: self.hass.helpers.event.async_call_later( @@ -120,8 +114,8 @@ class SmhiWeather(WeatherEntity): self._fail_count = 0 except (asyncio.TimeoutError, SmhiForecastException): - _LOGGER.error("Failed to connect to SMHI API, " - "retry in 5 minutes") + _LOGGER.error( + "Failed to connect to SMHI API, retry in 5 minutes") fail() async def retry_update(self): @@ -161,7 +155,7 @@ class SmhiWeather(WeatherEntity): """Return the wind speed.""" if self._forecasts is not None: # Convert from m/s to km/h - return round(self._forecasts[0].wind_speed*18/5) + return round(self._forecasts[0].wind_speed * 18 / 5) return None @property @@ -221,17 +215,13 @@ class SmhiWeather(WeatherEntity): # Only get mid day forecasts if forecast.valid_time.hour == 12: data.append({ - ATTR_FORECAST_TIME: - dt.as_local(forecast.valid_time), - ATTR_FORECAST_TEMP: - forecast.temperature_max, - ATTR_FORECAST_TEMP_LOW: - forecast.temperature_min, + ATTR_FORECAST_TIME: dt.as_local(forecast.valid_time), + ATTR_FORECAST_TEMP: forecast.temperature_max, + ATTR_FORECAST_TEMP_LOW: forecast.temperature_min, ATTR_FORECAST_PRECIPITATION: round(forecast.mean_precipitation*24), - ATTR_FORECAST_CONDITION: - condition - }) + ATTR_FORECAST_CONDITION: condition, + }) return data