2016-10-25 04:53:03 +00:00
|
|
|
"""
|
|
|
|
Weather component that handles meteorological data for your location.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/weather/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2017-11-14 09:36:18 +00:00
|
|
|
from homeassistant.helpers.temperature import display_temp as show_temp
|
|
|
|
from homeassistant.const import PRECISION_WHOLE, PRECISION_TENTHS, TEMP_CELSIUS
|
2016-10-25 04:53:03 +00:00
|
|
|
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEPENDENCIES = []
|
|
|
|
DOMAIN = 'weather'
|
|
|
|
|
|
|
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
|
|
|
|
|
|
|
ATTR_CONDITION_CLASS = 'condition_class'
|
2017-06-07 08:49:54 +00:00
|
|
|
ATTR_FORECAST = 'forecast'
|
2018-04-29 15:50:49 +00:00
|
|
|
ATTR_FORECAST_CONDITION = 'condition'
|
|
|
|
ATTR_FORECAST_PRECIPITATION = 'precipitation'
|
2017-06-07 08:49:54 +00:00
|
|
|
ATTR_FORECAST_TEMP = 'temperature'
|
2018-04-29 15:50:49 +00:00
|
|
|
ATTR_FORECAST_TEMP_LOW = 'templow'
|
2017-06-07 08:49:54 +00:00
|
|
|
ATTR_FORECAST_TIME = 'datetime'
|
2018-09-24 16:09:15 +00:00
|
|
|
ATTR_FORECAST_WIND_SPEED = 'wind_speed'
|
|
|
|
ATTR_FORECAST_WIND_BEARING = 'wind_bearing'
|
2016-10-25 04:53:03 +00:00
|
|
|
ATTR_WEATHER_ATTRIBUTION = 'attribution'
|
|
|
|
ATTR_WEATHER_HUMIDITY = 'humidity'
|
|
|
|
ATTR_WEATHER_OZONE = 'ozone'
|
|
|
|
ATTR_WEATHER_PRESSURE = 'pressure'
|
|
|
|
ATTR_WEATHER_TEMPERATURE = 'temperature'
|
2017-06-07 08:49:54 +00:00
|
|
|
ATTR_WEATHER_VISIBILITY = 'visibility'
|
2016-10-25 04:53:03 +00:00
|
|
|
ATTR_WEATHER_WIND_BEARING = 'wind_bearing'
|
|
|
|
ATTR_WEATHER_WIND_SPEED = 'wind_speed'
|
|
|
|
|
|
|
|
|
2018-10-01 06:52:42 +00:00
|
|
|
async def async_setup(hass, config):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the weather component."""
|
2018-10-08 21:54:55 +00:00
|
|
|
component = hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass)
|
2018-10-01 06:52:42 +00:00
|
|
|
await component.async_setup(config)
|
2016-10-25 04:53:03 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-10-08 21:54:55 +00:00
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Set up a config entry."""
|
|
|
|
return await hass.data[DOMAIN].async_setup_entry(entry)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.data[DOMAIN].async_unload_entry(entry)
|
|
|
|
|
|
|
|
|
2016-10-25 04:53:03 +00:00
|
|
|
class WeatherEntity(Entity):
|
2017-06-07 08:49:54 +00:00
|
|
|
"""ABC for weather data."""
|
2016-10-25 04:53:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def temperature(self):
|
|
|
|
"""Return the platform temperature."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def temperature_unit(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pressure(self):
|
|
|
|
"""Return the pressure."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def humidity(self):
|
|
|
|
"""Return the humidity."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def wind_speed(self):
|
|
|
|
"""Return the wind speed."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def wind_bearing(self):
|
|
|
|
"""Return the wind bearing."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ozone(self):
|
|
|
|
"""Return the ozone level."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def attribution(self):
|
|
|
|
"""Return the attribution."""
|
|
|
|
return None
|
|
|
|
|
2017-06-07 08:49:54 +00:00
|
|
|
@property
|
|
|
|
def visibility(self):
|
|
|
|
"""Return the visibility."""
|
|
|
|
return None
|
|
|
|
|
2017-02-20 00:42:12 +00:00
|
|
|
@property
|
|
|
|
def forecast(self):
|
|
|
|
"""Return the forecast."""
|
|
|
|
return None
|
|
|
|
|
2017-11-14 09:36:18 +00:00
|
|
|
@property
|
|
|
|
def precision(self):
|
|
|
|
"""Return the forecast."""
|
|
|
|
return PRECISION_TENTHS if self.temperature_unit == TEMP_CELSIUS \
|
|
|
|
else PRECISION_WHOLE
|
|
|
|
|
2016-10-25 04:53:03 +00:00
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
data = {
|
2017-11-14 09:36:18 +00:00
|
|
|
ATTR_WEATHER_TEMPERATURE: show_temp(
|
|
|
|
self.hass, self.temperature, self.temperature_unit,
|
|
|
|
self.precision),
|
2016-10-25 04:53:03 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 17:27:45 +00:00
|
|
|
humidity = self.humidity
|
|
|
|
if humidity is not None:
|
|
|
|
data[ATTR_WEATHER_HUMIDITY] = round(humidity)
|
|
|
|
|
2016-10-25 04:53:03 +00:00
|
|
|
ozone = self.ozone
|
|
|
|
if ozone is not None:
|
|
|
|
data[ATTR_WEATHER_OZONE] = ozone
|
|
|
|
|
|
|
|
pressure = self.pressure
|
|
|
|
if pressure is not None:
|
|
|
|
data[ATTR_WEATHER_PRESSURE] = pressure
|
|
|
|
|
|
|
|
wind_bearing = self.wind_bearing
|
|
|
|
if wind_bearing is not None:
|
|
|
|
data[ATTR_WEATHER_WIND_BEARING] = wind_bearing
|
|
|
|
|
|
|
|
wind_speed = self.wind_speed
|
|
|
|
if wind_speed is not None:
|
|
|
|
data[ATTR_WEATHER_WIND_SPEED] = wind_speed
|
|
|
|
|
2017-06-07 08:49:54 +00:00
|
|
|
visibility = self.visibility
|
|
|
|
if visibility is not None:
|
|
|
|
data[ATTR_WEATHER_VISIBILITY] = visibility
|
|
|
|
|
2016-10-25 04:53:03 +00:00
|
|
|
attribution = self.attribution
|
|
|
|
if attribution is not None:
|
|
|
|
data[ATTR_WEATHER_ATTRIBUTION] = attribution
|
|
|
|
|
2017-02-20 00:42:12 +00:00
|
|
|
if self.forecast is not None:
|
|
|
|
forecast = []
|
|
|
|
for forecast_entry in self.forecast:
|
|
|
|
forecast_entry = dict(forecast_entry)
|
2017-11-14 09:36:18 +00:00
|
|
|
forecast_entry[ATTR_FORECAST_TEMP] = show_temp(
|
|
|
|
self.hass, forecast_entry[ATTR_FORECAST_TEMP],
|
|
|
|
self.temperature_unit, self.precision)
|
2018-04-29 15:50:49 +00:00
|
|
|
if ATTR_FORECAST_TEMP_LOW in forecast_entry:
|
|
|
|
forecast_entry[ATTR_FORECAST_TEMP_LOW] = show_temp(
|
|
|
|
self.hass, forecast_entry[ATTR_FORECAST_TEMP_LOW],
|
|
|
|
self.temperature_unit, self.precision)
|
2017-02-20 00:42:12 +00:00
|
|
|
forecast.append(forecast_entry)
|
|
|
|
|
|
|
|
data[ATTR_FORECAST] = forecast
|
|
|
|
|
2016-10-25 04:53:03 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the current state."""
|
|
|
|
return self.condition
|
|
|
|
|
|
|
|
@property
|
|
|
|
def condition(self):
|
|
|
|
"""Return the current condition."""
|
|
|
|
raise NotImplementedError()
|