From 759caf09368956fb9a71cf0e18dfa610ee592a13 Mon Sep 17 00:00:00 2001 From: Chris Straffon Date: Tue, 4 Jul 2023 21:42:18 +0100 Subject: [PATCH] #16 - First pass at last updated time value --- .../grott/sensors/sensors_mqtt.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/custom_components/grott/sensors/sensors_mqtt.py b/custom_components/grott/sensors/sensors_mqtt.py index 3587599..f04b88d 100644 --- a/custom_components/grott/sensors/sensors_mqtt.py +++ b/custom_components/grott/sensors/sensors_mqtt.py @@ -1,6 +1,8 @@ """Sensor descriptions for Growatt systems - Raw MQTT values.""" - from __future__ import annotations + +import datetime + from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass from homeassistant.const import ( PERCENTAGE, @@ -13,6 +15,7 @@ UnitOfTime, ) from homeassistant.helpers.entity import DeviceInfo, EntityCategory +from homeassistant.util import dt from ..const import ( BATTERY_TYPES @@ -24,6 +27,11 @@ def battery_type_lookup(mqtt_data): batt_type = 2 return BATTERY_TYPES[int(batt_type)] +def datetime_formatter(mqtt_data): + last_updated_time_string = mqtt_data['time'] + last_updated_date_time = dt.parse_datetime(last_updated_time_string) + return datetime.datetime.combine(last_updated_date_time.date(), last_updated_date_time.time(), dt.DEFAULT_TIME_ZONE) + SENSORS_LABEL="raw_mqtt_sensors" SENSORS = [ @@ -1074,6 +1082,18 @@ def battery_type_lookup(mqtt_data): "unique_name": "mqtt_098", }, + { + "name": "Last data update", + "device_class": SensorDeviceClass.TIMESTAMP, + "unit_of_measurement": None, + "state_class": None, + "entity_category": EntityCategory.DIAGNOSTIC, + "icon": "mdi:information-outline", + "func": datetime_formatter, + "unique_name": "mqtt_099", + }, + + #The following entries are out-of-order in the above list, be sure to check for the latest value for unique_name from both lists #mqtt_066 - PV3 Voltage #mqtt_067 - PV3 Current