From a784fd314a450018fc7931cd4a3d6e7e5b3d0508 Mon Sep 17 00:00:00 2001 From: Yordan Suarez Date: Sat, 23 Jul 2022 06:33:19 -0400 Subject: [PATCH] make use of _attr_ --- custom_components/fpl/fplEntity.py | 74 ++++--------------- .../fpl/sensor_DailyUsageSensor.py | 2 +- .../fpl/sensor_ProjectedBillSensor.py | 47 +++--------- 3 files changed, 29 insertions(+), 94 deletions(-) diff --git a/custom_components/fpl/fplEntity.py b/custom_components/fpl/fplEntity.py index 91b0553..ecb6728 100644 --- a/custom_components/fpl/fplEntity.py +++ b/custom_components/fpl/fplEntity.py @@ -17,6 +17,8 @@ from .const import DOMAIN, VERSION, ATTRIBUTION class FplEntity(CoordinatorEntity, SensorEntity): """FPL base entity""" + _attr_attribution = ATTRIBUTION + def __init__(self, coordinator, config_entry, account, sensorName): super().__init__(coordinator) self.config_entry = config_entry @@ -26,9 +28,8 @@ class FplEntity(CoordinatorEntity, SensorEntity): @property def unique_id(self): """Return the ID of this device.""" - return "{}{}{}".format( - DOMAIN, self.account, self.sensorName.lower().replace(" ", "") - ) + sensorName = self.sensorName.lower().replace(" ", "") + return f"{DOMAIN}{self.account}{sensorName}" @property def name(self): @@ -51,8 +52,8 @@ class FplEntity(CoordinatorEntity, SensorEntity): def extra_state_attributes(self): """Return the state attributes.""" attributes = { - "attribution": ATTRIBUTION, - "integration": "FPL", + # "attribution": ATTRIBUTION, + # "integration": "FPL", } attributes.update(self.customAttributes()) return attributes @@ -65,11 +66,10 @@ class FplEntity(CoordinatorEntity, SensorEntity): class FplEnergyEntity(FplEntity): """Represents a energy sensor""" - @property - def state_class(self) -> str: - """Return the state class of this entity, from STATE_CLASSES, if any.""" - - return STATE_CLASS_MEASUREMENT + _attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR + _attr_device_class = DEVICE_CLASS_ENERGY + _attr_icon = "mdi:flash" + _attr_state_class = STATE_CLASS_MEASUREMENT @property def last_reset(self) -> datetime: @@ -79,65 +79,23 @@ class FplEnergyEntity(FplEntity): 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): """Represents a money sensor""" - @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 + _attr_native_unit_of_measurement = CURRENCY_DOLLAR + _attr_device_class = DEVICE_CLASS_MONETARY + _attr_icon = "mdi:currency-usd" class FplDateEntity(FplEntity): """Represents a date or days""" - # @property - # def device_class(self) -> str: - # """Return the class of this device, from component DEVICE_CLASSES.""" - # return DEVICE_CLASS_DATE - - @property - def icon(self): - return "mdi:calendar" + _attr_icon = "mdi:calendar" class FplDayEntity(FplEntity): """Represents a date or days""" - # @property - # def device_class(self) -> str: - # """Return the class of this device, from component DEVICE_CLASSES.""" - # return DEVICE_CLASS_DATE - - @property - def icon(self): - return "mdi:calendar" - - @property - def unit_of_measurement(self) -> str: - """Return the unit of measurement of this entity, if any.""" - return "days" + _attr_native_unit_of_measurement = "days" + _attr_icon = "mdi:calendar" diff --git a/custom_components/fpl/sensor_DailyUsageSensor.py b/custom_components/fpl/sensor_DailyUsageSensor.py index e3888f7..de4a486 100644 --- a/custom_components/fpl/sensor_DailyUsageSensor.py +++ b/custom_components/fpl/sensor_DailyUsageSensor.py @@ -1,6 +1,6 @@ """Daily Usage Sensors""" -from homeassistant.components.sensor import STATE_CLASS_TOTAL_INCREASING from datetime import timedelta +from homeassistant.components.sensor import STATE_CLASS_TOTAL_INCREASING from .fplEntity import FplEnergyEntity, FplMoneyEntity diff --git a/custom_components/fpl/sensor_ProjectedBillSensor.py b/custom_components/fpl/sensor_ProjectedBillSensor.py index 264217a..e1d5762 100644 --- a/custom_components/fpl/sensor_ProjectedBillSensor.py +++ b/custom_components/fpl/sensor_ProjectedBillSensor.py @@ -7,7 +7,9 @@ from .fplEntity import FplMoneyEntity class FplProjectedBillSensor(FplMoneyEntity): - """projected bill sensor""" + """Projected bill sensor""" + + _attr_state_class = STATE_CLASS_TOTAL def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Projected Bill") @@ -22,22 +24,9 @@ class FplProjectedBillSensor(FplMoneyEntity): return self.getData("projected_bill") - """ - @property - def state(self): - budget = self.getData("budget_bill") - budget_billing_projected_bill = self.getData("budget_billing_projected_bill") - - if budget and budget_billing_projected_bill is not None: - return self.getData("budget_billing_projected_bill") - - return self.getData("projected_bill") - """ - def customAttributes(self): """Return the state attributes.""" attributes = {} - attributes["state_class"] = STATE_CLASS_TOTAL attributes["budget_bill"] = self.getData("budget_bill") return attributes @@ -46,51 +35,39 @@ class FplProjectedBillSensor(FplMoneyEntity): class DeferedAmountSensor(FplMoneyEntity): """Defered amount sensor""" + _attr_state_class = STATE_CLASS_TOTAL + def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Defered Amount") @property - def state(self): + def native_value(self): if self.getData("budget_bill"): return self.getData("defered_amount") return 0 - def customAttributes(self): - """Return the state attributes.""" - attributes = {} - attributes["state_class"] = STATE_CLASS_TOTAL - return attributes - class ProjectedBudgetBillSensor(FplMoneyEntity): """projected budget bill sensor""" + _attr_state_class = STATE_CLASS_TOTAL + def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Projected Budget Bill") @property - def state(self): + def native_value(self): return self.getData("budget_billing_projected_bill") - def customAttributes(self): - """Return the state attributes.""" - attributes = {} - attributes["state_class"] = STATE_CLASS_TOTAL - return attributes - class ProjectedActualBillSensor(FplMoneyEntity): """projeted actual bill sensor""" + _attr_state_class = STATE_CLASS_TOTAL + def __init__(self, coordinator, config, account): super().__init__(coordinator, config, account, "Projected Actual Bill") @property - def state(self): + def native_value(self): return self.getData("projected_bill") - - def customAttributes(self): - """Return the state attributes.""" - attributes = {} - attributes["state_class"] = STATE_CLASS_TOTAL - return attributes