sensors mini rework

This commit is contained in:
Yordan Suarez
2021-12-13 12:02:39 -05:00
parent c4623d8234
commit ef2516ada4
6 changed files with 104 additions and 89 deletions

View File

@@ -1,10 +1,20 @@
"""BlueprintEntity class""" """BlueprintEntity class"""
from homeassistant.components.sensor import SensorEntity, STATE_CLASS_MEASUREMENT
from homeassistant.helpers.update_coordinator import CoordinatorEntity 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 from .const import DOMAIN, VERSION, ATTRIBUTION
class FplEntity(CoordinatorEntity): class FplEntity(CoordinatorEntity, SensorEntity):
def __init__(self, coordinator, config_entry, account, sensorName): def __init__(self, coordinator, config_entry, account, sensorName):
super().__init__(coordinator) super().__init__(coordinator)
self.config_entry = config_entry self.config_entry = config_entry
@@ -47,3 +57,69 @@ class FplEntity(CoordinatorEntity):
def getData(self, field): def getData(self, field):
return self.coordinator.data.get(self.account).get(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"

View File

@@ -1,7 +1,7 @@
from .fplEntity import FplEntity from .fplEntity import FplMoneyEntity
class FplAverageDailySensor(FplEntity): class FplAverageDailySensor(FplMoneyEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Daily Average") super().__init__(coordinator, config, account, "Daily Average")
@@ -15,12 +15,8 @@ class FplAverageDailySensor(FplEntity):
return self.getData("daily_avg") return self.getData("daily_avg")
@property
def icon(self):
return "mdi:currency-usd"
class BudgetDailyAverageSensor(FplMoneyEntity):
class BudgetDailyAverageSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Budget Daily Average") super().__init__(coordinator, config, account, "Budget Daily Average")
@@ -28,19 +24,11 @@ class BudgetDailyAverageSensor(FplEntity):
def state(self): def state(self):
return self.getData("budget_billing_daily_avg") return self.getData("budget_billing_daily_avg")
@property
def icon(self):
return "mdi:currency-usd"
class ActualDailyAverageSensor(FplMoneyEntity):
class ActualDailyAverageSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Actual Daily Average") super().__init__(coordinator, config, account, "Actual Daily Average")
@property @property
def state(self): def state(self):
return self.getData("daily_avg") return self.getData("daily_avg")
@property
def icon(self):
return "mdi:currency-usd"

View File

@@ -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): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Daily Usage") super().__init__(coordinator, config, account, "Daily Usage")
@@ -23,12 +23,8 @@ class FplDailyUsageSensor(FplEntity):
return {} return {}
@property
def icon(self):
return "mdi:currency-usd"
class FplDailyUsageKWHSensor(FplEnergyEntity):
class FplDailyUsageKWHSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Daily Usage KWH") super().__init__(coordinator, config, account, "Daily Usage KWH")
@@ -49,7 +45,3 @@ class FplDailyUsageKWHSensor(FplEntity):
return {"date": data[-1]["date"]} return {"date": data[-1]["date"]}
return {} return {}
@property
def icon(self):
return "mdi:currency-usd"

View File

@@ -1,7 +1,7 @@
from .fplEntity import FplEntity from .fplEntity import FplDateEntity
class CurrentBillDateSensor(FplEntity): class CurrentBillDateSensor(FplDateEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Current Bill Date") super().__init__(coordinator, config, account, "Current Bill Date")
@@ -9,12 +9,8 @@ class CurrentBillDateSensor(FplEntity):
def state(self): def state(self):
return self.getData("current_bill_date") return self.getData("current_bill_date")
@property
def icon(self):
return "mdi:calendar"
class NextBillDateSensor(FplDateEntity):
class NextBillDateSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Next Bill Date") super().__init__(coordinator, config, account, "Next Bill Date")
@@ -22,12 +18,8 @@ class NextBillDateSensor(FplEntity):
def state(self): def state(self):
return self.getData("next_bill_date") return self.getData("next_bill_date")
@property
def icon(self):
return "mdi:calendar"
class ServiceDaysSensor(FplDateEntity):
class ServiceDaysSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Service Days") super().__init__(coordinator, config, account, "Service Days")
@@ -35,12 +27,8 @@ class ServiceDaysSensor(FplEntity):
def state(self): def state(self):
return self.getData("service_days") return self.getData("service_days")
@property
def icon(self):
return "mdi:calendar"
class AsOfDaysSensor(FplDateEntity):
class AsOfDaysSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "As Of Days") super().__init__(coordinator, config, account, "As Of Days")
@@ -48,19 +36,11 @@ class AsOfDaysSensor(FplEntity):
def state(self): def state(self):
return self.getData("as_of_days") return self.getData("as_of_days")
@property
def icon(self):
return "mdi:calendar"
class RemainingDaysSensor(FplDateEntity):
class RemainingDaysSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Remaining Days") super().__init__(coordinator, config, account, "Remaining Days")
@property @property
def state(self): def state(self):
return self.getData("remaining_days") return self.getData("remaining_days")
@property
def icon(self):
return "mdi:calendar"

View File

@@ -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): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Projected KWH") super().__init__(coordinator, config, account, "Projected KWH")
@@ -9,12 +10,8 @@ class ProjectedKWHSensor(FplEntity):
def state(self): def state(self):
return self.getData("projectedKWH") return self.getData("projectedKWH")
@property
def icon(self):
return "mdi:flash"
class DailyAverageKWHSensor(FplEnergyEntity):
class DailyAverageKWHSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Daily Average KWH") super().__init__(coordinator, config, account, "Daily Average KWH")
@@ -22,12 +19,8 @@ class DailyAverageKWHSensor(FplEntity):
def state(self): def state(self):
return self.getData("dailyAverageKWH") return self.getData("dailyAverageKWH")
@property
def icon(self):
return "mdi:flash"
class BillToDateKWHSensor(FplEnergyEntity):
class BillToDateKWHSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Bill To Date KWH") super().__init__(coordinator, config, account, "Bill To Date KWH")
@@ -36,5 +29,7 @@ class BillToDateKWHSensor(FplEntity):
return self.getData("billToDateKWH") return self.getData("billToDateKWH")
@property @property
def icon(self): def state_class(self) -> str:
return "mdi:flash" """Return the state class of this entity, from STATE_CLASSES, if any."""
return STATE_CLASS_TOTAL_INCREASING

