Skip to content

Commit

Permalink
Customisable name and feelsLike temp (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: allister <[email protected]>
  • Loading branch information
allistermaguire and allistermaguire authored Nov 21, 2022
1 parent 1050516 commit 7886c03
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 27 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ Description of terms and variables
description: "You must enter a Personal Weather Station ID. The station id will be used to display current weather conditions."
required: true
type: string
name:
description: "Personalized name for Weather Station and/or forecast. If not specified will default to Weather Station ID or Coordinates."
required: false
type: string
numeric_precision:
description: Required - Show PWS data as integer or decimal
required: true - Value of 'none' or 'decimal'
type: string
description: Required - Show PWS data as integer or decimal
required: true - Value of 'none' or 'decimal'
type: string
lang:
description: Specify the language that the API returns. The current list of all Wunderground language codes is available at https://docs.google.com/document/d/13HTLgJDpsb39deFzk_YCQ5GoGoZCO_cRYzIxbwvgJLI/edit#). If not specified, it defaults to English (en-US).
required: false
Expand Down Expand Up @@ -118,7 +122,9 @@ Description of terms and variables
heatIndex:
description: Heat index (combined effects of the temperature and humidity of the air)
windChill:
description: Wind Chill (combined effects of the temperature and wind)
description: Wind Chill (combined effects of the temperature and wind)
feelsLike:
description: Feels Like Temperature (mixture of the Wind Chill Factor and the Heat Index)
elev:
description: Elevation
precipTotal:
Expand Down Expand Up @@ -174,12 +180,7 @@ in `_1n_` part of the sensor name. Valid values are from `1` to `5`.
- weather_4n
```
<p class='note warning'>
Note: While the platform is called “wundergroundpws” the sensors will show up in Home Assistant as “WUPWS” (eg: sensor.wupws_weather_1d).
Note: While the platform is called “wundergroundpws” the sensors will show up in Home Assistant as “WUPWS” (eg: sensor.wupws_[name|stationid|coordinates]_weather_1d).
</p>

Note that the Weather Underground sensor is added to the entity_registry, so second and subsequent Personal Weather Station ID (pws_id) will have their monitored conditions suffixed with an index number e.g.

```yaml
- sensor.wupws_weather_1d_metric_2
```
Additional details about the API are available [here](https://docs.google.com/document/d/1eKCnKXI9xnoMGRRzOL1xPCBihNV2rOet08qpE_gArAY/edit).
57 changes: 50 additions & 7 deletions custom_components/wundergroundpws/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

CONF_ATTRIBUTION = "Data provided by the WUnderground weather service"
CONF_PWS_ID = 'pws_id'
CONF_NAME = 'name'
CONF_NUMERIC_PRECISION = 'numeric_precision'
CONF_LANG = 'lang'

Expand All @@ -52,6 +53,11 @@
RATE = 5
PERCENTAGEUNIT = 6

FIELD_CONDITION_FEELSLIKE = "feelsLike"
FIELD_CONDITION_HEATINDEX = "heatIndex"
FIELD_CONDITION_TEMP = "temp"
FIELD_CONDITION_WINDCHILL = "windChill"


# Helper classes for declaring sensor configurations

Expand Down Expand Up @@ -218,10 +224,12 @@ def __init__(self, friendly_name, period, field,
'Elevation', 'elev', 'mdi:elevation-rise', ALTITUDEUNIT),
'dewpt': WUCurrentConditionsSensorConfig(
'Dewpoint', 'dewpt', 'mdi:water', TEMPUNIT),
'heatIndex': WUCurrentConditionsSensorConfig(
'Heat index', 'heatIndex', "mdi:thermometer", TEMPUNIT),
'windChill': WUCurrentConditionsSensorConfig(
'Wind chill', 'windChill', "mdi:thermometer", TEMPUNIT),
FIELD_CONDITION_HEATINDEX: WUCurrentConditionsSensorConfig(
"Heat index", FIELD_CONDITION_HEATINDEX, "mdi:thermometer", TEMPUNIT
),
FIELD_CONDITION_WINDCHILL: WUCurrentConditionsSensorConfig(
"Wind chill", FIELD_CONDITION_WINDCHILL, "mdi:thermometer", TEMPUNIT
),
'precipRate': WUCurrentConditionsSensorConfig(
'Precipitation Rate', 'precipRate', "mdi:umbrella", RATE),
'precipTotal': WUCurrentConditionsSensorConfig(
Expand All @@ -236,6 +244,9 @@ def __init__(self, friendly_name, period, field,
'Wind Gust', 'windGust', "mdi:weather-windy", SPEEDUNIT),
'windSpeed': WUCurrentConditionsSensorConfig(
'Wind Speed', 'windSpeed', "mdi:weather-windy", SPEEDUNIT),
FIELD_CONDITION_FEELSLIKE: WUCurrentConditionsSensorConfig(
"Feels Like", FIELD_CONDITION_FEELSLIKE, "mdi:thermometer", TEMPUNIT
),
# forecast
'weather_1d': WUDailyTextForecastSensorConfig(0),
'weather_1n': WUDailyTextForecastSensorConfig(1),
Expand Down Expand Up @@ -374,6 +385,7 @@ def wind_direction_to_friendly_name(argument):
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_PWS_ID): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_NUMERIC_PRECISION): cv.string,
vol.Optional(CONF_LANG, default=DEFAULT_LANG): vol.All(vol.In(LANG_CODES)),
vol.Inclusive(CONF_LATITUDE, 'coordinates',
Expand All @@ -391,6 +403,7 @@ async def async_setup_platform(hass: HomeAssistantType, config: ConfigType,
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
pws_id = config.get(CONF_PWS_ID)
name_prefix = config.get(CONF_NAME)
numeric_precision = config.get(CONF_NUMERIC_PRECISION)

if hass.config.units is METRIC_SYSTEM:
Expand All @@ -409,6 +422,11 @@ async def async_setup_platform(hass: HomeAssistantType, config: ConfigType,
else:
# Manually specified weather station, use that for unique_id
unique_id_base = pws_id

if name_prefix is not None:
# If name specified in config, use it for unique_id
unique_id_base = name_prefix

sensors = []
for variable in config[CONF_MONITORED_CONDITIONS]:
sensors.append(WUndergroundSensor(hass, rest, variable,
Expand Down Expand Up @@ -439,9 +457,13 @@ 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.entity_id = sensor.ENTITY_ID_FORMAT.format(
f"wupws_{unique_id_base.lower()}_{condition}"
)
self._unique_id = f"{unique_id_base},{condition}"
self._device_class = self._cfg_expand("device_class")
friendly_name = self._cfg_expand("friendly_name")
self._name = f"{unique_id_base} {friendly_name}"

def _cfg_expand(self, what, default=None):
"""Parse and return sensor data."""
Expand Down Expand Up @@ -477,7 +499,7 @@ def _update_attrs(self):
@property
def name(self):
"""Return the name of the sensor."""
return self._cfg_expand("friendly_name")
return self._name

@property
def state(self):
Expand Down Expand Up @@ -589,6 +611,27 @@ async def async_update(self):

if result_current is None:
raise ValueError('NO CURRENT RESULT')
else:
temp = result_current["observations"][0][self.unit_system][
FIELD_CONDITION_TEMP
]
windChill = result_current["observations"][0][self.unit_system][
FIELD_CONDITION_WINDCHILL
]
heatIndex = result_current["observations"][0][self.unit_system][
FIELD_CONDITION_HEATINDEX
]

feelsLike = temp
if all(item is not None for item in [temp, windChill, heatIndex]):
# Calculate feelsLike temperature and add to results based on
# https://www.wunderground.com/maps/temperature/feels-like
feelsLike = windChill if windChill < temp else heatIndex

result_current["observations"][0][self.unit_system][
FIELD_CONDITION_FEELSLIKE
] = feelsLike

with async_timeout.timeout(10):
response = await self._session.get(self._build_url(_RESOURCEFORECAST), headers=headers)
result_forecast = await response.json()
Expand Down
21 changes: 11 additions & 10 deletions custom_components/wundergroundpws/sensor.wundergroundpws.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ Description of terms and variables
description: "You must enter a Personal Weather Station ID. The station id will be used to display current weather conditions."
required: true
type: string
name:
description: "Personalized name for Weather Station and/or forecast. If not specified will default to Weather Station ID or Coordinates."
required: false
type: string
numeric_precision:
description: Required - Show PWS data as integer or decimal
required: true - Value of 'none' or 'decimal'
type: string
description: Required - Show PWS data as integer or decimal
required: true - Value of 'none' or 'decimal'
type: string
lang:
description: Specify the language that the API returns. The current list of all Wunderground language codes is available at https://docs.google.com/document/d/13HTLgJDpsb39deFzk_YCQ5GoGoZCO_cRYzIxbwvgJLI/edit#). If not specified, it defaults to English (en-US).
required: false
Expand Down Expand Up @@ -96,7 +100,9 @@ Description of terms and variables
heatIndex:
description: Heat index (combined effects of the temperature and humidity of the air)
windChill:
description: Wind Chill (combined effects of the temperature and wind)
description: Wind Chill (combined effects of the temperature and wind)
feelsLike:
description: Feels Like Temperature (mixture of the Wind Chill Factor and the Heat Index)
elev:
description: Elevation
precipTotal:
Expand Down Expand Up @@ -152,12 +158,7 @@ sensor:
- weather_4n
```
<p class='note warning'>
Note: While the platform is called “wundergroundpws” the sensors will show up in Home Assistant as “WUPWS” (eg: sensor.wupws_weather_1d).
Note: While the platform is called “wundergroundpws” the sensors will show up in Home Assistant as “WUPWS” (eg: sensor.wupws_[name|stationid|coordinates]_weather_1d.
</p>

Note that the Weather Underground sensor is added to the entity_registry, so second and subsequent Personal Weather Station ID (pws_id) will have their monitored conditions suffixed with an index number e.g.

```yaml
- sensor.wupws_weather_1d_metric_2
```
Additional details about the API are available [here](https://docs.google.com/document/d/1eKCnKXI9xnoMGRRzOL1xPCBihNV2rOet08qpE_gArAY/edit).

0 comments on commit 7886c03

Please sign in to comment.