Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate integration to new library #116232

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion homeassistant/components/openweathermap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
from typing import Any

from pyopenweathermap import OWMClient
from pyowm import OWM
from pyowm.utils.config import get_default_config

Expand Down Expand Up @@ -45,9 +46,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

config_dict = _get_owm_config(language)

owm_client = OWMClient(api_key, lang=language)
owm = OWM(api_key, config_dict).weather_manager()
weather_coordinator = WeatherUpdateCoordinator(
owm, latitude, longitude, forecast_mode, hass
owm_client, owm, latitude, longitude, forecast_mode, hass
)

await weather_coordinator.async_config_entry_first_refresh()
Expand Down
30 changes: 15 additions & 15 deletions homeassistant/components/openweathermap/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

from __future__ import annotations

from pyowm import OWM
from pyowm.commons.exceptions import APIRequestError, UnauthorizedError
from pyopenweathermap import OWMClient, RequestError
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.const import (
CONF_API_KEY,
CONF_LANGUAGE,
Expand Down Expand Up @@ -42,7 +46,7 @@ def async_get_options_flow(
"""Get the options flow for this handler."""
return OpenWeatherMapOptionsFlow(config_entry)

async def async_step_user(self, user_input=None):
async def async_step_user(self, user_input=None) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
errors = {}

Expand All @@ -54,14 +58,10 @@ async def async_step_user(self, user_input=None):
self._abort_if_unique_id_configured()

try:
api_online = await _is_owm_api_online(
self.hass, user_input[CONF_API_KEY], latitude, longitude
)
if not api_online:
api_key_valid = await _is_owm_api_key_valid(user_input[CONF_API_KEY])
if not api_key_valid:
errors["base"] = "invalid_api_key"
except UnauthorizedError:
errors["base"] = "invalid_api_key"
except APIRequestError:
except RequestError:
errors["base"] = "cannot_connect"

if not errors:
Expand Down Expand Up @@ -98,7 +98,7 @@ def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry

async def async_step_init(self, user_input=None):
async def async_step_init(self, user_input=None) -> ConfigFlowResult:
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
Expand Down Expand Up @@ -129,6 +129,6 @@ def _get_options_schema(self):
)


async def _is_owm_api_online(hass, api_key, lat, lon):
owm = OWM(api_key).weather_manager()
return await hass.async_add_executor_job(owm.weather_at_coords, lat, lon)
async def _is_owm_api_key_valid(api_key):
owm_client = OWMClient(api_key)
return await owm_client.validate_key()
3 changes: 3 additions & 0 deletions homeassistant/components/openweathermap/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
ATTR_API_VISIBILITY_DISTANCE = "visibility_distance"
ATTR_API_WEATHER_CODE = "weather_code"
ATTR_API_FORECAST = "forecast"
ATTR_API_CURRENT = "current"
ATTR_API_HOURLY_FORECAST = "hourly_forecast"
ATTR_API_DAILY_FORECAST = "daily_forecast"
UPDATE_LISTENER = "update_listener"
PLATFORMS = [Platform.SENSOR, Platform.WEATHER]

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/openweathermap/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/openweathermap",
"iot_class": "cloud_polling",
"loggers": ["geojson", "pyowm", "pysocks"],
"requirements": ["pyowm==3.2.0"]
"loggers": ["geojson", "pyopenweathermap", "pysocks"],
"requirements": ["pyopenweathermap==0.0.1"]
}
5 changes: 4 additions & 1 deletion homeassistant/components/openweathermap/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from .const import (
ATTR_API_CLOUDS,
ATTR_API_CONDITION,
ATTR_API_CURRENT,
ATTR_API_DEW_POINT,
ATTR_API_FEELS_LIKE_TEMPERATURE,
ATTR_API_FORECAST,
Expand Down Expand Up @@ -315,7 +316,9 @@ def __init__(
@property
def native_value(self) -> StateType:
"""Return the state of the device."""
return self._weather_coordinator.data.get(self.entity_description.key, None)
return self._weather_coordinator.data[ATTR_API_CURRENT].get(
self.entity_description.key, None
)


class OpenWeatherMapForecastSensor(AbstractOpenWeatherMapSensor):
Expand Down
21 changes: 11 additions & 10 deletions homeassistant/components/openweathermap/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from .const import (
ATTR_API_CLOUDS,
ATTR_API_CONDITION,
ATTR_API_CURRENT,
ATTR_API_DEW_POINT,
ATTR_API_FEELS_LIKE_TEMPERATURE,
ATTR_API_FORECAST,
Expand Down Expand Up @@ -137,52 +138,52 @@ def __init__(
@property
def condition(self) -> str | None:
"""Return the current condition."""
return self.coordinator.data[ATTR_API_CONDITION]
return self.coordinator.data[ATTR_API_CURRENT][ATTR_API_CONDITION]

@property
def cloud_coverage(self) -> float | None:
"""Return the Cloud coverage in %."""
return self.coordinator.data[ATTR_API_CLOUDS]
return self.coordinator.data[ATTR_API_CURRENT][ATTR_API_CLOUDS]

@property
def native_apparent_temperature(self) -> float | None:
"""Return the apparent temperature."""
return self.coordinator.data[ATTR_API_FEELS_LIKE_TEMPERATURE]
return self.coordinator.data[ATTR_API_CURRENT][ATTR_API_FEELS_LIKE_TEMPERATURE]

@property
def native_temperature(self) -> float | None:
"""Return the temperature."""
return self.coordinator.data[ATTR_API_TEMPERATURE]
return self.coordinator.data[ATTR_API_CURRENT][ATTR_API_TEMPERATURE]

@property
def native_pressure(self) -> float | None:
"""Return the pressure."""
return self.coordinator.data[ATTR_API_PRESSURE]
return self.coordinator.data[ATTR_API_CURRENT][ATTR_API_PRESSURE]

@property
def humidity(self) -> float | None:
"""Return the humidity."""
return self.coordinator.data[ATTR_API_HUMIDITY]
return self.coordinator.data[ATTR_API_CURRENT][ATTR_API_HUMIDITY]

@property
def native_dew_point(self) -> float | None:
"""Return the dew point."""
return self.coordinator.data[ATTR_API_DEW_POINT]
return self.coordinator.data[ATTR_API_CURRENT][ATTR_API_DEW_POINT]

@property
def native_wind_gust_speed(self) -> float | None:
"""Return the wind gust speed."""
return self.coordinator.data[ATTR_API_WIND_GUST]
return self.coordinator.data[ATTR_API_CURRENT][ATTR_API_WIND_GUST]

@property
def native_wind_speed(self) -> float | None:
"""Return the wind speed."""
return self.coordinator.data[ATTR_API_WIND_SPEED]
return self.coordinator.data[ATTR_API_CURRENT][ATTR_API_WIND_SPEED]

@property
def wind_bearing(self) -> float | str | None:
"""Return the wind bearing."""
return self.coordinator.data[ATTR_API_WIND_BEARING]
return self.coordinator.data[ATTR_API_CURRENT][ATTR_API_WIND_BEARING]

@property
def _forecast(self) -> list[Forecast] | None:
Expand Down
Loading
Loading