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
This commit is contained in:
Maciej Bieniek 2020-07-27 03:00:47 +02:00 committed by GitHub
parent 8abdc2c969
commit 2b0914994d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 34 deletions

View File

@ -1,7 +1,7 @@
{ {
"domain": "accuweather", "domain": "accuweather",
"name": "AccuWeather", "name": "AccuWeather",
"documentation": "https://github.com/bieniu/ha-accuweather", "documentation": "https://www.home-assistant.io/integrations/accuweather/",
"requirements": ["accuweather==0.0.9"], "requirements": ["accuweather==0.0.9"],
"codeowners": ["@bieniu"], "codeowners": ["@bieniu"],
"config_flow": true "config_flow": true

View File

@ -12,7 +12,7 @@ from homeassistant.components.weather import (
ATTR_FORECAST_WIND_SPEED, ATTR_FORECAST_WIND_SPEED,
WeatherEntity, 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 homeassistant.util.dt import utc_from_timestamp
from .const import ATTR_FORECAST, ATTRIBUTION, CONDITION_CLASSES, COORDINATOR, DOMAIN from .const import ATTR_FORECAST, ATTRIBUTION, CONDITION_CLASSES, COORDINATOR, DOMAIN
@ -74,7 +74,7 @@ class AccuWeatherEntity(WeatherEntity):
if self.coordinator.data["WeatherIcon"] in v if self.coordinator.data["WeatherIcon"] in v
][0] ][0]
except IndexError: except IndexError:
return STATE_UNKNOWN return None
@property @property
def temperature(self): def temperature(self):
@ -124,34 +124,32 @@ class AccuWeatherEntity(WeatherEntity):
@property @property
def forecast(self): def forecast(self):
"""Return the forecast array.""" """Return the forecast array."""
if self.coordinator.forecast: if not self.coordinator.forecast:
# remap keys from library to keys understood by the weather component return None
forecast = [ # remap keys from library to keys understood by the weather component
{ forecast = [
ATTR_FORECAST_TIME: utc_from_timestamp( {
item["EpochDate"] ATTR_FORECAST_TIME: utc_from_timestamp(item["EpochDate"]).isoformat(),
).isoformat(), ATTR_FORECAST_TEMP: item["TemperatureMax"]["Value"],
ATTR_FORECAST_TEMP: item["TemperatureMax"]["Value"], ATTR_FORECAST_TEMP_LOW: item["TemperatureMin"]["Value"],
ATTR_FORECAST_TEMP_LOW: item["TemperatureMin"]["Value"], ATTR_FORECAST_PRECIPITATION: self._calc_precipitation(item),
ATTR_FORECAST_PRECIPITATION: self._calc_precipitation(item), ATTR_FORECAST_PRECIPITATION_PROBABILITY: round(
ATTR_FORECAST_PRECIPITATION_PROBABILITY: round( mean(
mean( [
[ item["PrecipitationProbabilityDay"],
item["PrecipitationProbabilityDay"], item["PrecipitationProbabilityNight"],
item["PrecipitationProbabilityNight"], ]
] )
) ),
), ATTR_FORECAST_WIND_SPEED: item["WindDay"]["Speed"]["Value"],
ATTR_FORECAST_WIND_SPEED: item["WindDay"]["Speed"]["Value"], ATTR_FORECAST_WIND_BEARING: item["WindDay"]["Direction"]["Degrees"],
ATTR_FORECAST_WIND_BEARING: item["WindDay"]["Direction"]["Degrees"], ATTR_FORECAST_CONDITION: [
ATTR_FORECAST_CONDITION: [ k for k, v in CONDITION_CLASSES.items() if item["IconDay"] in v
k for k, v in CONDITION_CLASSES.items() if item["IconDay"] in v ][0],
][0], }
} for item in self.coordinator.data[ATTR_FORECAST]
for item in self.coordinator.data[ATTR_FORECAST] ]
] return forecast
return forecast
return None
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Connect to dispatcher listening for entity data notifications.""" """Connect to dispatcher listening for entity data notifications."""

View File

@ -29,8 +29,10 @@ async def test_show_form(hass):
assert result["step_id"] == SOURCE_USER assert result["step_id"] == SOURCE_USER
async def test_invalid_api_key_1(hass): async def test_api_key_too_short(hass):
"""Test that errors are shown when API key is invalid.""" """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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": SOURCE_USER}, 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"} 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.""" """Test that errors are shown when API key is invalid."""
with patch( with patch(
"accuweather.AccuWeather._async_get_data", "accuweather.AccuWeather._async_get_data",
@ -112,6 +114,8 @@ async def test_create_entry(hass):
with patch( with patch(
"accuweather.AccuWeather._async_get_data", "accuweather.AccuWeather._async_get_data",
return_value=json.loads(load_fixture("accuweather/location_data.json")), 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( result = await hass.config_entries.flow.async_init(