From 2b0914994de62333965ba346883c56f2b336ffb2 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Mon, 27 Jul 2020 03:00:47 +0200 Subject: [PATCH] Add changes from comments after merging AccuWeather (#38227) * Fix documentation url * Return None instead STATE_UNKNOWN * Invert forecast check * Patch async_setup_entry in test_create entry * Improve test name, docstring and add comment --- .../components/accuweather/manifest.json | 2 +- .../components/accuweather/weather.py | 58 +++++++++---------- .../accuweather/test_config_flow.py | 10 +++- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/accuweather/manifest.json b/homeassistant/components/accuweather/manifest.json index 3b74087a61d..4e54d937dee 100644 --- a/homeassistant/components/accuweather/manifest.json +++ b/homeassistant/components/accuweather/manifest.json @@ -1,7 +1,7 @@ { "domain": "accuweather", "name": "AccuWeather", - "documentation": "https://github.com/bieniu/ha-accuweather", + "documentation": "https://www.home-assistant.io/integrations/accuweather/", "requirements": ["accuweather==0.0.9"], "codeowners": ["@bieniu"], "config_flow": true diff --git a/homeassistant/components/accuweather/weather.py b/homeassistant/components/accuweather/weather.py index 866f4821b02..234c03d9e97 100644 --- a/homeassistant/components/accuweather/weather.py +++ b/homeassistant/components/accuweather/weather.py @@ -12,7 +12,7 @@ from homeassistant.components.weather import ( ATTR_FORECAST_WIND_SPEED, WeatherEntity, ) -from homeassistant.const import CONF_NAME, STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT +from homeassistant.const import CONF_NAME, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.util.dt import utc_from_timestamp from .const import ATTR_FORECAST, ATTRIBUTION, CONDITION_CLASSES, COORDINATOR, DOMAIN @@ -74,7 +74,7 @@ class AccuWeatherEntity(WeatherEntity): if self.coordinator.data["WeatherIcon"] in v ][0] except IndexError: - return STATE_UNKNOWN + return None @property def temperature(self): @@ -124,34 +124,32 @@ class AccuWeatherEntity(WeatherEntity): @property def forecast(self): """Return the forecast array.""" - if self.coordinator.forecast: - # remap keys from library to keys understood by the weather component - forecast = [ - { - ATTR_FORECAST_TIME: utc_from_timestamp( - item["EpochDate"] - ).isoformat(), - ATTR_FORECAST_TEMP: item["TemperatureMax"]["Value"], - ATTR_FORECAST_TEMP_LOW: item["TemperatureMin"]["Value"], - ATTR_FORECAST_PRECIPITATION: self._calc_precipitation(item), - ATTR_FORECAST_PRECIPITATION_PROBABILITY: round( - mean( - [ - item["PrecipitationProbabilityDay"], - item["PrecipitationProbabilityNight"], - ] - ) - ), - ATTR_FORECAST_WIND_SPEED: item["WindDay"]["Speed"]["Value"], - ATTR_FORECAST_WIND_BEARING: item["WindDay"]["Direction"]["Degrees"], - ATTR_FORECAST_CONDITION: [ - k for k, v in CONDITION_CLASSES.items() if item["IconDay"] in v - ][0], - } - for item in self.coordinator.data[ATTR_FORECAST] - ] - return forecast - return None + if not self.coordinator.forecast: + return None + # remap keys from library to keys understood by the weather component + forecast = [ + { + ATTR_FORECAST_TIME: utc_from_timestamp(item["EpochDate"]).isoformat(), + ATTR_FORECAST_TEMP: item["TemperatureMax"]["Value"], + ATTR_FORECAST_TEMP_LOW: item["TemperatureMin"]["Value"], + ATTR_FORECAST_PRECIPITATION: self._calc_precipitation(item), + ATTR_FORECAST_PRECIPITATION_PROBABILITY: round( + mean( + [ + item["PrecipitationProbabilityDay"], + item["PrecipitationProbabilityNight"], + ] + ) + ), + ATTR_FORECAST_WIND_SPEED: item["WindDay"]["Speed"]["Value"], + ATTR_FORECAST_WIND_BEARING: item["WindDay"]["Direction"]["Degrees"], + ATTR_FORECAST_CONDITION: [ + k for k, v in CONDITION_CLASSES.items() if item["IconDay"] in v + ][0], + } + for item in self.coordinator.data[ATTR_FORECAST] + ] + return forecast async def async_added_to_hass(self): """Connect to dispatcher listening for entity data notifications.""" diff --git a/tests/components/accuweather/test_config_flow.py b/tests/components/accuweather/test_config_flow.py index 399a69902e1..6b7430e524d 100644 --- a/tests/components/accuweather/test_config_flow.py +++ b/tests/components/accuweather/test_config_flow.py @@ -29,8 +29,10 @@ async def test_show_form(hass): assert result["step_id"] == SOURCE_USER -async def test_invalid_api_key_1(hass): - """Test that errors are shown when API key is invalid.""" +async def test_api_key_too_short(hass): + """Test that errors are shown when API key is too short.""" + # The API key length check is done by the library without polling the AccuWeather + # server so we don't need to patch the library method. result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, @@ -45,7 +47,7 @@ async def test_invalid_api_key_1(hass): assert result["errors"] == {CONF_API_KEY: "invalid_api_key"} -async def test_invalid_api_key_2(hass): +async def test_invalid_api_key(hass): """Test that errors are shown when API key is invalid.""" with patch( "accuweather.AccuWeather._async_get_data", @@ -112,6 +114,8 @@ async def test_create_entry(hass): with patch( "accuweather.AccuWeather._async_get_data", return_value=json.loads(load_fixture("accuweather/location_data.json")), + ), patch( + "homeassistant.components.accuweather.async_setup_entry", return_value=True ): result = await hass.config_entries.flow.async_init(