Use shorthand attributes in NWS (#99620)
This commit is contained in:
parent
9c775a8a24
commit
69ac8a0a2b
2 changed files with 15 additions and 51 deletions
|
@ -23,7 +23,6 @@ from homeassistant.const import (
|
||||||
UnitOfTemperature,
|
UnitOfTemperature,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
@ -163,6 +162,7 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
|
||||||
|
|
||||||
entity_description: NWSSensorEntityDescription
|
entity_description: NWSSensorEntityDescription
|
||||||
_attr_attribution = ATTRIBUTION
|
_attr_attribution = ATTRIBUTION
|
||||||
|
_attr_entity_registry_enabled_default = False
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
@ -175,13 +175,17 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
|
||||||
"""Initialise the platform with a data instance."""
|
"""Initialise the platform with a data instance."""
|
||||||
super().__init__(nws_data.coordinator_observation)
|
super().__init__(nws_data.coordinator_observation)
|
||||||
self._nws = nws_data.api
|
self._nws = nws_data.api
|
||||||
self._latitude = entry_data[CONF_LATITUDE]
|
latitude = entry_data[CONF_LATITUDE]
|
||||||
self._longitude = entry_data[CONF_LONGITUDE]
|
longitude = entry_data[CONF_LONGITUDE]
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
|
|
||||||
self._attr_name = f"{station} {description.name}"
|
self._attr_name = f"{station} {description.name}"
|
||||||
if hass.config.units is US_CUSTOMARY_SYSTEM:
|
if hass.config.units is US_CUSTOMARY_SYSTEM:
|
||||||
self._attr_native_unit_of_measurement = description.unit_convert
|
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
|
@property
|
||||||
def native_value(self) -> float | None:
|
def native_value(self) -> float | None:
|
||||||
|
@ -219,11 +223,6 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
|
||||||
return round(value)
|
return round(value)
|
||||||
return 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
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return if state is available."""
|
"""Return if state is available."""
|
||||||
|
@ -235,13 +234,3 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
|
||||||
else:
|
else:
|
||||||
last_success_time = False
|
last_success_time = False
|
||||||
return self.coordinator.last_update_success or last_success_time
|
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)
|
|
||||||
|
|
|
@ -32,7 +32,6 @@ from homeassistant.const import (
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
from homeassistant.util.unit_conversion import SpeedConverter, TemperatureConverter
|
from homeassistant.util.unit_conversion import SpeedConverter, TemperatureConverter
|
||||||
|
@ -121,6 +120,10 @@ class NWSWeather(CoordinatorWeatherEntity):
|
||||||
_attr_supported_features = (
|
_attr_supported_features = (
|
||||||
WeatherEntityFeature.FORECAST_HOURLY | WeatherEntityFeature.FORECAST_TWICE_DAILY
|
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__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
@ -137,8 +140,8 @@ class NWSWeather(CoordinatorWeatherEntity):
|
||||||
twice_daily_forecast_valid=FORECAST_VALID_TIME,
|
twice_daily_forecast_valid=FORECAST_VALID_TIME,
|
||||||
)
|
)
|
||||||
self.nws = nws_data.api
|
self.nws = nws_data.api
|
||||||
self.latitude = entry_data[CONF_LATITUDE]
|
latitude = entry_data[CONF_LATITUDE]
|
||||||
self.longitude = entry_data[CONF_LONGITUDE]
|
longitude = entry_data[CONF_LONGITUDE]
|
||||||
if mode == DAYNIGHT:
|
if mode == DAYNIGHT:
|
||||||
self.coordinator_forecast_legacy = nws_data.coordinator_forecast
|
self.coordinator_forecast_legacy = nws_data.coordinator_forecast
|
||||||
else:
|
else:
|
||||||
|
@ -153,6 +156,8 @@ class NWSWeather(CoordinatorWeatherEntity):
|
||||||
self._forecast_twice_daily: list[dict[str, Any]] | None = None
|
self._forecast_twice_daily: list[dict[str, Any]] | None = None
|
||||||
|
|
||||||
self._attr_unique_id = _calculate_unique_id(entry_data, mode)
|
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:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Set up a listener and load data."""
|
"""Set up a listener and load data."""
|
||||||
|
@ -193,11 +198,6 @@ class NWSWeather(CoordinatorWeatherEntity):
|
||||||
self._forecast_legacy = self.nws.forecast_hourly
|
self._forecast_legacy = self.nws.forecast_hourly
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str:
|
|
||||||
"""Return the name of the station."""
|
|
||||||
return f"{self.station} {self.mode.title()}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_temperature(self) -> float | None:
|
def native_temperature(self) -> float | None:
|
||||||
"""Return the current temperature."""
|
"""Return the current temperature."""
|
||||||
|
@ -205,11 +205,6 @@ class NWSWeather(CoordinatorWeatherEntity):
|
||||||
return self.observation.get("temperature")
|
return self.observation.get("temperature")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
|
||||||
def native_temperature_unit(self) -> str:
|
|
||||||
"""Return the current temperature unit."""
|
|
||||||
return UnitOfTemperature.CELSIUS
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_pressure(self) -> int | None:
|
def native_pressure(self) -> int | None:
|
||||||
"""Return the current pressure."""
|
"""Return the current pressure."""
|
||||||
|
@ -217,11 +212,6 @@ class NWSWeather(CoordinatorWeatherEntity):
|
||||||
return self.observation.get("seaLevelPressure")
|
return self.observation.get("seaLevelPressure")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
|
||||||
def native_pressure_unit(self) -> str:
|
|
||||||
"""Return the current pressure unit."""
|
|
||||||
return UnitOfPressure.PA
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def humidity(self) -> float | None:
|
def humidity(self) -> float | None:
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
|
@ -236,11 +226,6 @@ class NWSWeather(CoordinatorWeatherEntity):
|
||||||
return self.observation.get("windSpeed")
|
return self.observation.get("windSpeed")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
|
||||||
def native_wind_speed_unit(self) -> str:
|
|
||||||
"""Return the current windspeed."""
|
|
||||||
return UnitOfSpeed.KILOMETERS_PER_HOUR
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def wind_bearing(self) -> int | None:
|
def wind_bearing(self) -> int | None:
|
||||||
"""Return the current wind bearing (degrees)."""
|
"""Return the current wind bearing (degrees)."""
|
||||||
|
@ -267,11 +252,6 @@ class NWSWeather(CoordinatorWeatherEntity):
|
||||||
return self.observation.get("visibility")
|
return self.observation.get("visibility")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
|
||||||
def native_visibility_unit(self) -> str:
|
|
||||||
"""Return visibility unit."""
|
|
||||||
return UnitOfLength.METERS
|
|
||||||
|
|
||||||
def _forecast(
|
def _forecast(
|
||||||
self, nws_forecast: list[dict[str, Any]] | None, mode: str
|
self, nws_forecast: list[dict[str, Any]] | None, mode: str
|
||||||
) -> list[Forecast] | None:
|
) -> list[Forecast] | None:
|
||||||
|
@ -377,8 +357,3 @@ class NWSWeather(CoordinatorWeatherEntity):
|
||||||
def entity_registry_enabled_default(self) -> bool:
|
def entity_registry_enabled_default(self) -> bool:
|
||||||
"""Return if the entity should be enabled when first added to the entity registry."""
|
"""Return if the entity should be enabled when first added to the entity registry."""
|
||||||
return self.mode == DAYNIGHT
|
return self.mode == DAYNIGHT
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo:
|
|
||||||
"""Return device info."""
|
|
||||||
return device_info(self.latitude, self.longitude)
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue