From ed451cab3b76ab8ae76e37f1d74e13ba42c0ce6e Mon Sep 17 00:00:00 2001 From: William Easton Date: Sat, 6 Apr 2024 16:39:43 -0500 Subject: [PATCH] Update Ambient Weather to include Lightning Strike Time and Distance (#114255) * Update sensor.py to include Lightning Distance * Update strings.json to include Lightning Strikes distance * Update homeassistant/components/ambient_station/sensor.py * Update homeassistant/components/ambient_station/strings.json * Update device class lightning distance * Also add Last Lightning strike datetime --------- Co-authored-by: Aaron Bach Co-authored-by: J. Nick Koston --- .../components/ambient_station/sensor.py | 21 ++++++++++++++++++- .../components/ambient_station/strings.json | 6 ++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/ambient_station/sensor.py b/homeassistant/components/ambient_station/sensor.py index db729197a59..229ebee4fbf 100644 --- a/homeassistant/components/ambient_station/sensor.py +++ b/homeassistant/components/ambient_station/sensor.py @@ -2,7 +2,7 @@ from __future__ import annotations -from datetime import datetime +from datetime import UTC, datetime from homeassistant.components.sensor import ( SensorDeviceClass, @@ -19,6 +19,7 @@ from homeassistant.const import ( LIGHT_LUX, PERCENTAGE, UnitOfIrradiance, + UnitOfLength, UnitOfPrecipitationDepth, UnitOfPressure, UnitOfSpeed, @@ -61,6 +62,8 @@ TYPE_HUMIDITYIN = "humidityin" TYPE_LASTRAIN = "lastRain" TYPE_LIGHTNING_PER_DAY = "lightning_day" TYPE_LIGHTNING_PER_HOUR = "lightning_hour" +TYPE_LASTLIGHTNING_DISTANCE = "lightning_distance" +TYPE_LASTLIGHTNING = "lightning_time" TYPE_MAXDAILYGUST = "maxdailygust" TYPE_MONTHLYRAININ = "monthlyrainin" TYPE_PM25 = "pm25" @@ -296,6 +299,18 @@ SENSOR_DESCRIPTIONS = ( native_unit_of_measurement="strikes", state_class=SensorStateClass.TOTAL, ), + SensorEntityDescription( + key=TYPE_LASTLIGHTNING, + translation_key="last_lightning_strike", + device_class=SensorDeviceClass.TIMESTAMP, + ), + SensorEntityDescription( + key=TYPE_LASTLIGHTNING_DISTANCE, + translation_key="last_lightning_strike_distance", + native_unit_of_measurement=UnitOfLength.MILES, + device_class=SensorDeviceClass.DISTANCE, + state_class=SensorStateClass.MEASUREMENT, + ), SensorEntityDescription( key=TYPE_MAXDAILYGUST, translation_key="max_gust", @@ -685,5 +700,9 @@ class AmbientWeatherSensor(AmbientWeatherEntity, SensorEntity): raw = self._ambient.stations[self._mac_address][ATTR_LAST_DATA][key] if key == TYPE_LASTRAIN: self._attr_native_value = datetime.strptime(raw, "%Y-%m-%dT%H:%M:%S.%f%z") + elif key == TYPE_LASTLIGHTNING: + self._attr_native_value = datetime.fromtimestamp( + raw / 1000, tz=UTC + ) # Ambient uses millisecond epoch else: self._attr_native_value = raw diff --git a/homeassistant/components/ambient_station/strings.json b/homeassistant/components/ambient_station/strings.json index 02bceda500f..25006dce0e9 100644 --- a/homeassistant/components/ambient_station/strings.json +++ b/homeassistant/components/ambient_station/strings.json @@ -219,6 +219,12 @@ "last_rain": { "name": "Last rain" }, + "last_lightning_strike": { + "name": "Last Lightning strike" + }, + "last_lightning_strike_distance": { + "name": "Last Lightning strike distance" + }, "lightning_strikes_per_day": { "name": "Lightning strikes per day" },