From ef2516ada4782124331c6bfed0d5f75e62682df2 Mon Sep 17 00:00:00 2001 From: Yordan Suarez Date: Mon, 13 Dec 2021 12:02:39 -0500 Subject: [PATCH] sensors mini rework --- custom_components/fpl/fplEntity.py | 78 ++++++++++++++++++- .../fpl/sensor_AverageDailySensor.py | 20 +---- .../fpl/sensor_DailyUsageSensor.py | 14 +--- custom_components/fpl/sensor_DatesSensor.py | 32 ++------ custom_components/fpl/sensor_KWHSensor.py | 23 +++--- .../fpl/sensor_ProjectedBillSensor.py | 26 ++----- 6 files changed, 104 insertions(+), 89 deletions(-) diff --git a/custom_components/fpl/fplEntity.py b/custom_components/fpl/fplEntity.py index 7b30d27..1680931 100644 --- a/custom_components/fpl/fplEntity.py +++ b/custom_components/fpl/fplEntity.py @@ -1,10 +1,20 @@ """BlueprintEntity class""" +from homeassistant.components.sensor import SensorEntity, STATE_CLASS_MEASUREMENT from homeassistant.helpers.update_coordinator import CoordinatorEntity +from homeassistant.const import ( + CURRENCY_DOLLAR, + DEVICE_CLASS_ENERGY, + ENERGY_KILO_WATT_HOUR, + DEVICE_CLASS_MONETARY, + DEVICE_CLASS_TIMESTAMP, +) + +from datetime import datetime, timedelta from .const import DOMAIN, VERSION, ATTRIBUTION -class FplEntity(CoordinatorEntity): +class FplEntity(CoordinatorEntity, SensorEntity): def __init__(self, coordinator, config_entry, account, sensorName): super().__init__(coordinator) self.config_entry = config_entry @@ -47,3 +57,69 @@ class FplEntity(CoordinatorEntity): def getData(self, field): return self.coordinator.data.get(self.account).get(field) + + +class FplEnergyEntity(FplEntity): + def __init__(self, coordinator, config_entry, account, sensorName): + super().__init__(coordinator, config_entry, account, sensorName) + + @property + def state_class(self) -> str: + """Return the state class of this entity, from STATE_CLASSES, if any.""" + + return STATE_CLASS_MEASUREMENT + + @property + def last_reset(self) -> datetime: + """Return the time when the sensor was last reset, if any.""" + + today = datetime.today() + yesterday = today - timedelta(days=1) + return datetime.combine(yesterday, datetime.min.time()) + + @property + def device_class(self) -> str: + """Return the class of this device, from component DEVICE_CLASSES.""" + return DEVICE_CLASS_ENERGY + + @property + def unit_of_measurement(self) -> str: + """Return the unit of measurement of this entity, if any.""" + return ENERGY_KILO_WATT_HOUR + + @property + def icon(self): + return "mdi:flash" + + +class FplMoneyEntity(FplEntity): + def __init__(self, coordinator, config_entry, account, sensorName): + super().__init__(coordinator, config_entry, account, sensorName) + + @property + def icon(self): + return "mdi:currency-usd" + + @property + def device_class(self) -> str: + """Return the class of this device, from component DEVICE_CLASSES.""" + return DEVICE_CLASS_MONETARY + + @property + def unit_of_measurement(self) -> str: + """Return the unit of measurement of this entity, if any.""" + return CURRENCY_DOLLAR + + +class FplDateEntity(FplEntity): + def __init__(self, coordinator, config_entry, account, sensorName): + super().__init__(coordinator, config_entry, account, sensorName) + + # @property + # def device_class(self) -> str: + # """Return the class of this device, from component DEVICE_CLASSES.""" + # return DEVICE_CLASS_TIMESTAMP + + @property + def icon(self): + return "mdi:calendar" \ No newline at end of file diff --git a/custom_components/fpl/sensor_AverageDailySensor.py b/custom_components/fpl/sensor_AverageDailySensor.py index 66d25f1..3399ded 100644 --- a/custom_components/fpl/sensor_AverageDailySensor.py +++ b/custom_components/fpl/sensor_AverageDailySensor.py @@ -1,7 +1,7 @@ -from .fplEntity import FplEntity +from .fplEntity import FplMoneyEntity -class FplAverageDailySensor(FplEntity): +class FplAverageDailySensor(FplMoneyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Daily Average") @@ -15,12 +15,8 @@ class FplAverageDailySensor(FplEntity): return self.getData("daily_avg") - @property - def icon(self): - return "mdi:currency-usd" - -class BudgetDailyAverageSensor(FplEntity): +class BudgetDailyAverageSensor(FplMoneyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Budget Daily Average") @@ -28,19 +24,11 @@ class BudgetDailyAverageSensor(FplEntity): def state(self): return self.getData("budget_billing_daily_avg") - @property - def icon(self): - return "mdi:currency-usd" - -class ActualDailyAverageSensor(FplEntity): +class ActualDailyAverageSensor(FplMoneyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Actual Daily Average") @property def state(self): return self.getData("daily_avg") - - @property - def icon(self): - return "mdi:currency-usd" diff --git a/custom_components/fpl/sensor_DailyUsageSensor.py b/custom_components/fpl/sensor_DailyUsageSensor.py index 299188e..8bfa90e 100644 --- a/custom_components/fpl/sensor_DailyUsageSensor.py +++ b/custom_components/fpl/sensor_DailyUsageSensor.py @@ -1,7 +1,7 @@ -from .fplEntity import FplEntity +from .fplEntity import FplEnergyEntity, FplMoneyEntity -class FplDailyUsageSensor(FplEntity): +class FplDailyUsageSensor(FplMoneyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Daily Usage") @@ -23,12 +23,8 @@ class FplDailyUsageSensor(FplEntity): return {} - @property - def icon(self): - return "mdi:currency-usd" - -class FplDailyUsageKWHSensor(FplEntity): +class FplDailyUsageKWHSensor(FplEnergyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Daily Usage KWH") @@ -49,7 +45,3 @@ class FplDailyUsageKWHSensor(FplEntity): return {"date": data[-1]["date"]} return {} - - @property - def icon(self): - return "mdi:currency-usd" diff --git a/custom_components/fpl/sensor_DatesSensor.py b/custom_components/fpl/sensor_DatesSensor.py index b0414d7..b2f2fdb 100644 --- a/custom_components/fpl/sensor_DatesSensor.py +++ b/custom_components/fpl/sensor_DatesSensor.py @@ -1,7 +1,7 @@ -from .fplEntity import FplEntity +from .fplEntity import FplDateEntity -class CurrentBillDateSensor(FplEntity): +class CurrentBillDateSensor(FplDateEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Current Bill Date") @@ -9,12 +9,8 @@ class CurrentBillDateSensor(FplEntity): def state(self): return self.getData("current_bill_date") - @property - def icon(self): - return "mdi:calendar" - -class NextBillDateSensor(FplEntity): +class NextBillDateSensor(FplDateEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Next Bill Date") @@ -22,12 +18,8 @@ class NextBillDateSensor(FplEntity): def state(self): return self.getData("next_bill_date") - @property - def icon(self): - return "mdi:calendar" - -class ServiceDaysSensor(FplEntity): +class ServiceDaysSensor(FplDateEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Service Days") @@ -35,12 +27,8 @@ class ServiceDaysSensor(FplEntity): def state(self): return self.getData("service_days") - @property - def icon(self): - return "mdi:calendar" - -class AsOfDaysSensor(FplEntity): +class AsOfDaysSensor(FplDateEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "As Of Days") @@ -48,19 +36,11 @@ class AsOfDaysSensor(FplEntity): def state(self): return self.getData("as_of_days") - @property - def icon(self): - return "mdi:calendar" - -class RemainingDaysSensor(FplEntity): +class RemainingDaysSensor(FplDateEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Remaining Days") @property def state(self): return self.getData("remaining_days") - - @property - def icon(self): - return "mdi:calendar" diff --git a/custom_components/fpl/sensor_KWHSensor.py b/custom_components/fpl/sensor_KWHSensor.py index b360e4d..6b9ba6b 100644 --- a/custom_components/fpl/sensor_KWHSensor.py +++ b/custom_components/fpl/sensor_KWHSensor.py @@ -1,7 +1,8 @@ -from .fplEntity import FplEntity +from homeassistant.components.sensor import STATE_CLASS_TOTAL_INCREASING +from .fplEntity import FplEnergyEntity -class ProjectedKWHSensor(FplEntity): +class ProjectedKWHSensor(FplEnergyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Projected KWH") @@ -9,12 +10,8 @@ class ProjectedKWHSensor(FplEntity): def state(self): return self.getData("projectedKWH") - @property - def icon(self): - return "mdi:flash" - -class DailyAverageKWHSensor(FplEntity): +class DailyAverageKWHSensor(FplEnergyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Daily Average KWH") @@ -22,12 +19,8 @@ class DailyAverageKWHSensor(FplEntity): def state(self): return self.getData("dailyAverageKWH") - @property - def icon(self): - return "mdi:flash" - -class BillToDateKWHSensor(FplEntity): +class BillToDateKWHSensor(FplEnergyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Bill To Date KWH") @@ -36,5 +29,7 @@ class BillToDateKWHSensor(FplEntity): return self.getData("billToDateKWH") @property - def icon(self): - return "mdi:flash" + def state_class(self) -> str: + """Return the state class of this entity, from STATE_CLASSES, if any.""" + + return STATE_CLASS_TOTAL_INCREASING diff --git a/custom_components/fpl/sensor_ProjectedBillSensor.py b/custom_components/fpl/sensor_ProjectedBillSensor.py index acf7482..7f6a515 100644 --- a/custom_components/fpl/sensor_ProjectedBillSensor.py +++ b/custom_components/fpl/sensor_ProjectedBillSensor.py @@ -1,7 +1,7 @@ -from .fplEntity import FplEntity +from .fplEntity import FplMoneyEntity -class FplProjectedBillSensor(FplEntity): +class FplProjectedBillSensor(FplMoneyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Projected Bill") @@ -26,13 +26,9 @@ class FplProjectedBillSensor(FplEntity): return attributes - @property - def icon(self): - return "mdi:currency-usd" - # Defered Amount -class DeferedAmountSensor(FplEntity): +class DeferedAmountSensor(FplMoneyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Defered Amount") @@ -42,12 +38,8 @@ class DeferedAmountSensor(FplEntity): return self.getData("defered_amount") return 0 - @property - def icon(self): - return "mdi:currency-usd" - -class ProjectedBudgetBillSensor(FplEntity): +class ProjectedBudgetBillSensor(FplMoneyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Projected Budget Bill") @@ -55,19 +47,11 @@ class ProjectedBudgetBillSensor(FplEntity): def state(self): return self.getData("budget_billing_projected_bill") - @property - def icon(self): - return "mdi:currency-usd" - -class ProjectedActualBillSensor(FplEntity): +class ProjectedActualBillSensor(FplMoneyEntity): def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Projected Actual Bill") @property def state(self): return self.getData("projected_bill") - - @property - def icon(self): - return "mdi:currency-usd"