1 """Weather entity for Apple WeatherKit integration."""
3 from typing
import Any, cast
5 from apple_weatherkit
import DataSetType
9 ATTR_CONDITION_EXCEPTIONAL,
12 ATTR_CONDITION_LIGHTNING,
13 ATTR_CONDITION_PARTLYCLOUDY,
14 ATTR_CONDITION_POURING,
17 ATTR_CONDITION_SNOWY_RAINY,
21 SingleCoordinatorWeatherEntity,
41 from .coordinator
import WeatherKitDataUpdateCoordinator
42 from .entity
import WeatherKitEntity
47 config_entry: ConfigEntry,
48 async_add_entities: AddEntitiesCallback,
50 """Add a weather entity from a config_entry."""
51 coordinator: WeatherKitDataUpdateCoordinator = hass.data[DOMAIN][
58 condition_code_to_hass = {
59 "BlowingDust": ATTR_CONDITION_WINDY,
60 "Clear": ATTR_CONDITION_SUNNY,
61 "Cloudy": ATTR_CONDITION_CLOUDY,
62 "Foggy": ATTR_CONDITION_FOG,
63 "Haze": ATTR_CONDITION_FOG,
64 "MostlyClear": ATTR_CONDITION_SUNNY,
65 "MostlyCloudy": ATTR_CONDITION_CLOUDY,
66 "PartlyCloudy": ATTR_CONDITION_PARTLYCLOUDY,
67 "Smoky": ATTR_CONDITION_FOG,
68 "Breezy": ATTR_CONDITION_WINDY,
69 "Windy": ATTR_CONDITION_WINDY,
70 "Drizzle": ATTR_CONDITION_RAINY,
71 "HeavyRain": ATTR_CONDITION_POURING,
72 "IsolatedThunderstorms": ATTR_CONDITION_LIGHTNING,
73 "Rain": ATTR_CONDITION_RAINY,
74 "SunShowers": ATTR_CONDITION_RAINY,
75 "ScatteredThunderstorms": ATTR_CONDITION_LIGHTNING,
76 "StrongStorms": ATTR_CONDITION_LIGHTNING,
77 "Thunderstorms": ATTR_CONDITION_LIGHTNING,
78 "Frigid": ATTR_CONDITION_SNOWY,
79 "Hail": ATTR_CONDITION_HAIL,
80 "Hot": ATTR_CONDITION_SUNNY,
81 "Flurries": ATTR_CONDITION_SNOWY,
82 "Sleet": ATTR_CONDITION_SNOWY,
83 "Snow": ATTR_CONDITION_SNOWY,
84 "SunFlurries": ATTR_CONDITION_SNOWY,
85 "WintryMix": ATTR_CONDITION_SNOWY,
86 "Blizzard": ATTR_CONDITION_SNOWY,
87 "BlowingSnow": ATTR_CONDITION_SNOWY,
88 "FreezingDrizzle": ATTR_CONDITION_SNOWY_RAINY,
89 "FreezingRain": ATTR_CONDITION_SNOWY_RAINY,
90 "HeavySnow": ATTR_CONDITION_SNOWY,
91 "Hurricane": ATTR_CONDITION_EXCEPTIONAL,
92 "TropicalStorm": ATTR_CONDITION_EXCEPTIONAL,
98 "datetime": forecast[
"forecastStart"],
99 "condition": condition_code_to_hass[forecast[
"conditionCode"]],
100 "native_temperature": forecast[
"temperatureMax"],
101 "native_templow": forecast[
"temperatureMin"],
102 "native_precipitation": forecast[
"precipitationAmount"],
103 "precipitation_probability": forecast[
"precipitationChance"] * 100,
104 "uv_index": forecast[
"maxUvIndex"],
110 "datetime": forecast[
"forecastStart"],
111 "condition": condition_code_to_hass[forecast[
"conditionCode"]],
112 "native_temperature": forecast[
"temperature"],
113 "native_apparent_temperature": forecast[
"temperatureApparent"],
114 "native_dew_point": forecast.get(
"temperatureDewPoint"),
115 "native_pressure": forecast[
"pressure"],
116 "native_wind_gust_speed": forecast.get(
"windGust"),
117 "native_wind_speed": forecast[
"windSpeed"],
118 "wind_bearing": forecast.get(
"windDirection"),
119 "humidity": forecast[
"humidity"] * 100,
120 "native_precipitation": forecast.get(
"precipitationAmount"),
121 "precipitation_probability": forecast[
"precipitationChance"] * 100,
122 "cloud_coverage": forecast[
"cloudCover"] * 100,
123 "uv_index": forecast[
"uvIndex"],
128 SingleCoordinatorWeatherEntity[WeatherKitDataUpdateCoordinator], WeatherKitEntity
130 """Weather entity for Apple WeatherKit integration."""
132 _attr_attribution = ATTRIBUTION
136 _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
137 _attr_native_pressure_unit = UnitOfPressure.MBAR
138 _attr_native_visibility_unit = UnitOfLength.KILOMETERS
139 _attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
140 _attr_native_precipitation_unit = UnitOfLength.MILLIMETERS
144 coordinator: WeatherKitDataUpdateCoordinator,
146 """Initialize the platform with a coordinator."""
148 WeatherKitEntity.__init__(self, coordinator, unique_id_suffix=
None)
152 """Determine supported features based on available data sets reported by WeatherKit."""
153 features = WeatherEntityFeature(0)
155 if not self.coordinator.supported_data_sets:
158 if DataSetType.DAILY_FORECAST
in self.coordinator.supported_data_sets:
159 features |= WeatherEntityFeature.FORECAST_DAILY
160 if DataSetType.HOURLY_FORECAST
in self.coordinator.supported_data_sets:
161 features |= WeatherEntityFeature.FORECAST_HOURLY
165 def data(self) -> dict[str, Any]:
166 """Return coordinator data."""
167 return self.coordinator.data
171 """Return current weather data."""
172 return self.
datadatadata[ATTR_CURRENT_WEATHER]
176 """Return the current condition."""
178 condition = condition_code_to_hass[condition_code]
180 if condition ==
"sunny" and self.
current_weathercurrent_weather.
get(
"daylight")
is False:
181 condition =
"clear-night"
187 """Return the current temperature."""
192 """Return the current apparent_temperature."""
197 """Return the current dew_point."""
202 """Return the current pressure."""
207 """Return the current humidity."""
212 """Return the current cloud_coverage."""
217 """Return the current uv_index."""
222 """Return the current visibility."""
227 """Return the current wind_gust_speed."""
232 """Return the current wind_speed."""
237 """Return the current wind_bearing."""
242 """Return the daily forecast."""
243 daily_forecast = self.
datadatadata.
get(ATTR_FORECAST_DAILY)
244 if not daily_forecast:
247 forecast = daily_forecast.get(
"days")
252 """Return the hourly forecast."""
253 hourly_forecast = self.
datadatadata.
get(ATTR_FORECAST_HOURLY)
254 if not hourly_forecast:
257 forecast = hourly_forecast.get(
"hours")
WeatherEntityFeature supported_features(self)
float|None cloud_coverage(self)
float|None wind_bearing(self)
list[Forecast]|None _async_forecast_daily(self)
float|None native_visibility(self)
float|None native_temperature(self)
list[Forecast]|None _async_forecast_hourly(self)
float|None native_pressure(self)
float|None native_wind_speed(self)
float|None native_dew_point(self)
dict[str, Any] data(self)
float|None uv_index(self)
dict[str, Any] current_weather(self)
None __init__(self, WeatherKitDataUpdateCoordinator coordinator)
float|None humidity(self)
float|None native_apparent_temperature(self)
float|None native_wind_gust_speed(self)
web.Response get(self, web.Request request, str config_key)
Forecast _map_hourly_forecast(dict[str, Any] forecast)
Forecast _map_daily_forecast(dict[str, Any] forecast)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)