Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions homeassistant/components/template/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
CONF_HUMIDITY_TEMPLATE = "humidity_template"
CONF_CONDITION_TEMPLATE = "condition_template"
CONF_PRESSURE_TEMPLATE = "pressure_template"
CONF_VISIBILITY_TEMPLATE = "visibility_template"
CONF_WIND_BEARING_TEMPLATE = "wind_bearing_template"
CONF_WIND_SPEED_TEMPLATE = "wind_speed_template"
CONF_FORECAST_TEMPLATE = "forecast_template"

Expand All @@ -60,6 +62,8 @@
vol.Required(CONF_TEMPERATURE_TEMPLATE): cv.template,
vol.Required(CONF_HUMIDITY_TEMPLATE): cv.template,
vol.Optional(CONF_PRESSURE_TEMPLATE): cv.template,
vol.Optional(CONF_VISIBILITY_TEMPLATE): cv.template,
vol.Optional(CONF_WIND_BEARING_TEMPLATE): cv.template,
vol.Optional(CONF_WIND_SPEED_TEMPLATE): cv.template,
vol.Optional(CONF_FORECAST_TEMPLATE): cv.template,
vol.Optional(CONF_UNIQUE_ID): cv.string,
Expand All @@ -76,6 +80,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
temperature_template = config[CONF_TEMPERATURE_TEMPLATE]
humidity_template = config[CONF_HUMIDITY_TEMPLATE]
pressure_template = config.get(CONF_PRESSURE_TEMPLATE)
visibility_template = config.get(CONF_VISIBILITY_TEMPLATE)
wind_bearing_template = config.get(CONF_WIND_BEARING_TEMPLATE)
wind_speed_template = config.get(CONF_WIND_SPEED_TEMPLATE)
forecast_template = config.get(CONF_FORECAST_TEMPLATE)
unique_id = config.get(CONF_UNIQUE_ID)
Expand All @@ -89,6 +95,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
temperature_template,
humidity_template,
pressure_template,
visibility_template,
wind_bearing_template,
wind_speed_template,
forecast_template,
unique_id,
Expand All @@ -108,6 +116,8 @@ def __init__(
temperature_template,
humidity_template,
pressure_template,
visibility_template,
wind_bearing_template,
wind_speed_template,
forecast_template,
unique_id,
Expand All @@ -120,6 +130,8 @@ def __init__(
self._temperature_template = temperature_template
self._humidity_template = humidity_template
self._pressure_template = pressure_template
self._visibility_template = visibility_template
self._wind_bearing_template = wind_bearing_template
self._wind_speed_template = wind_speed_template
self._forecast_template = forecast_template
self._unique_id = unique_id
Expand All @@ -130,6 +142,8 @@ def __init__(
self._temperature = None
self._humidity = None
self._pressure = None
self._visibility = None
self._wind_bearing = None
self._wind_speed = None
self._forecast = []

Expand Down Expand Up @@ -158,6 +172,16 @@ def humidity(self):
"""Return the humidity."""
return self._humidity

@property
def visibility(self):
"""Return the visibility."""
return self._visibility

@property
def wind_bearing(self):
"""Return the wind direction."""
return self._wind_bearing

@property
def wind_speed(self):
"""Return the wind speed."""
Expand Down Expand Up @@ -207,6 +231,16 @@ async def async_added_to_hass(self):
"_pressure",
self._pressure_template,
)
if self._visibility_template:
self.add_template_attribute(
"_visibility",
self._visibility_template,
)
if self._wind_bearing_template:
self.add_template_attribute(
"_wind_bearing",
self._wind_bearing_template,
)
if self._wind_speed_template:
self.add_template_attribute(
"_wind_speed",
Expand Down
10 changes: 10 additions & 0 deletions tests/components/template/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
ATTR_WEATHER_HUMIDITY,
ATTR_WEATHER_PRESSURE,
ATTR_WEATHER_TEMPERATURE,
ATTR_WEATHER_VISIBILITY,
ATTR_WEATHER_WIND_BEARING,
ATTR_WEATHER_WIND_SPEED,
DOMAIN,
)
Expand All @@ -25,6 +27,8 @@ async def test_template_state_text(hass):
"temperature_template": "{{ states('sensor.temperature') | float }}",
"humidity_template": "{{ states('sensor.humidity') | int }}",
"pressure_template": "{{ states('sensor.pressure') }}",
"visibility_template": "{{ states('sensor.visibility') }}",
"wind_bearing_template": "{{ states('sensor.wind_direction') }}",
"wind_speed_template": "{{ states('sensor.windspeed') }}",
},
]
Expand All @@ -41,6 +45,10 @@ async def test_template_state_text(hass):
await hass.async_block_till_done()
hass.states.async_set("sensor.pressure", 1000)
await hass.async_block_till_done()
hass.states.async_set("sensor.visibility", 20)
await hass.async_block_till_done()
hass.states.async_set("sensor.wind_direction", 45)
await hass.async_block_till_done()
hass.states.async_set("sensor.windspeed", 20)
await hass.async_block_till_done()

Expand All @@ -53,4 +61,6 @@ async def test_template_state_text(hass):
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_VISIBILITY) == 20
assert data.get(ATTR_WEATHER_WIND_BEARING) == 45
assert data.get(ATTR_WEATHER_WIND_SPEED) == 20