View File

@@ -1,7 +1,7 @@
from .fplEntity import FplEntity from .fplEntity import FplMoneyEntity
class FplProjectedBillSensor(FplEntity): class FplProjectedBillSensor(FplMoneyEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Projected Bill") super().__init__(coordinator, config, account, "Projected Bill")
@@ -26,13 +26,9 @@ class FplProjectedBillSensor(FplEntity):
return attributes return attributes
@property
def icon(self):
return "mdi:currency-usd"
# Defered Amount # Defered Amount
class DeferedAmountSensor(FplEntity): class DeferedAmountSensor(FplMoneyEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Defered Amount") super().__init__(coordinator, config, account, "Defered Amount")
@@ -42,12 +38,8 @@ class DeferedAmountSensor(FplEntity):
return self.getData("defered_amount") return self.getData("defered_amount")
return 0 return 0
@property
def icon(self):
return "mdi:currency-usd"
class ProjectedBudgetBillSensor(FplMoneyEntity):
class ProjectedBudgetBillSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Projected Budget Bill") super().__init__(coordinator, config, account, "Projected Budget Bill")
@@ -55,19 +47,11 @@ class ProjectedBudgetBillSensor(FplEntity):
def state(self): def state(self):
return self.getData("budget_billing_projected_bill") return self.getData("budget_billing_projected_bill")
@property
def icon(self):
return "mdi:currency-usd"
class ProjectedActualBillSensor(FplMoneyEntity):
class ProjectedActualBillSensor(FplEntity):
def __init__(self, coordinator, config, account): def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Projected Actual Bill") super().__init__(coordinator, config, account, "Projected Actual Bill")
@property @property
def state(self): def state(self):
return self.getData("projected_bill") return self.getData("projected_bill")
@property
def icon(self):
return "mdi:currency-usd"