Skip to content
Merged
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
71 changes: 68 additions & 3 deletions homeassistant/components/template/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@
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(
Expand All @@ -59,8 +63,12 @@
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,
}
Expand All @@ -75,8 +83,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)

Expand All @@ -88,8 +100,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,
)
Expand All @@ -107,8 +123,12 @@ def __init__(
condition_template,
temperature_template,
humidity_template,
attribution_template,
pressure_template,
wind_speed_template,
wind_bearing_template,
ozone_template,
visibility_template,
forecast_template,
unique_id,
):
Expand All @@ -119,8 +139,12 @@ def __init__(
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

Expand All @@ -129,8 +153,12 @@ def __init__(
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
Expand Down Expand Up @@ -163,9 +191,24 @@ def wind_speed(self):
"""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
Expand All @@ -176,11 +219,13 @@ def forecast(self):
@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):
Expand All @@ -202,6 +247,11 @@ async def async_added_to_hass(self):
"_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",
Expand All @@ -212,6 +262,21 @@ async def async_added_to_hass(self):
"_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",
Expand Down
20 changes: 20 additions & 0 deletions tests/components/template/test_weather.py
Original file line number Diff line number Diff line change
@@ -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,
)
Expand All @@ -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') }}",
},
]
},
Expand All @@ -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)
Expand All @@ -43,14 +53,24 @@ 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

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