Add 'entity_picture' to Darksky component (#6141)

This commit is contained in:
Lev Aronsky 2017-02-21 10:01:44 +02:00 committed by Paulus Schoutsen
parent 3beb87c54d
commit c1be5ede1c
11 changed files with 42 additions and 0 deletions

View file

@ -97,6 +97,20 @@ SENSOR_TYPES = {
['currently', 'hourly', 'daily']],
}
CONDITION_PICTURES = {
'clear-day': '/static/images/darksky/weather-sunny.svg',
'clear-night': '/static/images/darksky/weather-night.svg',
'rain': '/static/images/darksky/weather-pouring.svg',
'snow': '/static/images/darksky/weather-snowy.svg',
'sleet': '/static/images/darksky/weather-hail.svg',
'wind': '/static/images/darksky/weather-windy.svg',
'fog': '/static/images/darksky/weather-fog.svg',
'cloudy': '/static/images/darksky/weather-cloudy.svg',
'partly-cloudy-day': '/static/images/darksky/weather-partlycloudy.svg',
'partly-cloudy-night': '/static/images/darksky/weather-cloudy.svg',
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_MONITORED_CONDITIONS):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
@ -162,6 +176,7 @@ class DarkSkySensor(Entity):
self.type = sensor_type
self.forecast_day = forecast_day
self._state = None
self._icon = None
self._unit_of_measurement = None
@property
@ -188,6 +203,17 @@ class DarkSkySensor(Entity):
"""Return the unit system of this entity."""
return self.forecast_data.unit_system
@property
def entity_picture(self):
"""Return the entity picture to use in the frontend, if any."""
if self._icon is None or 'summary' not in self.type:
return None
if self._icon in CONDITION_PICTURES:
return CONDITION_PICTURES[self._icon]
else:
return None
def update_unit_of_measurement(self):
"""Update units based on unit system."""
unit_index = {
@ -224,10 +250,12 @@ class DarkSkySensor(Entity):
self.forecast_data.update_minutely()
minutely = self.forecast_data.data_minutely
self._state = getattr(minutely, 'summary', '')
self._icon = getattr(minutely, 'icon', '')
elif self.type == 'hourly_summary':
self.forecast_data.update_hourly()
hourly = self.forecast_data.data_hourly
self._state = getattr(hourly, 'summary', '')
self._icon = getattr(hourly, 'icon', '')
elif self.forecast_day > 0 or (
self.type in ['daily_summary',
'temperature_min',
@ -239,6 +267,7 @@ class DarkSkySensor(Entity):
daily = self.forecast_data.data_daily
if self.type == 'daily_summary':
self._state = getattr(daily, 'summary', '')
self._icon = getattr(daily, 'icon', '')
else:
if hasattr(daily, 'data'):
self._state = self.get_state(
@ -262,6 +291,9 @@ class DarkSkySensor(Entity):
if state is None:
return state
if 'summary' in self.type:
self._icon = getattr(data, 'icon', '')
# Some state data needs to be rounded to whole values or converted to
# percentages
if self.type in ['precip_probability', 'cloud_cover', 'humidity']: