Use shorthand attributes in NWS (#99620)

This commit is contained in:
Joost Lekkerkerker 2023-09-12 20:05:57 +02:00 committed by GitHub
parent 9c775a8a24
commit 69ac8a0a2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 51 deletions

View file

@ -23,7 +23,6 @@ from homeassistant.const import (
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util.dt import utcnow
@ -163,6 +162,7 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
entity_description: NWSSensorEntityDescription
_attr_attribution = ATTRIBUTION
_attr_entity_registry_enabled_default = False
def __init__(
self,
@ -175,13 +175,17 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
"""Initialise the platform with a data instance."""
super().__init__(nws_data.coordinator_observation)
self._nws = nws_data.api
self._latitude = entry_data[CONF_LATITUDE]
self._longitude = entry_data[CONF_LONGITUDE]
latitude = entry_data[CONF_LATITUDE]
longitude = entry_data[CONF_LONGITUDE]
self.entity_description = description
self._attr_name = f"{station} {description.name}"
if hass.config.units is US_CUSTOMARY_SYSTEM:
self._attr_native_unit_of_measurement = description.unit_convert
self._attr_device_info = device_info(latitude, longitude)
self._attr_unique_id = (
f"{base_unique_id(latitude, longitude)}_{description.key}"
)
@property
def native_value(self) -> float | None:
@ -219,11 +223,6 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
return round(value)
return value
@property
def unique_id(self) -> str:
"""Return a unique_id for this entity."""
return f"{base_unique_id(self._latitude, self._longitude)}_{self.entity_description.key}"
@property
def available(self) -> bool:
"""Return if state is available."""
@ -235,13 +234,3 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
else:
last_success_time = False
return self.coordinator.last_update_success or last_success_time
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return False
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return device_info(self._latitude, self._longitude)

View file

@ -32,7 +32,6 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.dt import utcnow
from homeassistant.util.unit_conversion import SpeedConverter, TemperatureConverter
@ -121,6 +120,10 @@ class NWSWeather(CoordinatorWeatherEntity):
_attr_supported_features = (
WeatherEntityFeature.FORECAST_HOURLY | WeatherEntityFeature.FORECAST_TWICE_DAILY
)
_attr_native_temperature_unit = UnitOfTemperature.CELSIUS
_attr_native_pressure_unit = UnitOfPressure.PA
_attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
_attr_native_visibility_unit = UnitOfLength.METERS
def __init__(
self,
@ -137,8 +140,8 @@ class NWSWeather(CoordinatorWeatherEntity):
twice_daily_forecast_valid=FORECAST_VALID_TIME,
)
self.nws = nws_data.api
self.latitude = entry_data[CONF_LATITUDE]
self.longitude = entry_data[CONF_LONGITUDE]
latitude = entry_data[CONF_LATITUDE]
longitude = entry_data[CONF_LONGITUDE]
if mode == DAYNIGHT:
self.coordinator_forecast_legacy = nws_data.coordinator_forecast
else:
@ -153,6 +156,8 @@ class NWSWeather(CoordinatorWeatherEntity):
self._forecast_twice_daily: list[dict[str, Any]] | None = None
self._attr_unique_id = _calculate_unique_id(entry_data, mode)
self._attr_device_info = device_info(latitude, longitude)
self._attr_name = f"{self.station} {self.mode.title()}"
async def async_added_to_hass(self) -> None:
"""Set up a listener and load data."""
@ -193,11 +198,6 @@ class NWSWeather(CoordinatorWeatherEntity):
self._forecast_legacy = self.nws.forecast_hourly
self.async_write_ha_state()
@property
def name(self) -> str:
"""Return the name of the station."""
return f"{self.station} {self.mode.title()}"
@property
def native_temperature(self) -> float | None:
"""Return the current temperature."""
@ -205,11 +205,6 @@ class NWSWeather(CoordinatorWeatherEntity):
return self.observation.get("temperature")
return None
@property
def native_temperature_unit(self) -> str:
"""Return the current temperature unit."""
return UnitOfTemperature.CELSIUS
@property
def native_pressure(self) -> int | None:
"""Return the current pressure."""
@ -217,11 +212,6 @@ class NWSWeather(CoordinatorWeatherEntity):
return self.observation.get("seaLevelPressure")
return None
@property
def native_pressure_unit(self) -> str:
"""Return the current pressure unit."""
return UnitOfPressure.PA
@property
def humidity(self) -> float | None:
"""Return the name of the sensor."""
@ -236,11 +226,6 @@ class NWSWeather(CoordinatorWeatherEntity):
return self.observation.get("windSpeed")
return None
@property
def native_wind_speed_unit(self) -> str:
"""Return the current windspeed."""
return UnitOfSpeed.KILOMETERS_PER_HOUR
@property
def wind_bearing(self) -> int | None:
"""Return the current wind bearing (degrees)."""
@ -267,11 +252,6 @@ class NWSWeather(CoordinatorWeatherEntity):
return self.observation.get("visibility")
return None
@property
def native_visibility_unit(self) -> str:
"""Return visibility unit."""
return UnitOfLength.METERS
def _forecast(
self, nws_forecast: list[dict[str, Any]] | None, mode: str
) -> list[Forecast] | None:
@ -377,8 +357,3 @@ class NWSWeather(CoordinatorWeatherEntity):
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return self.mode == DAYNIGHT
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return device_info(self.latitude, self.longitude)