diff --git a/custom_components/sws12500/const.py b/custom_components/sws12500/const.py index 5551e8c..755b885 100644 --- a/custom_components/sws12500/const.py +++ b/custom_components/sws12500/const.py @@ -70,6 +70,7 @@ UV: Final = "uv" CH2_TEMP: Final = "ch2_temp" CH2_HUMIDITY: Final = "ch2_humidity" HEAT_INDEX: Final = "heat_index" +CHILL_INDEX: Final = "chill_index" REMAP_ITEMS: dict = { diff --git a/custom_components/sws12500/sensor.py b/custom_components/sws12500/sensor.py index ebf1750..985353a 100644 --- a/custom_components/sws12500/sensor.py +++ b/custom_components/sws12500/sensor.py @@ -1,4 +1,5 @@ """Sensors definition for SWS12500.""" + from collections.abc import Callable from dataclasses import dataclass import logging @@ -34,6 +35,7 @@ from .const import ( BARO_PRESSURE, CH2_HUMIDITY, CH2_TEMP, + CHILL_INDEX, DAILY_RAIN, DEW_POINT, DOMAIN, @@ -52,7 +54,7 @@ from .const import ( WIND_SPEED, UnitOfDir, ) -from .utils import wind_dir_to_text, heat_index +from .utils import heat_index, wind_dir_to_text, chill_index _LOGGER = logging.getLogger(__name__) @@ -226,6 +228,17 @@ SENSOR_TYPES: tuple[WeatherSensorEntityDescription, ...] = ( translation_key=HEAT_INDEX, value_fn=lambda data: cast(int, data), ), + WeatherSensorEntityDescription( + key=CHILL_INDEX, + native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT, + state_class=SensorStateClass.MEASUREMENT, + device_class=SensorDeviceClass.TEMPERATURE, + suggested_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=2, + icon="mdi:weather-sunny", + translation_key=CHILL_INDEX, + value_fn=lambda data: cast(int, data), + ), ) @@ -245,8 +258,11 @@ async def async_setup_entry( if sensors_to_load := config_entry.options.get(SENSORS_TO_LOAD): if WIND_DIR in sensors_to_load: sensors_to_load.append(WIND_AZIMUT) - if (WIND_SPEED in sensors_to_load) and (OUTSIDE_TEMP in sensors_to_load): + if (OUTSIDE_HUMIDITY in sensors_to_load) and (OUTSIDE_TEMP in sensors_to_load): sensors_to_load.append(HEAT_INDEX) + + if (WIND_SPEED in sensors_to_load) and (OUTSIDE_TEMP in sensors_to_load): + sensors_to_load.append(CHILL_INDEX) sensors = [ WeatherSensor(hass, description, coordinator) for description in SENSOR_TYPES @@ -309,6 +325,9 @@ class WeatherSensor( if self.coordinator.data and (HEAT_INDEX in self.entity_description.key): return self.entity_description.value_fn(heat_index(self.coordinator.data)) + if self.coordinator.data and (CHILL_INDEX in self.entity_description.key): + return self.entity_description.value_fn(chill_index(self.coordinator.data)) + return self.entity_description.value_fn(self._data) @property diff --git a/custom_components/sws12500/translations/cs.json b/custom_components/sws12500/translations/cs.json index f9ba0ad..bd127c7 100644 --- a/custom_components/sws12500/translations/cs.json +++ b/custom_components/sws12500/translations/cs.json @@ -77,6 +77,7 @@ "ch2_temp": { "name": "Teplota senzoru 2" }, "ch2_humidity": { "name": "Vlhkost sensoru 2" }, "heat_index": { "name": "Tepelný index" }, + "chill_index": { "name": "Pocitová teplota" }, "wind_azimut": { "name": "Azimut", "state": { diff --git a/custom_components/sws12500/translations/en.json b/custom_components/sws12500/translations/en.json index 362b175..f701d48 100644 --- a/custom_components/sws12500/translations/en.json +++ b/custom_components/sws12500/translations/en.json @@ -78,6 +78,7 @@ "ch2_temp": { "name": "Channel 2 temperature" }, "ch2_humidity": { "name": "Channel 2 humidity" }, "heat_index": { "name": "Apparent temperature" }, + "chill_index": { "name": "Wind chill" }, "wind_azimut": { "name": "Bearing", "state": { diff --git a/custom_components/sws12500/utils.py b/custom_components/sws12500/utils.py index 5d927fd..deee0b4 100644 --- a/custom_components/sws12500/utils.py +++ b/custom_components/sws12500/utils.py @@ -19,6 +19,7 @@ from .const import ( UnitOfDir, OUTSIDE_TEMP, OUTSIDE_HUMIDITY, + WIND_SPEED, ) _LOGGER = logging.getLogger(__name__) @@ -190,6 +191,22 @@ def heat_index(data: Any) -> UnitOfTemperature: if rh > 80 and (temp in np.arange(80, 87, 0.1)): adjustment = ((rh - 85) / 10) * ((87 - temp) / 5) - return full_index + adjustment if adjustment else full_index + return round((full_index + adjustment if adjustment else full_index), 2) return simple + + +def chill_index(data: Any) -> UnitOfTemperature: + """Calculate wind chill index.""" + + temp = float(data[OUTSIDE_TEMP]) + wind = float(data[WIND_SPEED]) + + return round( + ( + (35.7 + (0.6215 * temp)) + - (35.75 * (wind**0.16)) + + (0.4275 * (temp * (wind**0.16))) + ), + 2, + )