From 485717d662fd334e8452806c0b233136aacc9183 Mon Sep 17 00:00:00 2001 From: Adam Outler Date: Thu, 30 Dec 2021 15:07:04 -0500 Subject: [PATCH] Fixing sensor metadata --- custom_components/fpl/sensor_AllData.py | 2 +- .../fpl/sensor_AverageDailySensor.py | 2 +- .../fpl/sensor_DailyUsageSensor.py | 41 ++++++++++++------- custom_components/fpl/sensor_KWHSensor.py | 17 +++++--- .../fpl/sensor_ProjectedBillSensor.py | 14 +++---- 5 files changed, 48 insertions(+), 28 deletions(-) diff --git a/custom_components/fpl/sensor_AllData.py b/custom_components/fpl/sensor_AllData.py index 9f9840d..0e1ccad 100644 --- a/custom_components/fpl/sensor_AllData.py +++ b/custom_components/fpl/sensor_AllData.py @@ -27,7 +27,7 @@ class AllDataSensor(FplEntity): """Return the state attributes.""" attributes = {} attributes["friendly_name"] = "Budget Projected Bill" - attributes["device_class"] = "monitary" + attributes["device_class"] = "monetary" attributes["state_class"] = "total" attributes["unit_of_measurement"] = "$" return attributes \ No newline at end of file diff --git a/custom_components/fpl/sensor_AverageDailySensor.py b/custom_components/fpl/sensor_AverageDailySensor.py index d170470..10bc13c 100644 --- a/custom_components/fpl/sensor_AverageDailySensor.py +++ b/custom_components/fpl/sensor_AverageDailySensor.py @@ -23,7 +23,7 @@ class FplAverageDailySensor(FplEntity): """Return the state attributes.""" attributes = {} attributes["friendly_name"] = "Daily Average" - attributes["device_class"] = "monitary" + attributes["device_class"] = "monetary" attributes["state_class"] = "total" attributes["unit_of_measurement"] = "$" return attributes diff --git a/custom_components/fpl/sensor_DailyUsageSensor.py b/custom_components/fpl/sensor_DailyUsageSensor.py index 3ec2928..594b686 100644 --- a/custom_components/fpl/sensor_DailyUsageSensor.py +++ b/custom_components/fpl/sensor_DailyUsageSensor.py @@ -9,7 +9,7 @@ class FplDailyUsageSensor(FplEntity): def state(self): data = self.getData("daily_usage") - if ((data is not None) and (len(data) > 0)): + if (data is not None) and (len(data) > 0): return data[-1]["cost"] return None @@ -22,8 +22,11 @@ class FplDailyUsageSensor(FplEntity): attributes["device_class"] = "monetary" attributes["state_class"] = "total_increasing" attributes["unit_of_measurement"] = "$" - if ((data is not None) and (data[-1]["cost"] is not None)): - attributes["date"] = data[-1]["readTime"] + if data is not None: + if (data[-1] is not None) and (data[-1]["readTime"] is not None): + attributes["date"] = data[-1]["readTime"] + if (data[-2] is not None) and (data[-2]["readTime"] is not None): + attributes["last_reset"] = data[-2]["readTime"] return attributes @property @@ -39,7 +42,7 @@ class FplDailyUsageKWHSensor(FplEntity): def state(self): data = self.getData("daily_usage") - if ((data is not None) and (data[-1]["usage"] is not None)): + if (data is not None) and (data[-1]["usage"] is not None): return data[-1]["usage"] return None @@ -55,17 +58,18 @@ class FplDailyUsageKWHSensor(FplEntity): attributes["unit_of_measurement"] = "kWh" if data is not None: - if ((data[-1] is not None) and (data[-1]["readTime"] is not None)): + if (data[-1] is not None) and (data[-1]["readTime"] is not None): attributes["date"] = data[-1]["readTime"] - if ((data[-2] is not None) and (data[-2]["readTime"] is not None)): + if (data[-2] is not None) and (data[-2]["readTime"] is not None): attributes["last_reset"] = data[-2]["readTime"] - + return attributes @property def icon(self): return "mdi:flash" + class FplDailyReceivedKWHSensor(FplEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Daily Received KWH") @@ -73,7 +77,10 @@ class FplDailyReceivedKWHSensor(FplEntity): @property def state(self): data = self.getData("daily_usage") - return data[-1]["netReceivedKwh"] + try: + return data[-1]["netReceivedKwh"] + except: + return 0 def defineAttributes(self): """Return the state attributes.""" @@ -84,15 +91,18 @@ class FplDailyReceivedKWHSensor(FplEntity): attributes["device_class"] = "energy" attributes["state_class"] = "total_increasing" attributes["unit_of_measurement"] = "kWh" - attributes["date"] = data[-1]["readTime"] - attributes["last_reset"] = data[-2]["readTime"] + if data is not None: + if (data[-1] is not None) and (data[-1]["readTime"] is not None): + attributes["date"] = data[-1]["readTime"] + if (data[-2] is not None) and (data[-2]["readTime"] is not None): + attributes["last_reset"] = data[-2]["readTime"] return attributes - @property def icon(self): return "mdi:flash" + class FplDailyDeliveredKWHSensor(FplEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Daily Delivered KWH") @@ -100,7 +110,7 @@ class FplDailyDeliveredKWHSensor(FplEntity): @property def state(self): data = self.getData("daily_usage") - return data[-1]["netDeliveredKwh"] + return data[-1]["netDeliveredKwh"] def defineAttributes(self): """Return the state attributes.""" @@ -111,8 +121,11 @@ class FplDailyDeliveredKWHSensor(FplEntity): attributes["device_class"] = "energy" attributes["state_class"] = "total_increasing" attributes["unit_of_measurement"] = "kWh" - attributes["date"] = data[-1]["readTime"] - attributes["last_reset"] = data[-2]["readTime"] + if data is not None: + if (data[-1] is not None) and (data[-1]["readTime"] is not None): + attributes["date"] = data[-1]["readTime"] + if (data[-2] is not None) and (data[-2]["readTime"] is not None): + attributes["last_reset"] = data[-2]["readTime"] return attributes @property diff --git a/custom_components/fpl/sensor_KWHSensor.py b/custom_components/fpl/sensor_KWHSensor.py index 449f893..92b7992 100644 --- a/custom_components/fpl/sensor_KWHSensor.py +++ b/custom_components/fpl/sensor_KWHSensor.py @@ -17,7 +17,7 @@ class ProjectedKWHSensor(FplEntity): attributes = {} attributes["friendly_name"] = "Projected KWH" attributes["device_class"] = "energy" - attributes["state_class"] = "total_increasing" + attributes["state_class"] = "total" attributes["unit_of_measurement"] = "kWh" return attributes @@ -38,7 +38,7 @@ class DailyAverageKWHSensor(FplEntity): attributes = {} attributes["friendly_name"] = "Daily Average" attributes["device_class"] = "energy" - attributes["state_class"] = "total_increasing" + attributes["state_class"] = "total" attributes["unit_of_measurement"] = "kWh" return attributes @@ -57,10 +57,11 @@ class BillToDateKWHSensor(FplEntity): def defineAttributes(self): """Return the state attributes.""" attributes = {} - attributes["friendly_name"] = "Bill To Date" + attributes["friendly_name"] = "Billing Usage" attributes["device_class"] = "energy" attributes["state_class"] = "total_increasing" attributes["unit_of_measurement"] = "kWh" + attributes["last_reset"] = self.getData("billStartDate") return attributes class NetReceivedKWHSensor(FplEntity): @@ -69,7 +70,10 @@ class NetReceivedKWHSensor(FplEntity): @property def state(self): - return self.getData("recMtrReading") + try: + return self.getData("recMtrReading") + except: + return 0 @property def icon(self): @@ -92,7 +96,10 @@ class NetDeliveredKWHSensor(FplEntity): @property def state(self): - return self.getData("delMtrReading") + try: + return self.getData("delMtrReading") + except: + return self.getData("billToDateKWH") @property def icon(self): diff --git a/custom_components/fpl/sensor_ProjectedBillSensor.py b/custom_components/fpl/sensor_ProjectedBillSensor.py index 44650f0..1113dd9 100644 --- a/custom_components/fpl/sensor_ProjectedBillSensor.py +++ b/custom_components/fpl/sensor_ProjectedBillSensor.py @@ -18,8 +18,8 @@ class FplProjectedBillSensor(FplEntity): def defineAttributes(self): """Return the state attributes.""" attributes = {} - attributes["friendly_name"] = "Projected Bill" - attributes["device_class"] = "monitary" + attributes["friendly_name"] = "Projected Bill Payment Due" + attributes["device_class"] = "monetary" attributes["state_class"] = "total" attributes["unit_of_measurement"] = "$" return attributes @@ -32,12 +32,12 @@ class FplProjectedBillSensor(FplEntity): # Defered Amount class DeferedAmountSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "Defered Amount") + super().__init__(coordinator, config, account, "Deferred Amount") @property def state(self): - if self.getData("defered_amount") is not None: - return self.getData("defered_amount") + if self.getData("deferred_amount") is not None: + return self.getData("deferred_amount") return 0 @property @@ -47,8 +47,8 @@ class DeferedAmountSensor(FplEntity): def defineAttributes(self): """Return the state attributes.""" attributes = {} - attributes["friendly_name"] = "Defered Amount" - attributes["device_class"] = "monitary" + attributes["friendly_name"] = "Deferred Amount" + attributes["device_class"] = "monetary" attributes["state_class"] = "total" attributes["unit_of_measurement"] = "$" return attributes