Native to Weather Template (#74060)

* Native to Weather template

* Add validation
This commit is contained in:
G Johansson 2022-06-28 13:42:58 +02:00 committed by GitHub
parent c1f621e9c0
commit 4b5c0be896
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,15 +20,22 @@ from homeassistant.components.weather import (
ATTR_CONDITION_WINDY, ATTR_CONDITION_WINDY,
ATTR_CONDITION_WINDY_VARIANT, ATTR_CONDITION_WINDY_VARIANT,
ENTITY_ID_FORMAT, ENTITY_ID_FORMAT,
Forecast,
WeatherEntity, WeatherEntity,
) )
from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID from homeassistant.const import CONF_NAME, CONF_TEMPERATURE_UNIT, CONF_UNIQUE_ID
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import (
distance as distance_util,
pressure as pressure_util,
speed as speed_util,
temperature as temp_util,
)
from .template_entity import TemplateEntity, rewrite_common_legacy_to_modern_conf from .template_entity import TemplateEntity, rewrite_common_legacy_to_modern_conf
@ -61,6 +68,10 @@ CONF_WIND_BEARING_TEMPLATE = "wind_bearing_template"
CONF_OZONE_TEMPLATE = "ozone_template" CONF_OZONE_TEMPLATE = "ozone_template"
CONF_VISIBILITY_TEMPLATE = "visibility_template" CONF_VISIBILITY_TEMPLATE = "visibility_template"
CONF_FORECAST_TEMPLATE = "forecast_template" CONF_FORECAST_TEMPLATE = "forecast_template"
CONF_PRESSURE_UNIT = "pressure_unit"
CONF_WIND_SPEED_UNIT = "wind_speed_unit"
CONF_VISIBILITY_UNIT = "visibility_unit"
CONF_PRECIPITATION_UNIT = "precipitation_unit"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
@ -76,6 +87,11 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
vol.Optional(CONF_VISIBILITY_TEMPLATE): cv.template, vol.Optional(CONF_VISIBILITY_TEMPLATE): cv.template,
vol.Optional(CONF_FORECAST_TEMPLATE): cv.template, vol.Optional(CONF_FORECAST_TEMPLATE): cv.template,
vol.Optional(CONF_UNIQUE_ID): cv.string, vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_TEMPERATURE_UNIT): vol.In(temp_util.VALID_UNITS),
vol.Optional(CONF_PRESSURE_UNIT): vol.In(pressure_util.VALID_UNITS),
vol.Optional(CONF_WIND_SPEED_UNIT): vol.In(speed_util.VALID_UNITS),
vol.Optional(CONF_VISIBILITY_UNIT): vol.In(distance_util.VALID_UNITS),
vol.Optional(CONF_PRECIPITATION_UNIT): vol.In(distance_util.VALID_UNITS),
} }
) )
@ -109,10 +125,10 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
def __init__( def __init__(
self, self,
hass, hass: HomeAssistant,
config, config: ConfigType,
unique_id, unique_id: str | None,
): ) -> None:
"""Initialize the Template weather.""" """Initialize the Template weather."""
super().__init__(hass, config=config, unique_id=unique_id) super().__init__(hass, config=config, unique_id=unique_id)
@ -128,6 +144,12 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
self._visibility_template = config.get(CONF_VISIBILITY_TEMPLATE) self._visibility_template = config.get(CONF_VISIBILITY_TEMPLATE)
self._forecast_template = config.get(CONF_FORECAST_TEMPLATE) self._forecast_template = config.get(CONF_FORECAST_TEMPLATE)
self._attr_native_precipitation_unit = config.get(CONF_PRECIPITATION_UNIT)
self._attr_native_pressure_unit = config.get(CONF_PRESSURE_UNIT)
self._attr_native_temperature_unit = config.get(CONF_TEMPERATURE_UNIT)
self._attr_native_visibility_unit = config.get(CONF_VISIBILITY_UNIT)
self._attr_native_wind_speed_unit = config.get(CONF_WIND_SPEED_UNIT)
self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, name, hass=hass) self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, name, hass=hass)
self._condition = None self._condition = None
@ -139,66 +161,61 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
self._wind_bearing = None self._wind_bearing = None
self._ozone = None self._ozone = None
self._visibility = None self._visibility = None
self._forecast = [] self._forecast: list[Forecast] = []
@property @property
def condition(self): def condition(self) -> str | None:
"""Return the current condition.""" """Return the current condition."""
return self._condition return self._condition
@property @property
def temperature(self): def native_temperature(self) -> float | None:
"""Return the temperature.""" """Return the temperature."""
return self._temperature return self._temperature
@property @property
def temperature_unit(self): def humidity(self) -> float | None:
"""Return the unit of measurement."""
return self.hass.config.units.temperature_unit
@property
def humidity(self):
"""Return the humidity.""" """Return the humidity."""
return self._humidity return self._humidity
@property @property
def wind_speed(self): def native_wind_speed(self) -> float | None:
"""Return the wind speed.""" """Return the wind speed."""
return self._wind_speed return self._wind_speed
@property @property
def wind_bearing(self): def wind_bearing(self) -> float | str | None:
"""Return the wind bearing.""" """Return the wind bearing."""
return self._wind_bearing return self._wind_bearing
@property @property
def ozone(self): def ozone(self) -> float | None:
"""Return the ozone level.""" """Return the ozone level."""
return self._ozone return self._ozone
@property @property
def visibility(self): def native_visibility(self) -> float | None:
"""Return the visibility.""" """Return the visibility."""
return self._visibility return self._visibility
@property @property
def pressure(self): def native_pressure(self) -> float | None:
"""Return the air pressure.""" """Return the air pressure."""
return self._pressure return self._pressure
@property @property
def forecast(self): def forecast(self) -> list[Forecast]:
"""Return the forecast.""" """Return the forecast."""
return self._forecast return self._forecast
@property @property
def attribution(self): def attribution(self) -> str | None:
"""Return the attribution.""" """Return the attribution."""
if self._attribution is None: if self._attribution is None:
return "Powered by Home Assistant" return "Powered by Home Assistant"
return self._attribution return self._attribution
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Register callbacks.""" """Register callbacks."""
if self._condition_template: if self._condition_template: