From ff77bdbeb31d75d36b55edcd540b8e6ba3e3bec1 Mon Sep 17 00:00:00 2001 From: Allister Maguire Date: Fri, 27 Jan 2023 13:30:35 +1300 Subject: [PATCH] Pws station in entity name and feels like temp (#9) * PWS Station in Enity Name * Add Feels Like Temp Co-authored-by: allister --- custom_components/wundergroundpws/sensor.py | 20 ++++++++++++++++--- custom_components/wundergroundpws/weather.py | 3 +++ .../wupws_translations/en.json | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/custom_components/wundergroundpws/sensor.py b/custom_components/wundergroundpws/sensor.py index 2d63fb8..ed8f7c2 100644 --- a/custom_components/wundergroundpws/sensor.py +++ b/custom_components/wundergroundpws/sensor.py @@ -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={ @@ -238,6 +242,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), @@ -386,6 +393,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)]) @@ -428,8 +441,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") diff --git a/custom_components/wundergroundpws/weather.py b/custom_components/wundergroundpws/weather.py index 97a21bd..1353003 100644 --- a/custom_components/wundergroundpws/weather.py +++ b/custom_components/wundergroundpws/weather.py @@ -46,6 +46,7 @@ ATTR_FORECAST_TIME, ATTR_FORECAST_WIND_BEARING, ATTR_FORECAST_WIND_SPEED, + ENTITY_ID_FORMAT, WeatherEntity, Forecast, ) @@ -95,6 +96,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: diff --git a/custom_components/wundergroundpws/wupws_translations/en.json b/custom_components/wundergroundpws/wupws_translations/en.json index 5c74f52..eb283d3 100644 --- a/custom_components/wundergroundpws/wupws_translations/en.json +++ b/custom_components/wundergroundpws/wupws_translations/en.json @@ -1,6 +1,7 @@ { "dewpt": "Dewpoint", "elev": "Elevation", + "feelsLike": "Feels Like", "heatIndex": "Heat index", "humidity": "Relative Humidity", "neighborhood": "Neighborhood",