Skip to content

Commit

Permalink
Pws station in entity name and feels like temp (#8)
Browse files Browse the repository at this point in the history
* PWS Station in Enity Name

* Add Feels Like Temp

* Move calculate_feels_like_temp call to WUCurrentConditionsSensorConfig helper

Co-authored-by: allister <[email protected]>
  • Loading branch information
allistermaguire and allistermaguire authored Jan 11, 2023
1 parent fa4451b commit 3bab773
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
20 changes: 17 additions & 3 deletions custom_components/wundergroundpws/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ def __init__(self, friendly_name, field, icon="mdi:gauge",
super().__init__(
friendly_name,
"conditions",
value=lambda wu: wu.data['observations'][0][wu.unit_system][field],
value=lambda wu: wu.data['observations'][0][wu.unit_system][field]
if (field != 'feelsLike') else calculate_feels_like_temp(
float(wu.data['observations'][0][wu.unit_system]['heatIndex'] or 0),
float(wu.data['observations'][0][wu.unit_system]['windChill'] or 0),
float(wu.data['observations'][0][wu.unit_system]['temp'] or 0)),
icon=icon,
unit_of_measurement=lambda wu: wu.units_of_measurement[unit_of_measurement],
device_state_attributes={
Expand Down Expand Up @@ -234,6 +238,9 @@ def __init__(self, friendly_name, period, field,
'temp': WUCurrentConditionsSensorConfig(
'Temperature', 'temp', "mdi:thermometer", TEMPUNIT,
device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT),
'feelsLike': WUCurrentConditionsSensorConfig(
'Feels Like', 'feelsLike', "mdi:thermometer", TEMPUNIT,
device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT),
'windGust': WUCurrentConditionsSensorConfig(
'Wind Gust', 'windGust', "mdi:weather-windy", SPEEDUNIT, device_class=SensorDeviceClass.WIND_SPEED,
state_class=SensorStateClass.MEASUREMENT),
Expand Down Expand Up @@ -382,6 +389,12 @@ def wind_direction_to_friendly_name(argument):
return ""


def calculate_feels_like_temp(heatIndex, windChill, temp):
# Return Feels Like temperature based on
# https://www.wunderground.com/maps/temperature/feels-like
return windChill if windChill < temp else heatIndex


PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_MONITORED_CONDITIONS):
vol.All(cv.ensure_list, vol.Length(min=1), [vol.In(SENSOR_TYPES)])
Expand Down Expand Up @@ -424,8 +437,9 @@ def __init__(self, hass: HomeAssistantType, rest, condition,
self.rest.request_feature(SENSOR_TYPES[condition].feature)
# This is only the suggested entity id, it might get changed by
# the entity registry later.
self.entity_id = sensor.ENTITY_ID_FORMAT.format('wupws_' + condition)
self._unique_id = "{},{}".format(unique_id_base, condition)
self._unique_id = f'{unique_id_base}.{condition}'
self.entity_id = sensor.ENTITY_ID_FORMAT.format(
f'wupws.{self.unique_id}')
self._device_class = self._cfg_expand("device_class")
self._state_class = self._cfg_expand("state_class")

Expand Down
1 change: 1 addition & 0 deletions custom_components/wundergroundpws/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dewpt": "Dewpoint",
"elev": "Elevation",
"feelsLike": "Feels Like",
"heatIndex": "Heat index",
"humidity": "Relative Humidity",
"neighborhood": "Neighborhood",
Expand Down
3 changes: 3 additions & 0 deletions custom_components/wundergroundpws/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
ATTR_FORECAST_TIME,
ATTR_FORECAST_WIND_BEARING,
ATTR_FORECAST_WIND_SPEED,
ENTITY_ID_FORMAT,
WeatherEntity,
Forecast,
)
Expand Down Expand Up @@ -92,6 +93,8 @@ def __init__(
name=NAME,
)
self._rest = wunderground_data
self.entity_id = ENTITY_ID_FORMAT.format(
f'wupws.{self.unique_id}')

@property
def native_temperature(self) -> float:
Expand Down

0 comments on commit 3bab773

Please sign in to comment.