mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Migrate darksky to native_* (#74047)
This commit is contained in:
parent
f45afe7379
commit
0769b33e19
@ -21,12 +21,12 @@ from homeassistant.components.weather import (
|
||||
ATTR_CONDITION_SUNNY,
|
||||
ATTR_CONDITION_WINDY,
|
||||
ATTR_FORECAST_CONDITION,
|
||||
ATTR_FORECAST_PRECIPITATION,
|
||||
ATTR_FORECAST_TEMP,
|
||||
ATTR_FORECAST_TEMP_LOW,
|
||||
ATTR_FORECAST_NATIVE_PRECIPITATION,
|
||||
ATTR_FORECAST_NATIVE_TEMP,
|
||||
ATTR_FORECAST_NATIVE_TEMP_LOW,
|
||||
ATTR_FORECAST_NATIVE_WIND_SPEED,
|
||||
ATTR_FORECAST_TIME,
|
||||
ATTR_FORECAST_WIND_BEARING,
|
||||
ATTR_FORECAST_WIND_SPEED,
|
||||
PLATFORM_SCHEMA,
|
||||
WeatherEntity,
|
||||
)
|
||||
@ -36,10 +36,11 @@ from homeassistant.const import (
|
||||
CONF_LONGITUDE,
|
||||
CONF_MODE,
|
||||
CONF_NAME,
|
||||
PRESSURE_HPA,
|
||||
PRESSURE_INHG,
|
||||
LENGTH_KILOMETERS,
|
||||
LENGTH_MILLIMETERS,
|
||||
PRESSURE_MBAR,
|
||||
SPEED_METERS_PER_SECOND,
|
||||
TEMP_CELSIUS,
|
||||
TEMP_FAHRENHEIT,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
@ -47,7 +48,6 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.util.dt import utc_from_timestamp
|
||||
from homeassistant.util.pressure import convert as convert_pressure
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -75,15 +75,18 @@ CONF_UNITS = "units"
|
||||
|
||||
DEFAULT_NAME = "Dark Sky"
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_API_KEY): cv.string,
|
||||
vol.Optional(CONF_LATITUDE): cv.latitude,
|
||||
vol.Optional(CONF_LONGITUDE): cv.longitude,
|
||||
vol.Optional(CONF_MODE, default="hourly"): vol.In(FORECAST_MODE),
|
||||
vol.Optional(CONF_UNITS): vol.In(["auto", "si", "us", "ca", "uk", "uk2"]),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
}
|
||||
PLATFORM_SCHEMA = vol.All(
|
||||
cv.removed(CONF_UNITS),
|
||||
PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_API_KEY): cv.string,
|
||||
vol.Optional(CONF_LATITUDE): cv.latitude,
|
||||
vol.Optional(CONF_LONGITUDE): cv.longitude,
|
||||
vol.Optional(CONF_MODE, default="hourly"): vol.In(FORECAST_MODE),
|
||||
vol.Optional(CONF_UNITS): vol.In(["auto", "si", "us", "ca", "uk", "uk2"]),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=3)
|
||||
@ -101,9 +104,7 @@ def setup_platform(
|
||||
name = config.get(CONF_NAME)
|
||||
mode = config.get(CONF_MODE)
|
||||
|
||||
if not (units := config.get(CONF_UNITS)):
|
||||
units = "ca" if hass.config.units.is_metric else "us"
|
||||
|
||||
units = "si"
|
||||
dark_sky = DarkSkyData(config.get(CONF_API_KEY), latitude, longitude, units)
|
||||
|
||||
add_entities([DarkSkyWeather(name, dark_sky, mode)], True)
|
||||
@ -112,6 +113,12 @@ def setup_platform(
|
||||
class DarkSkyWeather(WeatherEntity):
|
||||
"""Representation of a weather condition."""
|
||||
|
||||
_attr_native_precipitation_unit = LENGTH_MILLIMETERS
|
||||
_attr_native_pressure_unit = PRESSURE_MBAR
|
||||
_attr_native_temperature_unit = TEMP_CELSIUS
|
||||
_attr_native_visibility_unit = LENGTH_KILOMETERS
|
||||
_attr_native_wind_speed_unit = SPEED_METERS_PER_SECOND
|
||||
|
||||
def __init__(self, name, dark_sky, mode):
|
||||
"""Initialize Dark Sky weather."""
|
||||
self._name = name
|
||||
@ -139,24 +146,17 @@ class DarkSkyWeather(WeatherEntity):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def temperature(self):
|
||||
def native_temperature(self):
|
||||
"""Return the temperature."""
|
||||
return self._ds_currently.get("temperature")
|
||||
|
||||
@property
|
||||
def temperature_unit(self):
|
||||
"""Return the unit of measurement."""
|
||||
if self._dark_sky.units is None:
|
||||
return None
|
||||
return TEMP_FAHRENHEIT if "us" in self._dark_sky.units else TEMP_CELSIUS
|
||||
|
||||
@property
|
||||
def humidity(self):
|
||||
"""Return the humidity."""
|
||||
return round(self._ds_currently.get("humidity") * 100.0, 2)
|
||||
|
||||
@property
|
||||
def wind_speed(self):
|
||||
def native_wind_speed(self):
|
||||
"""Return the wind speed."""
|
||||
return self._ds_currently.get("windSpeed")
|
||||
|
||||
@ -171,15 +171,12 @@ class DarkSkyWeather(WeatherEntity):
|
||||
return self._ds_currently.get("ozone")
|
||||
|
||||
@property
|
||||
def pressure(self):
|
||||
def native_pressure(self):
|
||||
"""Return the pressure."""
|
||||
pressure = self._ds_currently.get("pressure")
|
||||
if "us" in self._dark_sky.units:
|
||||
return round(convert_pressure(pressure, PRESSURE_HPA, PRESSURE_INHG), 2)
|
||||
return pressure
|
||||
return self._ds_currently.get("pressure")
|
||||
|
||||
@property
|
||||
def visibility(self):
|
||||
def native_visibility(self):
|
||||
"""Return the visibility."""
|
||||
return self._ds_currently.get("visibility")
|
||||
|
||||
@ -208,12 +205,12 @@ class DarkSkyWeather(WeatherEntity):
|
||||
ATTR_FORECAST_TIME: utc_from_timestamp(
|
||||
entry.d.get("time")
|
||||
).isoformat(),
|
||||
ATTR_FORECAST_TEMP: entry.d.get("temperatureHigh"),
|
||||
ATTR_FORECAST_TEMP_LOW: entry.d.get("temperatureLow"),
|
||||
ATTR_FORECAST_PRECIPITATION: calc_precipitation(
|
||||
ATTR_FORECAST_NATIVE_TEMP: entry.d.get("temperatureHigh"),
|
||||
ATTR_FORECAST_NATIVE_TEMP_LOW: entry.d.get("temperatureLow"),
|
||||
ATTR_FORECAST_NATIVE_PRECIPITATION: calc_precipitation(
|
||||
entry.d.get("precipIntensity"), 24
|
||||
),
|
||||
ATTR_FORECAST_WIND_SPEED: entry.d.get("windSpeed"),
|
||||
ATTR_FORECAST_NATIVE_WIND_SPEED: entry.d.get("windSpeed"),
|
||||
ATTR_FORECAST_WIND_BEARING: entry.d.get("windBearing"),
|
||||
ATTR_FORECAST_CONDITION: MAP_CONDITION.get(entry.d.get("icon")),
|
||||
}
|
||||
@ -225,8 +222,8 @@ class DarkSkyWeather(WeatherEntity):
|
||||
ATTR_FORECAST_TIME: utc_from_timestamp(
|
||||
entry.d.get("time")
|
||||
).isoformat(),
|
||||
ATTR_FORECAST_TEMP: entry.d.get("temperature"),
|
||||
ATTR_FORECAST_PRECIPITATION: calc_precipitation(
|
||||
ATTR_FORECAST_NATIVE_TEMP: entry.d.get("temperature"),
|
||||
ATTR_FORECAST_NATIVE_PRECIPITATION: calc_precipitation(
|
||||
entry.d.get("precipIntensity"), 1
|
||||
),
|
||||
ATTR_FORECAST_CONDITION: MAP_CONDITION.get(entry.d.get("icon")),
|
||||
@ -281,10 +278,3 @@ class DarkSkyData:
|
||||
self._connect_error = True
|
||||
_LOGGER.error("Unable to connect to Dark Sky. %s", error)
|
||||
self.data = None
|
||||
|
||||
@property
|
||||
def units(self):
|
||||
"""Get the unit system of returned data."""
|
||||
if self.data is None:
|
||||
return None
|
||||
return self.data.json.get("flags").get("units")
|
||||
|
Loading…
x
Reference in New Issue
Block a user