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 <bachya1208@gmail.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
pull/115059/head
William Easton 2024-04-06 16:39:43 -05:00 committed by GitHub
parent 78bb21138c
commit ed451cab3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View File

@ -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

View File

@ -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"
},