Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions homeassistant/components/accuweather/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
from homeassistant.const import CONF_API_KEY, CONF_CITY, CONF_NAME
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.schema_config_entry_flow import (
SchemaFlowFormStep,
SchemaOptionsFlowHandler,
Expand All @@ -37,7 +36,7 @@
class AccuWeatherFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for AccuWeather."""

VERSION = 1
VERSION = 2

async def async_step_user(
self, user_input: dict[str, Any] | None = None
Expand All @@ -53,12 +52,27 @@ async def async_step_user(
if user_input is not None:
websession = async_get_clientsession(self.hass)
try:
async with timeout(10):
async with timeout(20):
# Get the location key by querying for the city
city = user_input[CONF_CITY]
url1 = f"http://dataservice.accuweather.com/locations/v1/cities/search?apikey={user_input[CONF_API_KEY]}&q={city}"
location_key_request = await websession.get(url1)
location_key_response = await location_key_request.json()
location_key = location_key_response[0]["Key"]

# Get the lat and long from the location_key
url2 = f"http://dataservice.accuweather.com/locations/v1/{location_key}?apikey={user_input[CONF_API_KEY]}"
lat_long_request = await websession.get(url2)
lat_long_response = await lat_long_request.json()
latitude = lat_long_response["GeoPosition"]["Latitude"]
longitude = lat_long_response["GeoPosition"]["Longitude"]

# Use the location key to fetch the weather data
accuweather = AccuWeather(
user_input[CONF_API_KEY],
websession,
latitude=user_input[CONF_LATITUDE],
longitude=user_input[CONF_LONGITUDE],
latitude=latitude,
longitude=longitude,
)
await accuweather.async_get_location()
except (ApiError, ClientConnectorError, asyncio.TimeoutError, ClientError):
Expand All @@ -72,21 +86,14 @@ async def async_step_user(
accuweather.location_key, raise_on_progress=False
)

return self.async_create_entry(
title=user_input[CONF_NAME], data=user_input
)
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)

return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_API_KEY): str,
vol.Optional(
CONF_LATITUDE, default=self.hass.config.latitude
): cv.latitude,
vol.Optional(
CONF_LONGITUDE, default=self.hass.config.longitude
): cv.longitude,
vol.Required(CONF_CITY, default=self.hass.config.city): str,
vol.Optional(
CONF_NAME, default=self.hass.config.location_name
): str,
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/accuweather/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.const import CONF_API_KEY, CONF_CITY, CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant

from . import AccuWeatherDataUpdateCoordinator
from .const import DOMAIN

TO_REDACT = {CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE}
TO_REDACT = {CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_CITY}


async def async_get_config_entry_diagnostics(
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
CONF_ALLOWLIST_EXTERNAL_URLS,
CONF_AUTH_MFA_MODULES,
CONF_AUTH_PROVIDERS,
CONF_CITY,
CONF_COUNTRY,
CONF_CURRENCY,
CONF_CUSTOMIZE,
Expand Down Expand Up @@ -248,6 +249,7 @@ def _validate_currency(data: Any) -> Any:
CUSTOMIZE_CONFIG_SCHEMA.extend(
{
CONF_NAME: vol.Coerce(str),
CONF_CITY: vol.Coerce(str),
CONF_LATITUDE: cv.latitude,
CONF_LONGITUDE: cv.longitude,
CONF_ELEVATION: vol.Coerce(int),
Expand Down Expand Up @@ -570,6 +572,7 @@ async def async_process_ha_core_config(hass: HomeAssistant, config: dict) -> Non
if any(
k in config
for k in (
CONF_CITY,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
Expand All @@ -586,6 +589,7 @@ async def async_process_ha_core_config(hass: HomeAssistant, config: dict) -> Non
hac.config_source = ConfigSource.YAML

for key, attr in (
(CONF_CITY, "city"),
(CONF_LATITUDE, "latitude"),
(CONF_LONGITUDE, "longitude"),
(CONF_NAME, "location_name"),
Expand Down
1 change: 1 addition & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class Platform(StrEnum):
CONF_IP_ADDRESS: Final = "ip_address"
CONF_LANGUAGE: Final = "language"
CONF_LATITUDE: Final = "latitude"
CONF_CITY: Final = "city"
CONF_LEGACY_TEMPLATES: Final = "legacy_templates"
CONF_LIGHTS: Final = "lights"
CONF_LOCATION: Final = "location"
Expand Down
8 changes: 7 additions & 1 deletion homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2087,7 +2087,7 @@ def __init__(self, hass: HomeAssistant, config_dir: str) -> None:
self.hass = hass

self._store = self._ConfigStore(self.hass)

self.city: str = "City"
self.latitude: float = 0
self.longitude: float = 0

Expand Down Expand Up @@ -2194,6 +2194,7 @@ def as_dict(self) -> dict[str, Any]:
Async friendly.
"""
return {
"city": self.city,
"latitude": self.latitude,
"longitude": self.longitude,
"elevation": self.elevation,
Expand Down Expand Up @@ -2230,6 +2231,7 @@ def _update(
self,
*,
source: ConfigSource,
city: str | None = None,
latitude: float | None = None,
longitude: float | None = None,
elevation: int | None = None,
Expand All @@ -2245,6 +2247,8 @@ def _update(
) -> None:
"""Update the configuration from a dictionary."""
self.config_source = source
if city is not None:
self.city = city
if latitude is not None:
self.latitude = latitude
if longitude is not None:
Expand Down Expand Up @@ -2307,6 +2311,7 @@ async def async_load(self) -> None:

self._update(
source=ConfigSource.STORAGE,
city=data.get("city"),
latitude=data.get("latitude"),
longitude=data.get("longitude"),
elevation=data.get("elevation"),
Expand All @@ -2323,6 +2328,7 @@ async def async_load(self) -> None:
async def _async_store(self) -> None:
"""Store [homeassistant] core config."""
data = {
"city": self.city,
"latitude": self.latitude,
"longitude": self.longitude,
"elevation": self.elevation,
Expand Down