From f3ccfce2e49b60c4a142c85884e9745c46b67388 Mon Sep 17 00:00:00 2001 From: Yordan Suarez Date: Thu, 3 Mar 2022 07:31:40 -0500 Subject: [PATCH] renamed defineAttributes method with customAttributes --- custom_components/fpl/fplEntity.py | 4 +- .../fpl/sensor_AverageDailySensor.py | 13 +++--- .../fpl/sensor_DailyUsageSensor.py | 42 ++++++++++--------- custom_components/fpl/sensor_KWHSensor.py | 19 ++++++--- .../fpl/sensor_ProjectedBillSensor.py | 20 +++++---- 5 files changed, 58 insertions(+), 40 deletions(-) diff --git a/custom_components/fpl/fplEntity.py b/custom_components/fpl/fplEntity.py index 0af1072..91b0553 100644 --- a/custom_components/fpl/fplEntity.py +++ b/custom_components/fpl/fplEntity.py @@ -43,7 +43,7 @@ class FplEntity(CoordinatorEntity, SensorEntity): "manufacturer": "Florida Power & Light", } - def defineAttributes(self) -> dict: + def customAttributes(self) -> dict: """override this method to set custom attributes""" return {} @@ -54,7 +54,7 @@ class FplEntity(CoordinatorEntity, SensorEntity): "attribution": ATTRIBUTION, "integration": "FPL", } - attributes.update(self.defineAttributes()) + attributes.update(self.customAttributes()) return attributes def getData(self, field): diff --git a/custom_components/fpl/sensor_AverageDailySensor.py b/custom_components/fpl/sensor_AverageDailySensor.py index 5d5a139..f997c0c 100644 --- a/custom_components/fpl/sensor_AverageDailySensor.py +++ b/custom_components/fpl/sensor_AverageDailySensor.py @@ -1,4 +1,5 @@ """Average daily sensors""" +from homeassistant.components.sensor import STATE_CLASS_TOTAL from .fplEntity import FplMoneyEntity @@ -18,10 +19,10 @@ class DailyAverageSensor(FplMoneyEntity): return self.getData("daily_avg") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} - attributes["state_class"] = "total" + attributes["state_class"] = STATE_CLASS_TOTAL return attributes @@ -35,10 +36,10 @@ class BudgetDailyAverageSensor(FplMoneyEntity): def state(self): return self.getData("budget_billing_daily_avg") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} - attributes["state_class"] = "total" + attributes["state_class"] = STATE_CLASS_TOTAL return attributes @@ -52,8 +53,8 @@ class ActualDailyAverageSensor(FplMoneyEntity): def state(self): return self.getData("daily_avg") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} - attributes["state_class"] = "total" + attributes["state_class"] = STATE_CLASS_TOTAL return attributes diff --git a/custom_components/fpl/sensor_DailyUsageSensor.py b/custom_components/fpl/sensor_DailyUsageSensor.py index a3e1f06..d0c8ba1 100644 --- a/custom_components/fpl/sensor_DailyUsageSensor.py +++ b/custom_components/fpl/sensor_DailyUsageSensor.py @@ -1,4 +1,6 @@ """Daily Usage Sensors""" +from homeassistant.components.sensor import STATE_CLASS_TOTAL_INCREASING +from datetime import timedelta from .fplEntity import FplEnergyEntity, FplMoneyEntity @@ -17,11 +19,11 @@ class FplDailyUsageSensor(FplMoneyEntity): return None - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" data = self.getData("daily_usage") attributes = {} - attributes["state_class"] = "total_increasing" + attributes["state_class"] = STATE_CLASS_TOTAL_INCREASING if data is not None and len(data) > 0 and "readTime" in data[-1].keys(): attributes["date"] = data[-1]["readTime"] @@ -43,18 +45,16 @@ class FplDailyUsageKWHSensor(FplEnergyEntity): return None - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" data = self.getData("daily_usage") + date = data[-1]["readTime"] + last_reset = date - timedelta(days=1) + attributes = {} - attributes["state_class"] = "total_increasing" - - if data is not None: - if data[-1] is not None and "readTime" in data[-1].keys(): - attributes["date"] = data[-1]["readTime"] - if data[-2] is not None and "readTime" in data[-2].keys(): - attributes["last_reset"] = data[-2]["readTime"] - + attributes["state_class"] = STATE_CLASS_TOTAL_INCREASING + attributes["date"] = date + attributes["last_reset"] = last_reset return attributes @@ -71,14 +71,16 @@ class FplDailyReceivedKWHSensor(FplEnergyEntity): return data[-1]["netReceivedKwh"] return 0 - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" data = self.getData("daily_usage") + date = data[-1]["readTime"] + last_reset = date - timedelta(days=1) attributes = {} - attributes["state_class"] = "total_increasing" - attributes["date"] = data[-1]["readTime"] - attributes["last_reset"] = data[-2]["readTime"] + attributes["state_class"] = STATE_CLASS_TOTAL_INCREASING + attributes["date"] = date + attributes["last_reset"] = last_reset return attributes @@ -95,12 +97,14 @@ class FplDailyDeliveredKWHSensor(FplEnergyEntity): return data[-1]["netDeliveredKwh"] return 0 - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" data = self.getData("daily_usage") + date = data[-1]["readTime"] + last_reset = date - timedelta(days=1) attributes = {} - attributes["state_class"] = "total_increasing" - attributes["date"] = data[-1]["readTime"] - attributes["last_reset"] = data[-2]["readTime"] + attributes["state_class"] = STATE_CLASS_TOTAL_INCREASING + attributes["date"] = date + attributes["last_reset"] = last_reset return attributes diff --git a/custom_components/fpl/sensor_KWHSensor.py b/custom_components/fpl/sensor_KWHSensor.py index 865becb..767f186 100644 --- a/custom_components/fpl/sensor_KWHSensor.py +++ b/custom_components/fpl/sensor_KWHSensor.py @@ -1,4 +1,6 @@ """energy sensors""" +from datetime import date, timedelta +import datetime from homeassistant.components.sensor import ( STATE_CLASS_TOTAL_INCREASING, STATE_CLASS_TOTAL, @@ -14,7 +16,7 @@ class ProjectedKWHSensor(FplEnergyEntity): def state(self): return self.getData("projectedKWH") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} attributes["state_class"] = STATE_CLASS_TOTAL @@ -29,7 +31,7 @@ class DailyAverageKWHSensor(FplEnergyEntity): def state(self): return self.getData("dailyAverageKWH") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} attributes["state_class"] = STATE_CLASS_TOTAL @@ -44,10 +46,17 @@ class BillToDateKWHSensor(FplEnergyEntity): def state(self): return self.getData("billToDateKWH") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" + + # data = self.getData("daily_usage") + # date = data[-1]["readTime"] + asOfDays = self.getData("as_of_days") + last_reset = date.today() - timedelta(days=asOfDays) + attributes = {} attributes["state_class"] = STATE_CLASS_TOTAL_INCREASING + attributes["last_reset"] = last_reset return attributes @@ -59,7 +68,7 @@ class NetReceivedKWHSensor(FplEnergyEntity): def state(self): return self.getData("recMtrReading") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} attributes["state_class"] = STATE_CLASS_TOTAL_INCREASING @@ -74,7 +83,7 @@ class NetDeliveredKWHSensor(FplEnergyEntity): def state(self): return self.getData("delMtrReading") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} attributes["state_class"] = STATE_CLASS_TOTAL_INCREASING diff --git a/custom_components/fpl/sensor_ProjectedBillSensor.py b/custom_components/fpl/sensor_ProjectedBillSensor.py index f8c3671..3c3b123 100644 --- a/custom_components/fpl/sensor_ProjectedBillSensor.py +++ b/custom_components/fpl/sensor_ProjectedBillSensor.py @@ -1,4 +1,8 @@ """Projected bill sensors""" +from homeassistant.components.sensor import ( + STATE_CLASS_TOTAL_INCREASING, + STATE_CLASS_TOTAL, +) from .fplEntity import FplMoneyEntity @@ -18,10 +22,10 @@ class FplProjectedBillSensor(FplMoneyEntity): return self.getData("projected_bill") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} - attributes["state_class"] = "total" + attributes["state_class"] = STATE_CLASS_TOTAL attributes["budget_bill"] = self.getData("budget_bill") return attributes @@ -39,10 +43,10 @@ class DeferedAmountSensor(FplMoneyEntity): return self.getData("defered_amount") return 0 - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} - attributes["state_class"] = "total" + attributes["state_class"] = STATE_CLASS_TOTAL return attributes @@ -56,10 +60,10 @@ class ProjectedBudgetBillSensor(FplMoneyEntity): def state(self): return self.getData("budget_billing_projected_bill") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} - attributes["state_class"] = "total" + attributes["state_class"] = STATE_CLASS_TOTAL return attributes @@ -73,8 +77,8 @@ class ProjectedActualBillSensor(FplMoneyEntity): def state(self): return self.getData("projected_bill") - def defineAttributes(self): + def customAttributes(self): """Return the state attributes.""" attributes = {} - attributes["state_class"] = "total" + attributes["state_class"] = STATE_CLASS_TOTAL return attributes