diff --git a/custom_components/fpl/ProjectedBillSensor.py b/custom_components/fpl/ProjectedBillSensor.py index bf14a32..8945dd0 100644 --- a/custom_components/fpl/ProjectedBillSensor.py +++ b/custom_components/fpl/ProjectedBillSensor.py @@ -24,13 +24,12 @@ class FplProjectedBillSensor(FplSensor): @property def device_state_attributes(self): """Return the state attributes.""" - try: - if "budget_bill" in self.data.keys(): - self.attr["budget_bill"] = self.data["budget_bill"] - except: - pass - return self.attr + if "budget_bill" in self.data.keys(): + self.attr["budget_bill"] = self.data["budget_bill"] + + + return self._state @property def icon(self): diff --git a/custom_components/fpl/const.py b/custom_components/fpl/const.py index 63615e0..5a418bb 100644 --- a/custom_components/fpl/const.py +++ b/custom_components/fpl/const.py @@ -3,7 +3,7 @@ NAME = "FPL Integration" DOMAIN = "fpl" DOMAIN_DATA = f"{DOMAIN}_data" -VERSION = "0.0.1" +VERSION = "0.1.0" PLATFORMS = ["sensor"] REQUIRED_FILES = [ ".translations/en.json", diff --git a/custom_components/fpl/fplDataUpdateCoordinator.py b/custom_components/fpl/fplDataUpdateCoordinator.py index e4056ac..5679cbf 100644 --- a/custom_components/fpl/fplDataUpdateCoordinator.py +++ b/custom_components/fpl/fplDataUpdateCoordinator.py @@ -7,7 +7,7 @@ from datetime import timedelta from .fplapi import FplApi from .const import DOMAIN -SCAN_INTERVAL = timedelta(seconds=7200) +SCAN_INTERVAL = timedelta(seconds=1200) _LOGGER: logging.Logger = logging.getLogger(__package__) diff --git a/custom_components/fpl/fplEntity.py b/custom_components/fpl/fplEntity.py index 7b30d27..c19937a 100644 --- a/custom_components/fpl/fplEntity.py +++ b/custom_components/fpl/fplEntity.py @@ -19,16 +19,13 @@ class FplEntity(CoordinatorEntity): ) return id - @property - def name(self): - return f"{DOMAIN.upper()} {self.account} {self.sensorName}" - @property def device_info(self): return { "identifiers": {(DOMAIN, self.account)}, - "name": f"Account {self.account}", - "model": VERSION, + "name": f"FPL Account {self.account}", + "model": "FPL Monitoring API", + "sw_version": VERSION, "manufacturer": "Florida Power & Light", } diff --git a/custom_components/fpl/fplapi.py b/custom_components/fpl/fplapi.py index 3265f81..4790cd0 100644 --- a/custom_components/fpl/fplapi.py +++ b/custom_components/fpl/fplapi.py @@ -300,6 +300,9 @@ class FplApi(object): "cost": daily["billingCharge"], "date": daily["date"], "max_temperature": daily["averageHighTemperature"], + "netDeliveredKwh": daily["netDeliveredKwh"], + "netReceivedKwh": daily["netReceivedKwh"], + "readTime": daily["readTime"], } ) # totalPowerUsage += int(daily["kwhUsed"]) @@ -312,6 +315,7 @@ class FplApi(object): data["billToDateKWH"] = r["CurrentUsage"]["billToDateKWH"] data["recMtrReading"] = r["CurrentUsage"]["recMtrReading"] data["delMtrReading"] = r["CurrentUsage"]["delMtrReading"] + data["billStartDate"] = r["CurrentUsage"]["billStartDate"] return data async def __getDataFromApplianceUsage(self, account, lastBilledDate) -> dict: diff --git a/custom_components/fpl/manifest.json b/custom_components/fpl/manifest.json index 805319d..4993feb 100644 --- a/custom_components/fpl/manifest.json +++ b/custom_components/fpl/manifest.json @@ -12,6 +12,6 @@ "bs4", "integrationhelper" ], - "homeassistant": "0.96.0", + "homeassistant": "2021.12.7", "version": "1.0.0" } \ No newline at end of file diff --git a/custom_components/fpl/sensor.py b/custom_components/fpl/sensor.py index a868e1e..6b2c85d 100644 --- a/custom_components/fpl/sensor.py +++ b/custom_components/fpl/sensor.py @@ -23,9 +23,14 @@ from .sensor_ProjectedBillSensor import ( from .sensor_AverageDailySensor import ( FplAverageDailySensor, BudgetDailyAverageSensor, - ActualDailyAverageSensor, ) -from .sensor_DailyUsageSensor import FplDailyUsageKWHSensor, FplDailyUsageSensor +from .sensor_DailyUsageSensor import ( + FplDailyUsageKWHSensor, + FplDailyUsageSensor, + FplDailyDeliveredKWHSensor, + FplDailyReceivedKWHSensor, +) + from .const import DOMAIN from .sensor_AllData import AllDataSensor @@ -54,10 +59,10 @@ async def async_setup_entry(hass, entry, async_add_devices): # usage sensors fpl_accounts.append(FplAverageDailySensor(coordinator, entry, account)) fpl_accounts.append(BudgetDailyAverageSensor(coordinator, entry, account)) - fpl_accounts.append(ActualDailyAverageSensor(coordinator, entry, account)) - fpl_accounts.append(FplDailyUsageSensor(coordinator, entry, account)) fpl_accounts.append(FplDailyUsageKWHSensor(coordinator, entry, account)) + fpl_accounts.append(FplDailyReceivedKWHSensor(coordinator, entry, account)) + fpl_accounts.append(FplDailyDeliveredKWHSensor(coordinator, entry, account)) # date sensors fpl_accounts.append(CurrentBillDateSensor(coordinator, entry, account)) @@ -70,5 +75,8 @@ async def async_setup_entry(hass, entry, async_add_devices): fpl_accounts.append(ProjectedKWHSensor(coordinator, entry, account)) fpl_accounts.append(DailyAverageKWHSensor(coordinator, entry, account)) fpl_accounts.append(BillToDateKWHSensor(coordinator, entry, account)) + fpl_accounts.append(NetReceivedKWHSensor(coordinator, entry, account)) + fpl_accounts.append(NetDeliveredKWHSensor(coordinator, entry, account)) + async_add_devices(fpl_accounts) diff --git a/custom_components/fpl/sensor_AllData.py b/custom_components/fpl/sensor_AllData.py index 98044b3..d8395fd 100644 --- a/custom_components/fpl/sensor_AllData.py +++ b/custom_components/fpl/sensor_AllData.py @@ -13,12 +13,21 @@ class AllDataSensor(FplEntity): if budget == True and budget_billing_projected_bill is not None: return self.getData("budget_billing_projected_bill") - return self.getData("projected_bill") - - def defineAttributes(self): - """Return the state attributes.""" - return self.coordinator.data.get(self.account) + try: + self._state=self.getData("projected_bill") + except: + pass + return self._state @property def icon(self): return "mdi:currency-usd" + + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Budget Projected Bill" + 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 66d25f1..2b40f75 100644 --- a/custom_components/fpl/sensor_AverageDailySensor.py +++ b/custom_components/fpl/sensor_AverageDailySensor.py @@ -13,12 +13,24 @@ class FplAverageDailySensor(FplEntity): if budget == True and budget_billing_projected_bill is not None: return self.getData("budget_billing_daily_avg") - return self.getData("daily_avg") + try: + self._state=self.getData("daily_avg") + except: + pass + return self._state @property def icon(self): return "mdi:currency-usd" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Daily Average" + attributes["device_class"] = "monetary" + attributes["state_class"] = "total" + attributes["unit_of_measurement"] = "$" + return attributes class BudgetDailyAverageSensor(FplEntity): def __init__(self, coordinator, config, account): @@ -26,21 +38,22 @@ class BudgetDailyAverageSensor(FplEntity): @property def state(self): - return self.getData("budget_billing_daily_avg") + try: + self._state= self.getData("budget_billing_daily_avg") + except: + pass + return self._state @property def icon(self): return "mdi:currency-usd" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Budget Daily Average" + attributes["device_class"] = "monitary" + attributes["state_class"] = "total" + attributes["unit_of_measurement"] = "$" + return attributes -class ActualDailyAverageSensor(FplEntity): - 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..98cdeb5 100644 --- a/custom_components/fpl/sensor_DailyUsageSensor.py +++ b/custom_components/fpl/sensor_DailyUsageSensor.py @@ -9,19 +9,34 @@ class FplDailyUsageSensor(FplEntity): def state(self): data = self.getData("daily_usage") - if len(data) > 0: - return data[-1]["cost"] - - return None + try: + self._state = data[-1]["cost"] + except: + pass + return self._state def defineAttributes(self): """Return the state attributes.""" data = self.getData("daily_usage") - - if len(data) > 0: - return {"date": data[-1]["date"]} - - return {} + attributes = {} + attributes["friendly_name"] = "Daily Usage" + attributes["device_class"] = "monetary" + attributes["state_class"] = "total_increasing" + attributes["unit_of_measurement"] = "$" + if data is not None: + if ( + (len(data) > 0) + and (data[-1] is not None) + and (data[-1]["readTime"] is not None) + ): + attributes["date"] = data[-1]["readTime"] + if ( + (len(data) > 1) + and (data[-2] is not None) + and (data[-2]["readTime"] is not None) + ): + attributes["last_reset"] = data[-2]["readTime"] + return attributes @property def icon(self): @@ -36,20 +51,122 @@ class FplDailyUsageKWHSensor(FplEntity): def state(self): data = self.getData("daily_usage") - if len(data) > 0: - return data[-1]["usage"] - - return None + try: + self._state = data[-1]["usage"] + except: + pass + return self._state def defineAttributes(self): """Return the state attributes.""" data = self.getData("daily_usage") - if len(data) > 0: - return {"date": data[-1]["date"]} + attributes = {} + attributes["friendly_name"] = "Daily Usage" + attributes["device_class"] = "energy" + attributes["state_class"] = "total_increasing" + attributes["unit_of_measurement"] = "kWh" - return {} + if data is not None: + if ( + (len(data) > 0) + and (data[-1] is not None) + and (data[-1]["readTime"] is not None) + ): + attributes["date"] = data[-1]["readTime"] + if ( + (len(data) > 1) + and (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:currency-usd" + return "mdi:flash" + + +class FplDailyReceivedKWHSensor(FplEntity): + def __init__(self, coordinator, config, account): + super().__init__(coordinator, config, account, "Daily Received KWH") + + @property + def state(self): + data = self.getData("daily_usage") + try: + self._state = data[-1]["netReceivedKwh"] + except: + pass + return self._state + + def defineAttributes(self): + """Return the state attributes.""" + data = self.getData("daily_usage") + + attributes = {} + attributes["friendly_name"] = "Daily Return to Grid" + attributes["device_class"] = "energy" + attributes["state_class"] = "total_increasing" + attributes["unit_of_measurement"] = "kWh" + if data is not None: + if ( + (len(data) > 0) + and (data[-1] is not None) + and (data[-1]["readTime"] is not None) + ): + attributes["date"] = data[-1]["readTime"] + if ( + (len(data) > 1) + and (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") + + @property + def state(self): + data = self.getData("daily_usage") + try: + self._state = data[-1]["netDeliveredKwh"] + except: + pass + return self._state + + def defineAttributes(self): + """Return the state attributes.""" + data = self.getData("daily_usage") + + attributes = {} + attributes["friendly_name"] = "Daily Consumption" + attributes["device_class"] = "energy" + attributes["state_class"] = "total_increasing" + attributes["unit_of_measurement"] = "kWh" + if data is not None: + if ( + (len(data) > 0) + and (data[-1] is not None) + and (data[-1]["readTime"] is not None) + ): + attributes["date"] = data[-1]["readTime"] + if ( + (len(data) > 1) + and (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" diff --git a/custom_components/fpl/sensor_DatesSensor.py b/custom_components/fpl/sensor_DatesSensor.py index b0414d7..cfc05a9 100644 --- a/custom_components/fpl/sensor_DatesSensor.py +++ b/custom_components/fpl/sensor_DatesSensor.py @@ -1,66 +1,117 @@ from .fplEntity import FplEntity - +import datetime class CurrentBillDateSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "Current Bill Date") + super().__init__(coordinator, config, account, "Billing Current Date") @property def state(self): - return self.getData("current_bill_date") + try: + self._state= datetime.date.fromisoformat(self.getData("current_bill_date")) + except: + pass + return self._state @property def icon(self): return "mdi:calendar" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["device_class"] = "date" + attributes["friendly_name"] = "Billing Current" + return attributes class NextBillDateSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "Next Bill Date") + super().__init__(coordinator, config, account, "Billing Next") @property def state(self): - return self.getData("next_bill_date") + try: + self._state= datetime.date.fromisoformat(self.getData("next_bill_date")) + except: + pass + return self._state @property def icon(self): return "mdi:calendar" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["device_class"] = "date" + attributes["friendly_name"] = "Billing Next" + return attributes class ServiceDaysSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "Service Days") + super().__init__(coordinator, config, account, "Billing Total Days") @property def state(self): - return self.getData("service_days") + try: + self._state= self.getData("service_days") + except: + pass + return self._state @property def icon(self): return "mdi:calendar" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["unit_of_measurement"] = "days" + attributes["friendly_name"] = "Billing Total" + return attributes class AsOfDaysSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "As Of Days") + super().__init__(coordinator, config, account, "Billing As Of") @property def state(self): - return self.getData("as_of_days") + try: + self._state= self.getData("as_of_days") + except: + pass + return self._state @property def icon(self): return "mdi:calendar" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["unit_of_measurement"] = "days" + attributes["friendly_name"] = "Billing As Of" + return attributes class RemainingDaysSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "Remaining Days") + super().__init__(coordinator, config, account, "Billing Remaining") @property def state(self): - return self.getData("remaining_days") + try: + self._state= self.getData("remaining_days") + except: + pass + return self._state @property def icon(self): return "mdi:calendar" + + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["unit_of_measurement"] = "days" + attributes["friendly_name"] = "Billing Remaining" + return attributes diff --git a/custom_components/fpl/sensor_KWHSensor.py b/custom_components/fpl/sensor_KWHSensor.py index ba2d24a..965d541 100644 --- a/custom_components/fpl/sensor_KWHSensor.py +++ b/custom_components/fpl/sensor_KWHSensor.py @@ -3,62 +3,140 @@ from .fplEntity import FplEntity class ProjectedKWHSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "Projected KWH") + super().__init__(coordinator, config, account, "Projected") @property def state(self): - return self.getData("projectedKWH") + try: + self._state = self.getData("projectedKWH") + except: + pass + return self._state @property def icon(self): return "mdi:flash" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Projected KWH" + attributes["device_class"] = "energy" + attributes["state_class"] = "total" + attributes["unit_of_measurement"] = "kWh" + return attributes + class DailyAverageKWHSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "Daily Average KWH") + super().__init__(coordinator, config, account, "Daily Average") @property def state(self): - return self.getData("dailyAverageKWH") + try: + self._state = self.getData("dailyAverageKWH") + except: + pass + return self._state @property def icon(self): return "mdi:flash" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Daily Average" + attributes["device_class"] = "energy" + attributes["state_class"] = "total" + attributes["unit_of_measurement"] = "kWh" + return attributes + class BillToDateKWHSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "Bill To Date KWH") + super().__init__(coordinator, config, account, "Bill To Date") @property def state(self): - return self.getData("billToDateKWH") + try: + self._state = self.getData("billToDateKWH") + except: + pass + return self._state @property def icon(self): return "mdi:flash" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Billing Usage" + attributes["device_class"] = "energy" + attributes["state_class"] = "total_increasing" + attributes["unit_of_measurement"] = "kWh" + if self.getData("billStartDate") is not None: + attributes["last_reset"] = self.getData("billStartDate") + return attributes + + class NetReceivedKWHSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "Received Meter Reading KWH") + super().__init__(coordinator, config, account, "Received Reading") @property def state(self): - return self.getData("recMtrReading") + try: + self._state = self.getData("recMtrReading") + except: + pass + return self._state @property def icon(self): return "mdi:flash" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Meter Return to Grid" + attributes["device_class"] = "energy" + attributes["state_class"] = "total_increasing" + attributes["unit_of_measurement"] = "kWh" + if self.getData("billStartDate") is not None: + attributes["last_reset"] = self.getData("billStartDate") + + return attributes + + class NetDeliveredKWHSensor(FplEntity): def __init__(self, coordinator, config, account): - super().__init__(coordinator, config, account, "Delivered Meter Reading KWH") + super().__init__(coordinator, config, account, "Delivered Reading") @property def state(self): - return self.getData("delMtrReading") + try: + self._state = self.getData("delMtrReading") + except: + try: + self._state = self.getData("billToDateKWH") + except: + pass + return self._state @property def icon(self): return "mdi:flash" + + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Meter Consumption" + attributes["device_class"] = "energy" + attributes["state_class"] = "total_increasing" + attributes["unit_of_measurement"] = "kWh" + if self.getData("billStartDate") is not None: + attributes["last_reset"] = self.getData("billStartDate") + + return attributes diff --git a/custom_components/fpl/sensor_ProjectedBillSensor.py b/custom_components/fpl/sensor_ProjectedBillSensor.py index acf7482..524db5c 100644 --- a/custom_components/fpl/sensor_ProjectedBillSensor.py +++ b/custom_components/fpl/sensor_ProjectedBillSensor.py @@ -10,20 +10,22 @@ class FplProjectedBillSensor(FplEntity): budget = self.getData("budget_bill") budget_billing_projected_bill = self.getData("budget_billing_projected_bill") - if budget == True and budget_billing_projected_bill is not None: - return self.getData("budget_billing_projected_bill") - - return self.getData("projected_bill") + try: + if budget == True and budget_billing_projected_bill is not None: + self._state = self.getData("budget_billing_projected_bill") + else: + self._state = self.getData("projected_bill") + except: + self._state = None + return self._state def defineAttributes(self): """Return the state attributes.""" attributes = {} - try: - if self.getData("budget_bill") == True: - attributes["budget_bill"] = self.getData("budget_bill") - except: - pass - + attributes["friendly_name"] = "Projected Bill Payment Due" + attributes["device_class"] = "monetary" + attributes["state_class"] = "total" + attributes["unit_of_measurement"] = "$" return attributes @property @@ -34,18 +36,30 @@ 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("budget_bill") == True: - return self.getData("defered_amount") - return 0 + try: + self._state = self.getData("deferred_amount") + except: + self._state = 0 + pass + return self._state @property def icon(self): return "mdi:currency-usd" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Deferred Amount" + attributes["device_class"] = "monetary" + attributes["state_class"] = "total" + attributes["unit_of_measurement"] = "$" + return attributes + class ProjectedBudgetBillSensor(FplEntity): def __init__(self, coordinator, config, account): @@ -53,12 +67,25 @@ class ProjectedBudgetBillSensor(FplEntity): @property def state(self): - return self.getData("budget_billing_projected_bill") + try: + self._state = self.getData("budget_billing_projected_bill") + except: + pass + return self._state @property def icon(self): return "mdi:currency-usd" + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Projected Budget Bill" + attributes["device_class"] = "monitary" + attributes["state_class"] = "total" + attributes["unit_of_measurement"] = "$" + return attributes + class ProjectedActualBillSensor(FplEntity): def __init__(self, coordinator, config, account): @@ -66,8 +93,22 @@ class ProjectedActualBillSensor(FplEntity): @property def state(self): - return self.getData("projected_bill") + try: + self._state = self.getData("projected_bill") + except: + pass + return self._state @property def icon(self): return "mdi:currency-usd" + + def defineAttributes(self): + """Return the state attributes.""" + attributes = {} + attributes["friendly_name"] = "Projected Actual Bill" + attributes["device_class"] = "monitary" + attributes["state_class"] = "total" + attributes["unit_of_measurement"] = "$" + + return attributes