From 99ee5034d694e86c3c23affbc3bef7c80bb162c0 Mon Sep 17 00:00:00 2001 From: Piotr Machowski Date: Mon, 16 Jan 2023 20:10:17 +0100 Subject: [PATCH] Add diagnostic entities --- custom_components/gne_pv_monitoring/const.py | 42 +++++++++++++++++-- custom_components/gne_pv_monitoring/entity.py | 11 ----- custom_components/gne_pv_monitoring/sensor.py | 8 ++++ 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/custom_components/gne_pv_monitoring/const.py b/custom_components/gne_pv_monitoring/const.py index 5f6827a..9738fa8 100644 --- a/custom_components/gne_pv_monitoring/const.py +++ b/custom_components/gne_pv_monitoring/const.py @@ -1,8 +1,9 @@ import datetime from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass -from homeassistant.const import (ELECTRIC_CURRENT_AMPERE, ELECTRIC_POTENTIAL_VOLT, ENERGY_WATT_HOUR, POWER_WATT, - TEMP_CELSIUS) +from homeassistant.const import (ELECTRIC_CURRENT_AMPERE, ELECTRIC_POTENTIAL_VOLT, ENERGY_KILO_WATT_HOUR, + POWER_WATT, TEMP_CELSIUS) +from homeassistant.helpers.entity import EntityCategory NAME = "GNE PV Monitoring" DOMAIN = "gne_pv_monitoring" @@ -32,35 +33,70 @@ "unit": ELECTRIC_POTENTIAL_VOLT, "device_class": SensorDeviceClass.VOLTAGE, "state_class": SensorStateClass.MEASUREMENT, + "icon": "mdi:lightning-bolt-circle", + "entity_category": None }, "inCurrent1": { "unit": ELECTRIC_CURRENT_AMPERE, "device_class": SensorDeviceClass.CURRENT, "state_class": SensorStateClass.MEASUREMENT, + "icon": "mdi:current-dc", + "entity_category": None }, "power1": { "unit": POWER_WATT, "device_class": SensorDeviceClass.POWER, "state_class": SensorStateClass.MEASUREMENT, + "icon": "mdi:flash", + "entity_category": None }, "outVoltage": { "unit": ELECTRIC_POTENTIAL_VOLT, "device_class": SensorDeviceClass.VOLTAGE, "state_class": SensorStateClass.MEASUREMENT, + "icon": "mdi:lightning-bolt-circle", + "entity_category": None }, "outCurrent": { "unit": ELECTRIC_CURRENT_AMPERE, "device_class": SensorDeviceClass.CURRENT, "state_class": SensorStateClass.MEASUREMENT, + "icon": "mdi:current-dc", + "entity_category": None }, "dailyEnergy": { - "unit": ENERGY_WATT_HOUR, + "unit": ENERGY_KILO_WATT_HOUR, "device_class": SensorDeviceClass.ENERGY, "state_class": SensorStateClass.TOTAL_INCREASING, + "icon": "mdi:solar-power", + "entity_category": None }, "temperature": { "unit": TEMP_CELSIUS, "device_class": SensorDeviceClass.TEMPERATURE, "state_class": SensorStateClass.MEASUREMENT, + "icon": "mdi:thermometer", + "entity_category": None + }, + "transTime": { + "unit": None, + "device_class": None, + "state_class": None, + "icon": "mdi:clock-outline", + "entity_category": EntityCategory.DIAGNOSTIC + }, + "warnCode": { + "unit": None, + "device_class": None, + "state_class": None, + "icon": "mdi:alert", + "entity_category": EntityCategory.DIAGNOSTIC + }, + "status": { + "unit": None, + "device_class": None, + "state_class": None, + "icon": "mdi:solar-panel", + "entity_category": EntityCategory.DIAGNOSTIC } } diff --git a/custom_components/gne_pv_monitoring/entity.py b/custom_components/gne_pv_monitoring/entity.py index 4b60b0c..599e19a 100644 --- a/custom_components/gne_pv_monitoring/entity.py +++ b/custom_components/gne_pv_monitoring/entity.py @@ -14,7 +14,6 @@ def __init__(self, entity_id, coordinator, config_entry, mac_id): def name(self): return f"{NAME} {self.mac_id}" - @property def unique_id(self): return f"{self.config_entry.entry_id}_{self.mac_id}" @@ -28,13 +27,3 @@ def device_info(self): def get_current_data(self) -> dict: return self.coordinator.data[self.mac_id] - - @property - def extra_state_attributes(self) -> dict: - return { - "trans_time": self.coordinator.data[self.mac_id]["transTime"] - } - - @property - def icon(self): - return ICON diff --git a/custom_components/gne_pv_monitoring/sensor.py b/custom_components/gne_pv_monitoring/sensor.py index cb77b4b..9bfb6fa 100644 --- a/custom_components/gne_pv_monitoring/sensor.py +++ b/custom_components/gne_pv_monitoring/sensor.py @@ -45,6 +45,14 @@ def device_class(self): def state_class(self): return PARAMETERS[self._parameter]["state_class"] + @property + def entity_category(self): + return PARAMETERS[self._parameter]["entity_category"] + + @property + def icon(self): + return PARAMETERS[self._parameter]["icon"] + @property def unique_id(self): return f"{super().unique_id}_sensor_{self._parameter}"