Add template support for remaining attributes of weather entity (#47736)

* added template support for remaining attributes of weather entity

* wind bearing is now angle (number)
This commit is contained in:
Christian Soltenborn 2021-03-27 19:53:35 +01:00 committed by GitHub
parent 388815b81a
commit 955804bf58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 3 deletions

View file

@ -49,8 +49,12 @@ CONF_WEATHER = "weather"
CONF_TEMPERATURE_TEMPLATE = "temperature_template"
CONF_HUMIDITY_TEMPLATE = "humidity_template"
CONF_CONDITION_TEMPLATE = "condition_template"
CONF_ATTRIBUTION_TEMPLATE = "attribution_template"
CONF_PRESSURE_TEMPLATE = "pressure_template"
CONF_WIND_SPEED_TEMPLATE = "wind_speed_template"
CONF_WIND_BEARING_TEMPLATE = "wind_bearing_template"
CONF_OZONE_TEMPLATE = "ozone_template"
CONF_VISIBILITY_TEMPLATE = "visibility_template"
CONF_FORECAST_TEMPLATE = "forecast_template"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -59,8 +63,12 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
vol.Required(CONF_CONDITION_TEMPLATE): cv.template,
vol.Required(CONF_TEMPERATURE_TEMPLATE): cv.template,
vol.Required(CONF_HUMIDITY_TEMPLATE): cv.template,
vol.Optional(CONF_ATTRIBUTION_TEMPLATE): cv.template,
vol.Optional(CONF_PRESSURE_TEMPLATE): cv.template,
vol.Optional(CONF_WIND_SPEED_TEMPLATE): cv.template,
vol.Optional(CONF_WIND_BEARING_TEMPLATE): cv.template,
vol.Optional(CONF_OZONE_TEMPLATE): cv.template,
vol.Optional(CONF_VISIBILITY_TEMPLATE): cv.template,
vol.Optional(CONF_FORECAST_TEMPLATE): cv.template,
vol.Optional(CONF_UNIQUE_ID): cv.string,
}
@ -74,8 +82,12 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
condition_template = config[CONF_CONDITION_TEMPLATE]
temperature_template = config[CONF_TEMPERATURE_TEMPLATE]
humidity_template = config[CONF_HUMIDITY_TEMPLATE]
attribution_template = config.get(CONF_ATTRIBUTION_TEMPLATE)
pressure_template = config.get(CONF_PRESSURE_TEMPLATE)
wind_speed_template = config.get(CONF_WIND_SPEED_TEMPLATE)
wind_bearing_template = config.get(CONF_WIND_BEARING_TEMPLATE)
ozone_template = config.get(CONF_OZONE_TEMPLATE)
visibility_template = config.get(CONF_VISIBILITY_TEMPLATE)
forecast_template = config.get(CONF_FORECAST_TEMPLATE)
unique_id = config.get(CONF_UNIQUE_ID)
@ -87,8 +99,12 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
condition_template,
temperature_template,
humidity_template,
attribution_template,
pressure_template,
wind_speed_template,
wind_bearing_template,
ozone_template,
visibility_template,
forecast_template,
unique_id,
)
@ -106,8 +122,12 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
condition_template,
temperature_template,
humidity_template,
attribution_template,
pressure_template,
wind_speed_template,
wind_bearing_template,
ozone_template,
visibility_template,
forecast_template,
unique_id,
):
@ -118,8 +138,12 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
self._condition_template = condition_template
self._temperature_template = temperature_template
self._humidity_template = humidity_template
self._attribution_template = attribution_template
self._pressure_template = pressure_template
self._wind_speed_template = wind_speed_template
self._wind_bearing_template = wind_bearing_template
self._ozone_template = ozone_template
self._visibility_template = visibility_template
self._forecast_template = forecast_template
self._unique_id = unique_id
@ -128,8 +152,12 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
self._condition = None
self._temperature = None
self._humidity = None
self._attribution = None
self._pressure = None
self._wind_speed = None
self._wind_bearing = None
self._ozone = None
self._visibility = None
self._forecast = []
@property
@ -162,9 +190,24 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
"""Return the wind speed."""
return self._wind_speed
@property
def wind_bearing(self):
"""Return the wind bearing."""
return self._wind_bearing
@property
def ozone(self):
"""Return the ozone level."""
return self._ozone
@property
def visibility(self):
"""Return the visibility."""
return self._visibility
@property
def pressure(self):
"""Return the pressure."""
"""Return the air pressure."""
return self._pressure
@property
@ -175,11 +218,13 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
@property
def attribution(self):
"""Return the attribution."""
return "Powered by Home Assistant"
if self._attribution is None:
return "Powered by Home Assistant"
return self._attribution
@property
def unique_id(self):
"""Return the unique id of this light."""
"""Return the unique id of this weather instance."""
return self._unique_id
async def async_added_to_hass(self):
@ -201,6 +246,11 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
"_humidity",
self._humidity_template,
)
if self._attribution_template:
self.add_template_attribute(
"_attribution",
self._attribution_template,
)
if self._pressure_template:
self.add_template_attribute(
"_pressure",
@ -211,6 +261,21 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
"_wind_speed",
self._wind_speed_template,
)
if self._wind_bearing_template:
self.add_template_attribute(
"_wind_bearing",
self._wind_bearing_template,
)
if self._ozone_template:
self.add_template_attribute(
"_ozone",
self._ozone_template,
)
if self._visibility_template:
self.add_template_attribute(
"_visibility",
self._visibility_template,
)
if self._forecast_template:
self.add_template_attribute(
"_forecast",

View file

@ -1,8 +1,12 @@
"""The tests for the Template Weather platform."""
from homeassistant.components.weather import (
ATTR_WEATHER_ATTRIBUTION,
ATTR_WEATHER_HUMIDITY,
ATTR_WEATHER_OZONE,
ATTR_WEATHER_PRESSURE,
ATTR_WEATHER_TEMPERATURE,
ATTR_WEATHER_VISIBILITY,
ATTR_WEATHER_WIND_BEARING,
ATTR_WEATHER_WIND_SPEED,
DOMAIN,
)
@ -20,12 +24,16 @@ async def test_template_state_text(hass):
{
"platform": "template",
"name": "test",
"attribution_template": "{{ states('sensor.attribution') }}",
"condition_template": "sunny",
"forecast_template": "{{ states.weather.demo.attributes.forecast }}",
"temperature_template": "{{ states('sensor.temperature') | float }}",
"humidity_template": "{{ states('sensor.humidity') | int }}",
"pressure_template": "{{ states('sensor.pressure') }}",
"wind_speed_template": "{{ states('sensor.windspeed') }}",
"wind_bearing_template": "{{ states('sensor.windbearing') }}",
"ozone_template": "{{ states('sensor.ozone') }}",
"visibility_template": "{{ states('sensor.visibility') }}",
},
]
},
@ -35,6 +43,8 @@ async def test_template_state_text(hass):
await hass.async_start()
await hass.async_block_till_done()
hass.states.async_set("sensor.attribution", "The custom attribution")
await hass.async_block_till_done()
hass.states.async_set("sensor.temperature", 22.3)
await hass.async_block_till_done()
hass.states.async_set("sensor.humidity", 60)
@ -43,6 +53,12 @@ async def test_template_state_text(hass):
await hass.async_block_till_done()
hass.states.async_set("sensor.windspeed", 20)
await hass.async_block_till_done()
hass.states.async_set("sensor.windbearing", 180)
await hass.async_block_till_done()
hass.states.async_set("sensor.ozone", 25)
await hass.async_block_till_done()
hass.states.async_set("sensor.visibility", 4.6)
await hass.async_block_till_done()
state = hass.states.get("weather.test")
assert state is not None
@ -50,7 +66,11 @@ async def test_template_state_text(hass):
assert state.state == "sunny"
data = state.attributes
assert data.get(ATTR_WEATHER_ATTRIBUTION) == "The custom attribution"
assert data.get(ATTR_WEATHER_TEMPERATURE) == 22.3
assert data.get(ATTR_WEATHER_HUMIDITY) == 60
assert data.get(ATTR_WEATHER_PRESSURE) == 1000
assert data.get(ATTR_WEATHER_WIND_SPEED) == 20
assert data.get(ATTR_WEATHER_WIND_BEARING) == 180
assert data.get(ATTR_WEATHER_OZONE) == 25
assert data.get(ATTR_WEATHER_VISIBILITY) == 4.6