2021-04-01 16:50:37 +00:00
|
|
|
"""Sensors for National Weather Service (NWS)."""
|
|
|
|
from homeassistant.components.sensor import SensorEntity
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
LENGTH_KILOMETERS,
|
|
|
|
LENGTH_METERS,
|
|
|
|
LENGTH_MILES,
|
|
|
|
PERCENTAGE,
|
|
|
|
PRESSURE_INHG,
|
|
|
|
PRESSURE_PA,
|
|
|
|
SPEED_MILES_PER_HOUR,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2021-07-22 05:24:07 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-01 16:50:37 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
from homeassistant.util.distance import convert as convert_distance
|
|
|
|
from homeassistant.util.dt import utcnow
|
|
|
|
from homeassistant.util.pressure import convert as convert_pressure
|
|
|
|
|
|
|
|
from . import base_unique_id
|
|
|
|
from .const import (
|
|
|
|
ATTRIBUTION,
|
|
|
|
CONF_STATION,
|
|
|
|
COORDINATOR_OBSERVATION,
|
|
|
|
DOMAIN,
|
|
|
|
NWS_DATA,
|
|
|
|
OBSERVATION_VALID_TIME,
|
|
|
|
SENSOR_TYPES,
|
2021-07-26 23:46:01 +00:00
|
|
|
NWSSensorEntityDescription,
|
2021-04-01 16:50:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""Set up the NWS weather platform."""
|
|
|
|
hass_data = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
station = entry.data[CONF_STATION]
|
|
|
|
|
2021-07-26 23:46:01 +00:00
|
|
|
async_add_entities(
|
|
|
|
NWSSensor(
|
|
|
|
hass=hass,
|
|
|
|
entry_data=entry.data,
|
|
|
|
hass_data=hass_data,
|
|
|
|
description=description,
|
|
|
|
station=station,
|
2021-04-01 16:50:37 +00:00
|
|
|
)
|
2021-07-26 23:46:01 +00:00
|
|
|
for description in SENSOR_TYPES
|
|
|
|
)
|
2021-04-01 16:50:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NWSSensor(CoordinatorEntity, SensorEntity):
|
|
|
|
"""An NWS Sensor Entity."""
|
|
|
|
|
2021-07-26 23:46:01 +00:00
|
|
|
entity_description: NWSSensorEntityDescription
|
2021-09-04 20:16:01 +00:00
|
|
|
_attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
2021-07-26 23:46:01 +00:00
|
|
|
|
2021-04-01 16:50:37 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-07-22 05:24:07 +00:00
|
|
|
hass: HomeAssistant,
|
2021-04-01 16:50:37 +00:00
|
|
|
entry_data,
|
|
|
|
hass_data,
|
2021-07-26 23:46:01 +00:00
|
|
|
description: NWSSensorEntityDescription,
|
2021-04-01 16:50:37 +00:00
|
|
|
station,
|
|
|
|
):
|
|
|
|
"""Initialise the platform with a data instance."""
|
|
|
|
super().__init__(hass_data[COORDINATOR_OBSERVATION])
|
|
|
|
self._nws = hass_data[NWS_DATA]
|
|
|
|
self._latitude = entry_data[CONF_LATITUDE]
|
|
|
|
self._longitude = entry_data[CONF_LONGITUDE]
|
2021-07-26 23:46:01 +00:00
|
|
|
self.entity_description = description
|
2021-07-22 05:24:07 +00:00
|
|
|
|
2021-07-26 23:46:01 +00:00
|
|
|
self._attr_name = f"{station} {description.name}"
|
|
|
|
if not hass.config.units.is_metric:
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_unit_of_measurement = description.unit_convert
|
2021-04-01 16:50:37 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-12 11:26:17 +00:00
|
|
|
def native_value(self):
|
2021-04-01 16:50:37 +00:00
|
|
|
"""Return the state."""
|
2021-07-26 23:46:01 +00:00
|
|
|
value = self._nws.observation.get(self.entity_description.key)
|
2021-04-01 16:50:37 +00:00
|
|
|
if value is None:
|
|
|
|
return None
|
2021-07-26 23:46:01 +00:00
|
|
|
# Set alias to unit property -> prevent unnecessary hasattr calls
|
2021-08-12 11:26:17 +00:00
|
|
|
unit_of_measurement = self.native_unit_of_measurement
|
2021-07-26 23:46:01 +00:00
|
|
|
if unit_of_measurement == SPEED_MILES_PER_HOUR:
|
2021-04-01 16:50:37 +00:00
|
|
|
return round(convert_distance(value, LENGTH_KILOMETERS, LENGTH_MILES))
|
2021-07-26 23:46:01 +00:00
|
|
|
if unit_of_measurement == LENGTH_MILES:
|
2021-04-01 16:50:37 +00:00
|
|
|
return round(convert_distance(value, LENGTH_METERS, LENGTH_MILES))
|
2021-07-26 23:46:01 +00:00
|
|
|
if unit_of_measurement == PRESSURE_INHG:
|
2021-04-01 16:50:37 +00:00
|
|
|
return round(convert_pressure(value, PRESSURE_PA, PRESSURE_INHG), 2)
|
2021-07-26 23:46:01 +00:00
|
|
|
if unit_of_measurement == TEMP_CELSIUS:
|
2021-04-01 16:50:37 +00:00
|
|
|
return round(value, 1)
|
2021-07-26 23:46:01 +00:00
|
|
|
if unit_of_measurement == PERCENTAGE:
|
2021-04-01 16:50:37 +00:00
|
|
|
return round(value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
2021-07-26 23:46:01 +00:00
|
|
|
return f"{base_unique_id(self._latitude, self._longitude)}_{self.entity_description.key}"
|
2021-04-01 16:50:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return if state is available."""
|
|
|
|
if self.coordinator.last_update_success_time:
|
|
|
|
last_success_time = (
|
|
|
|
utcnow() - self.coordinator.last_update_success_time
|
|
|
|
< OBSERVATION_VALID_TIME
|
|
|
|
)
|
|
|
|
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
|