mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
commit
852305b996
@ -13,14 +13,14 @@ sensor:
|
||||
platform: openweathermap
|
||||
api_key: YOUR_APP_KEY
|
||||
monitored_variables:
|
||||
- type: 'weather'
|
||||
- type: 'temperature'
|
||||
- type: 'wind_speed'
|
||||
- type: 'humidity'
|
||||
- type: 'pressure'
|
||||
- type: 'clouds'
|
||||
- type: 'rain'
|
||||
- type: 'snow'
|
||||
- weather
|
||||
- temperature
|
||||
- wind_speed
|
||||
- humidity
|
||||
- pressure
|
||||
- clouds
|
||||
- rain
|
||||
- snow
|
||||
|
||||
Variables:
|
||||
|
||||
@ -28,28 +28,29 @@ api_key
|
||||
*Required
|
||||
To retrieve this value log into your account at http://openweathermap.org/
|
||||
|
||||
monitored_variables
|
||||
monitored_conditions
|
||||
*Required
|
||||
An array specifying the variables to monitor.
|
||||
|
||||
These are the variables for the monitored_variables array:
|
||||
These are the variables for the monitored_conditions array:
|
||||
|
||||
type
|
||||
*Required
|
||||
The variable you wish to monitor, see the configuration example above for a
|
||||
list of all available variables
|
||||
list of all available conditions to monitor.
|
||||
|
||||
Details for the API : http://bugs.openweathermap.org/projects/api/wiki
|
||||
|
||||
Only metric measurements are supported at the moment.
|
||||
"""
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.const import (CONF_API_KEY, TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
_THROTTLED_REFRESH = None
|
||||
SENSOR_TYPES = {
|
||||
'weather': ['Condition', ''],
|
||||
'temperature': ['Temperature', ''],
|
||||
@ -61,6 +62,9 @@ SENSOR_TYPES = {
|
||||
'snow': ['Snow', 'mm']
|
||||
}
|
||||
|
||||
# Return cached results if last scan was less then this time ago
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=120)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
""" Get the OpenWeatherMap sensor. """
|
||||
@ -82,7 +86,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
SENSOR_TYPES['temperature'][1] = hass.config.temperature_unit
|
||||
unit = hass.config.temperature_unit
|
||||
owm = OWM(config.get(CONF_API_KEY, None))
|
||||
obs = owm.weather_at_coords(hass.config.latitude, hass.config.longitude)
|
||||
|
||||
if not owm:
|
||||
_LOGGER.error(
|
||||
@ -90,12 +93,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"Please check your settings for OpenWeatherMap.")
|
||||
return None
|
||||
|
||||
data = WeatherData(owm, hass.config.latitude, hass.config.longitude)
|
||||
dev = []
|
||||
for variable in config['monitored_variables']:
|
||||
if variable['type'] not in SENSOR_TYPES:
|
||||
_LOGGER.error('Sensor type: "%s" does not exist', variable['type'])
|
||||
for variable in config['monitored_conditions']:
|
||||
if variable not in SENSOR_TYPES:
|
||||
_LOGGER.error('Sensor type: "%s" does not exist', variable)
|
||||
else:
|
||||
dev.append(OpenWeatherMapSensor(variable['type'], obs, unit))
|
||||
dev.append(OpenWeatherMapSensor(data, variable, unit))
|
||||
|
||||
add_devices(dev)
|
||||
|
||||
@ -104,7 +108,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
class OpenWeatherMapSensor(Entity):
|
||||
""" Implements an OpenWeatherMap sensor. """
|
||||
|
||||
def __init__(self, sensor_type, weather_data, unit):
|
||||
def __init__(self, weather_data, sensor_type, unit):
|
||||
self.client_name = 'Weather - '
|
||||
self._name = SENSOR_TYPES[sensor_type][0]
|
||||
self.owa_client = weather_data
|
||||
@ -131,7 +135,9 @@ class OpenWeatherMapSensor(Entity):
|
||||
# pylint: disable=too-many-branches
|
||||
def update(self):
|
||||
""" Gets the latest data from OWM and updates the states. """
|
||||
data = self.owa_client.get_weather()
|
||||
|
||||
self.owa_client.update()
|
||||
data = self.owa_client.data
|
||||
|
||||
if self.type == 'weather':
|
||||
self._state = data.get_detailed_status()
|
||||
@ -164,3 +170,20 @@ class OpenWeatherMapSensor(Entity):
|
||||
else:
|
||||
self._state = 'not snowing'
|
||||
self._unit_of_measurement = ''
|
||||
|
||||
|
||||
class WeatherData(object):
|
||||
""" Gets the latest data from OpenWeatherMap. """
|
||||
|
||||
def __init__(self, owm, latitude, longitude):
|
||||
self.owm = owm
|
||||
self.latitude = latitude
|
||||
self.longitude = longitude
|
||||
self.data = None
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
""" Gets the latest data from OpenWeatherMap. """
|
||||
|
||||
obs = self.owm.weather_at_coords(self.latitude, self.longitude)
|
||||
self.data = obs.get_weather()
|
||||
|
Loading…
x
Reference in New Issue
Block a user