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",
"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

View file

@ -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,13 +124,12 @@ class AccuWeatherEntity(WeatherEntity):
@property
def forecast(self):
"""Return the forecast array."""
if self.coordinator.forecast:
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_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),
@ -151,7 +150,6 @@ class AccuWeatherEntity(WeatherEntity):
for item in self.coordinator.data[ATTR_FORECAST]
]
return forecast
return None
async def async_added_to_hass(self):
"""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
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(