mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Yahoo Weather update, supports forecast for more days (#8626)
* work on weather panel * update yahooweather with more forecast details * Update yweather to allow user input forecast date * fix for houndci * fix long line * fix1 * Revert "work on weather panel" This reverts commit 28b4972233de42617fb05df574de22743604edfd. revert unintentional submodule change * fix2 fix typo, add try catch to another int() * fix pylint * fix3 * fix4 * Update yweather.py * Update yweather.py * Remove global data construct * Yahoo API support only 5 days forecast * remove forecast * fix lint * fix lint p2 * Update yweather.py
This commit is contained in:
parent
fff269e790
commit
abcfcdd887
@ -11,17 +11,21 @@ import voluptuous as vol
|
|||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.weather import (
|
from homeassistant.components.weather import (
|
||||||
WeatherEntity, PLATFORM_SCHEMA, ATTR_FORECAST_TEMP)
|
WeatherEntity, PLATFORM_SCHEMA,
|
||||||
|
ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME)
|
||||||
from homeassistant.const import (TEMP_CELSIUS, CONF_NAME, STATE_UNKNOWN)
|
from homeassistant.const import (TEMP_CELSIUS, CONF_NAME, STATE_UNKNOWN)
|
||||||
|
|
||||||
REQUIREMENTS = ["yahooweather==0.8"]
|
REQUIREMENTS = ["yahooweather==0.8"]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DATA_CONDITION = 'yahoo_condition'
|
||||||
|
|
||||||
ATTR_FORECAST_CONDITION = 'condition'
|
ATTR_FORECAST_CONDITION = 'condition'
|
||||||
ATTRIBUTION = "Weather details provided by Yahoo! Inc."
|
ATTRIBUTION = "Weather details provided by Yahoo! Inc."
|
||||||
|
|
||||||
CONF_FORECAST = 'forecast'
|
ATTR_FORECAST_TEMP_LOW = 'templow'
|
||||||
|
|
||||||
CONF_WOEID = 'woeid'
|
CONF_WOEID = 'woeid'
|
||||||
|
|
||||||
DEFAULT_NAME = 'Yweather'
|
DEFAULT_NAME = 'Yweather'
|
||||||
@ -33,23 +37,22 @@ CONDITION_CLASSES = {
|
|||||||
'fog': [19, 20, 21, 22, 23],
|
'fog': [19, 20, 21, 22, 23],
|
||||||
'hail': [17, 18, 35],
|
'hail': [17, 18, 35],
|
||||||
'lightning': [37],
|
'lightning': [37],
|
||||||
'lightning-rainy': [38, 39],
|
'lightning-rainy': [38, 39, 47],
|
||||||
'partlycloudy': [44],
|
'partlycloudy': [44],
|
||||||
'pouring': [40, 45],
|
'pouring': [40, 45],
|
||||||
'rainy': [9, 11, 12],
|
'rainy': [9, 11, 12],
|
||||||
'snowy': [8, 13, 14, 15, 16, 41, 42, 43],
|
'snowy': [8, 13, 14, 15, 16, 41, 42, 43],
|
||||||
'snowy-rainy': [5, 6, 7, 10, 46, 47],
|
'snowy-rainy': [5, 6, 7, 10, 46],
|
||||||
'sunny': [32],
|
'sunny': [32, 33, 34],
|
||||||
'windy': [24],
|
'windy': [24],
|
||||||
'windy-variant': [],
|
'windy-variant': [],
|
||||||
'exceptional': [0, 1, 2, 3, 4, 25, 36],
|
'exceptional': [0, 1, 2, 3, 4, 25, 36],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Optional(CONF_WOEID, default=None): cv.string,
|
vol.Optional(CONF_WOEID, default=None): cv.string,
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
vol.Optional(CONF_FORECAST, default=0):
|
|
||||||
vol.All(vol.Coerce(int), vol.Range(min=0, max=5)),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -59,7 +62,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
|
|
||||||
unit = hass.config.units.temperature_unit
|
unit = hass.config.units.temperature_unit
|
||||||
woeid = config.get(CONF_WOEID)
|
woeid = config.get(CONF_WOEID)
|
||||||
forecast = config.get(CONF_FORECAST)
|
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
|
|
||||||
yunit = UNIT_C if unit == TEMP_CELSIUS else UNIT_F
|
yunit = UNIT_C if unit == TEMP_CELSIUS else UNIT_F
|
||||||
@ -77,22 +79,23 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
_LOGGER.critical("Can't retrieve weather data from Yahoo!")
|
_LOGGER.critical("Can't retrieve weather data from Yahoo!")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if forecast >= len(yahoo_api.yahoo.Forecast):
|
# create condition helper
|
||||||
_LOGGER.error("Yahoo! only support %d days forecast",
|
if DATA_CONDITION not in hass.data:
|
||||||
len(yahoo_api.yahoo.Forecast))
|
hass.data[DATA_CONDITION] = [str(x) for x in range(0, 50)]
|
||||||
return False
|
for cond, condlst in CONDITION_CLASSES.items():
|
||||||
|
for condi in condlst:
|
||||||
|
hass.data[DATA_CONDITION][condi] = cond
|
||||||
|
|
||||||
add_devices([YahooWeatherWeather(yahoo_api, name, forecast)], True)
|
add_devices([YahooWeatherWeather(yahoo_api, name)], True)
|
||||||
|
|
||||||
|
|
||||||
class YahooWeatherWeather(WeatherEntity):
|
class YahooWeatherWeather(WeatherEntity):
|
||||||
"""Representation of Yahoo! weather data."""
|
"""Representation of Yahoo! weather data."""
|
||||||
|
|
||||||
def __init__(self, weather_data, name, forecast):
|
def __init__(self, weather_data, name):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._data = weather_data
|
self._data = weather_data
|
||||||
self._forecast = forecast
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -103,9 +106,9 @@ class YahooWeatherWeather(WeatherEntity):
|
|||||||
def condition(self):
|
def condition(self):
|
||||||
"""Return the current condition."""
|
"""Return the current condition."""
|
||||||
try:
|
try:
|
||||||
return [k for k, v in CONDITION_CLASSES.items() if
|
return self.hass.data[DATA_CONDITION][int(
|
||||||
int(self._data.yahoo.Now['code']) in v][0]
|
self._data.yahoo.Now['code'])]
|
||||||
except IndexError:
|
except (ValueError, IndexError):
|
||||||
return STATE_UNKNOWN
|
return STATE_UNKNOWN
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -138,6 +141,11 @@ class YahooWeatherWeather(WeatherEntity):
|
|||||||
"""Return the wind speed."""
|
"""Return the wind speed."""
|
||||||
return self._data.yahoo.Wind['speed']
|
return self._data.yahoo.Wind['speed']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def wind_bearing(self):
|
||||||
|
"""Return the wind direction."""
|
||||||
|
return self._data.yahoo.Wind['direction']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def attribution(self):
|
def attribution(self):
|
||||||
"""Return the attribution."""
|
"""Return the attribution."""
|
||||||
@ -147,19 +155,17 @@ class YahooWeatherWeather(WeatherEntity):
|
|||||||
def forecast(self):
|
def forecast(self):
|
||||||
"""Return the forecast array."""
|
"""Return the forecast array."""
|
||||||
try:
|
try:
|
||||||
forecast_condition = \
|
return [
|
||||||
[k for k, v in CONDITION_CLASSES.items() if
|
{
|
||||||
int(self._data.yahoo.Forecast[self._forecast]['code'])
|
ATTR_FORECAST_TIME: v['date'],
|
||||||
in v][0]
|
ATTR_FORECAST_TEMP:int(v['high']),
|
||||||
except IndexError:
|
ATTR_FORECAST_TEMP_LOW: int(v['low']),
|
||||||
|
ATTR_FORECAST_CONDITION:
|
||||||
|
self.hass.data[DATA_CONDITION][int(v['code'])]
|
||||||
|
} for v in self._data.yahoo.Forecast]
|
||||||
|
except (ValueError, IndexError):
|
||||||
return STATE_UNKNOWN
|
return STATE_UNKNOWN
|
||||||
|
|
||||||
return [{
|
|
||||||
ATTR_FORECAST_CONDITION: forecast_condition,
|
|
||||||
ATTR_FORECAST_TEMP:
|
|
||||||
self._data.yahoo.Forecast[self._forecast]['high'],
|
|
||||||
}]
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from Yahoo! and updates the states."""
|
"""Get the latest data from Yahoo! and updates the states."""
|
||||||
self._data.update()
|
self._data.update()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user