Move imports in openweathermap component (#27779)

This commit is contained in:
Quentame 2019-10-18 07:12:32 +02:00 committed by Paulus Schoutsen
parent 7ec8384fa6
commit 5cb145f588
2 changed files with 6 additions and 12 deletions

View file

@ -2,6 +2,8 @@
from datetime import timedelta
import logging
from pyowm import OWM
from pyowm.exceptions.api_call_error import APICallError
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
@ -56,7 +58,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the OpenWeatherMap sensor."""
from pyowm import OWM
if None in (hass.config.latitude, hass.config.longitude):
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
@ -127,8 +128,6 @@ class OpenWeatherMapSensor(Entity):
def update(self):
"""Get the latest data from OWM and updates the states."""
from pyowm.exceptions.api_call_error import APICallError
try:
self.owa_client.update()
except APICallError:
@ -201,8 +200,6 @@ class WeatherData:
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Get the latest data from OpenWeatherMap."""
from pyowm.exceptions.api_call_error import APICallError
try:
obs = self.owm.weather_at_coords(self.latitude, self.longitude)
except (APICallError, TypeError):

View file

@ -2,6 +2,8 @@
from datetime import timedelta
import logging
from pyowm import OWM
from pyowm.exceptions.api_call_error import APICallError
import voluptuous as vol
from homeassistant.components.weather import (
@ -71,7 +73,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the OpenWeatherMap weather platform."""
import pyowm
longitude = config.get(CONF_LONGITUDE, round(hass.config.longitude, 5))
latitude = config.get(CONF_LATITUDE, round(hass.config.latitude, 5))
@ -79,8 +80,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
mode = config.get(CONF_MODE)
try:
owm = pyowm.OWM(config.get(CONF_API_KEY))
except pyowm.exceptions.api_call_error.APICallError:
owm = OWM(config.get(CONF_API_KEY))
except APICallError:
_LOGGER.error("Error while connecting to OpenWeatherMap")
return False
@ -225,8 +226,6 @@ class OpenWeatherMapWeather(WeatherEntity):
def update(self):
"""Get the latest data from OWM and updates the states."""
from pyowm.exceptions.api_call_error import APICallError
try:
self._owm.update()
self._owm.update_forecast()
@ -263,8 +262,6 @@ class WeatherData:
@Throttle(MIN_TIME_BETWEEN_FORECAST_UPDATES)
def update_forecast(self):
"""Get the latest forecast from OpenWeatherMap."""
from pyowm.exceptions.api_call_error import APICallError
try:
if self._mode == "daily":
fcd = self.owm.daily_forecast_at_coords(