Skip to content

Commit 4a10fb5

Browse files
committed
Use more attributes from base classes like others do
1 parent bb11238 commit 4a10fb5

File tree

1 file changed

+22
-59
lines changed

1 file changed

+22
-59
lines changed

custom_components/dwd/weather.py

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,14 @@ def __init__(
151151
"""Initialize."""
152152
super().__init__(coordinator)
153153
self._hass: HomeAssistant = hass
154-
self._unique_id: str = unique_id
154+
self._attr_unique_id = unique_id
155155
self._config: ConfigEntry = config
156156
self._forecast_mode: ForecastMode = forecast_mode
157-
self._device: DeviceInfo = device
157+
self._attr_device_info = device
158158
self._conf_current_weather: str = self._config.options.get(
159159
CONF_CURRENT_WEATHER, CONF_CURRENT_WEATHER_DEFAULT
160160
)
161161

162-
self._attr_supported_features = 0
163-
if self._config.options.get(CONF_FORECAST, CONF_FORECAST_DEFAULT):
164-
if self._forecast_mode in (ForecastMode.STANDARD, ForecastMode.DAILY):
165-
self._attr_supported_features |= WeatherEntityFeature.FORECAST_DAILY
166-
if self._forecast_mode in (ForecastMode.STANDARD, ForecastMode.HOURLY):
167-
self._attr_supported_features |= WeatherEntityFeature.FORECAST_HOURLY
168-
169-
@property
170-
def unique_id(self) -> str:
171-
"""Return unique ID."""
172-
return self._unique_id
173-
174-
@property
175-
def name(self) -> str:
176-
"""Return the name of the sensor."""
177-
178162
name = self._config.title
179163
name_appendix = ""
180164

@@ -189,23 +173,32 @@ def name(self) -> str:
189173
if name is None:
190174
name = "DWD"
191175

192-
return f"{name}{name_appendix}"
176+
self._attr_name = f"{name}{name_appendix}"
177+
178+
self._attr_entity_registry_enabled_default = (
179+
self._forecast_mode == ForecastMode.STANDARD
180+
)
181+
182+
self._attr_supported_features = 0
183+
if self._config.options.get(CONF_FORECAST, CONF_FORECAST_DEFAULT):
184+
if self._forecast_mode in (ForecastMode.STANDARD, ForecastMode.DAILY):
185+
self._attr_supported_features |= WeatherEntityFeature.FORECAST_DAILY
186+
if self._forecast_mode in (ForecastMode.STANDARD, ForecastMode.HOURLY):
187+
self._attr_supported_features |= WeatherEntityFeature.FORECAST_HOURLY
188+
189+
self._attr_native_temperature_unit = UnitOfTemperature.CELSIUS
190+
self._attr_native_pressure_unit = UnitOfPressure.HPA
191+
self._attr_native_visibility_unit = UnitOfLength.KILOMETERS
192+
self._attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
193+
self._attr_native_precipitation_unit = UnitOfLength.MILLIMETERS
194+
195+
self._attr_attribution = ATTRIBUTION
193196

194197
@property
195198
def available(self) -> bool:
196199
"""Return True if entity is available."""
197200
return self.coordinator.last_update_success
198201

199-
@property
200-
def device_info(self) -> DeviceInfo:
201-
"""Device info."""
202-
return self._device
203-
204-
@property
205-
def entity_registry_enabled_default(self) -> bool:
206-
"""Return if the entity should be enabled when first added to the entity registry."""
207-
return self._forecast_mode == ForecastMode.STANDARD
208-
209202
@property
210203
def condition(self) -> str | None:
211204
"""Return the current condition."""
@@ -246,11 +239,6 @@ def native_temperature(self) -> float | None:
246239
DWD_MEASUREMENT_TEMPERATURE, ATTR_FORECAST_NATIVE_TEMP
247240
)
248241

249-
@property
250-
def native_temperature_unit(self) -> str:
251-
"""Return the native unit of measurement for temperature."""
252-
return UnitOfTemperature.CELSIUS
253-
254242
@property
255243
def native_dew_point(self) -> float | None:
256244
"""Return the dew point temperature in native units."""
@@ -265,11 +253,6 @@ def native_pressure(self) -> float | None:
265253
DWD_MEASUREMENT_PRESSURE, ATTR_FORECAST_NATIVE_PRESSURE
266254
)
267255

268-
@property
269-
def native_pressure_unit(self) -> str:
270-
"""Return the native unit of measurement for pressure."""
271-
return UnitOfPressure.HPA
272-
273256
@property
274257
def humidity(self) -> float | None:
275258
"""Return the humidity in native units."""
@@ -287,11 +270,6 @@ def native_visibility(self) -> float | None:
287270
"""Return the visibility in native units."""
288271
return self._get_float_measurement_without_fallback(DWD_MEASUREMENT_VISIBILITY)
289272

290-
@property
291-
def native_visibility_unit(self) -> str:
292-
"""Return the native unit of measurement for visibility."""
293-
return UnitOfLength.KILOMETERS
294-
295273
@property
296274
def native_wind_gust_speed(self) -> float | None:
297275
"""Return the wind gust speed in native units."""
@@ -306,16 +284,6 @@ def native_wind_speed(self) -> float | None:
306284
DWD_MEASUREMENT_MEANWIND_SPEED, ATTR_FORECAST_NATIVE_WIND_SPEED
307285
)
308286

309-
@property
310-
def native_wind_speed_unit(self) -> str:
311-
"""Return the native unit of measurement for wind speed."""
312-
return UnitOfSpeed.KILOMETERS_PER_HOUR
313-
314-
@property
315-
def native_precipitation_unit(self) -> str:
316-
"""Return the native unit of measurement for accumulated precipitation."""
317-
return UnitOfLength.MILLIMETERS
318-
319287
@property
320288
def wind_bearing(self) -> float | None:
321289
"""Return the wind bearing."""
@@ -370,11 +338,6 @@ def _get_float_measurement_without_fallback(
370338
else:
371339
return None
372340

373-
@property
374-
def attribution(self) -> str:
375-
"""Return the attribution."""
376-
return ATTRIBUTION
377-
378341
@property
379342
def forecast(self):
380343
"""Return the forecast array."""

0 commit comments

Comments
 (0)