Skip to content

Commit 3b2fa25

Browse files
authored
Merge pull request #58 from alexandrocampos/master
HA 2025.1.1 Version Broke The Connection to FPL #57
2 parents 7b76ee9 + 732f80c commit 3b2fa25

8 files changed

+284
-312
lines changed

custom_components/fpl/fplEntity.py

+31-24
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
"""Fpl Entity class"""
2-
32
from datetime import datetime, timedelta
43

5-
from homeassistant.components.sensor import SensorEntity, STATE_CLASS_MEASUREMENT
4+
from homeassistant.components.sensor import (
5+
SensorEntity,
6+
SensorDeviceClass,
7+
SensorStateClass, # Imported if you need to set _attr_state_class
8+
)
69
from homeassistant.helpers.update_coordinator import CoordinatorEntity
710

11+
# Updated import for kWh unit:
812
from homeassistant.const import (
913
CURRENCY_DOLLAR,
10-
DEVICE_CLASS_ENERGY,
11-
ENERGY_KILO_WATT_HOUR,
12-
DEVICE_CLASS_MONETARY,
14+
UnitOfEnergy,
1315
)
16+
1417
from .const import DOMAIN, VERSION, ATTRIBUTION
1518

1619

@@ -33,73 +36,77 @@ def unique_id(self):
3336

3437
@property
3538
def name(self):
39+
"""Return a friendly name for this entity."""
3640
return f"{DOMAIN.upper()} {self.account} {self.sensorName}"
3741

3842
@property
3943
def device_info(self):
44+
"""Return device information for this entity."""
4045
return {
4146
"identifiers": {(DOMAIN, self.account)},
4247
"name": f"FPL {self.account}",
4348
"model": VERSION,
4449
"manufacturer": "Florida Power & Light",
45-
"configuration_url":"https://www.fpl.com/my-account/residential-dashboard.html"
50+
"configuration_url": "https://www.fpl.com/my-account/residential-dashboard.html",
4651
}
4752

4853
def customAttributes(self) -> dict:
49-
"""override this method to set custom attributes"""
54+
"""Override this method in child classes to add custom attributes."""
5055
return {}
5156

5257
@property
5358
def extra_state_attributes(self):
5459
"""Return the state attributes."""
55-
attributes = {
56-
"attribution": ATTRIBUTION,
57-
# "integration": "FPL",
58-
}
60+
attributes = {"attribution": ATTRIBUTION}
5961
attributes.update(self.customAttributes())
6062
return attributes
6163

6264
def getData(self, field):
63-
"""call this method to retrieve sensor data"""
65+
"""Get data from the coordinator for this sensor."""
6466
if self.coordinator.data is not None:
65-
account = self.coordinator.data.get(self.account)
66-
if account is not None:
67-
return account.get(field, None)
67+
account_data = self.coordinator.data.get(self.account)
68+
if account_data is not None:
69+
return account_data.get(field, None)
6870
return None
6971

7072

7173
class FplEnergyEntity(FplEntity):
72-
"""Represents a energy sensor"""
74+
"""Represents an energy sensor (in kWh)."""
7375

74-
_attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR
75-
# _attr_device_class = DEVICE_CLASS_ENERGY
76+
_attr_device_class = SensorDeviceClass.ENERGY
77+
# Switch from ENERGY_KILO_WATT_HOUR to UnitOfEnergy.KILO_WATT_HOUR
78+
_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
7679
_attr_icon = "mdi:flash"
7780

7881
@property
7982
def last_reset_not_use(self) -> datetime:
80-
"""Return the time when the sensor was last reset, if any."""
81-
83+
"""
84+
Example method if you ever need a daily reset time.
85+
Not typically used in the modern approach, but left here
86+
if you'd like to implement older style sensor resets.
87+
"""
8288
today = datetime.today()
8389
yesterday = today - timedelta(days=1)
8490
return datetime.combine(yesterday, datetime.min.time())
8591

8692

8793
class FplMoneyEntity(FplEntity):
88-
"""Represents a money sensor"""
94+
"""Represents a money sensor (in dollars)."""
8995

96+
# If you prefer "USD" as the native unit, just replace CURRENCY_DOLLAR with "USD".
9097
_attr_native_unit_of_measurement = CURRENCY_DOLLAR
91-
_attr_device_class = DEVICE_CLASS_MONETARY
98+
_attr_device_class = SensorDeviceClass.MONETARY
9299
_attr_icon = "mdi:currency-usd"
93100

94101

95102
class FplDateEntity(FplEntity):
96-
"""Represents a date or days"""
103+
"""Represents a date-based sensor."""
97104

98105
_attr_icon = "mdi:calendar"
99106

100107

101108
class FplDayEntity(FplEntity):
102-
"""Represents a date or days"""
109+
"""Represents a sensor measured in days."""
103110

104111
_attr_native_unit_of_measurement = "days"
105112
_attr_icon = "mdi:calendar"
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,73 @@
11
"""Average daily sensors"""
2+
from homeassistant.components.sensor import (
3+
SensorDeviceClass,
4+
SensorStateClass,
5+
)
26
from .fplEntity import FplMoneyEntity
37

48

59
class DailyAverageSensor(FplMoneyEntity):
6-
"""average daily sensor, use budget value if available, otherwise use actual daily values"""
10+
"""Average daily sensor, use budget value if available, otherwise use actual daily values"""
11+
12+
_attr_device_class = SensorDeviceClass.MONETARY
13+
_attr_state_class = SensorStateClass.MEASUREMENT
714

815
def __init__(self, coordinator, config, account):
916
super().__init__(coordinator, config, account, "Daily Average")
1017

1118
@property
1219
def native_value(self):
1320
daily_avg = self.getData("daily_avg")
14-
1521
if daily_avg is not None:
1622
self._attr_native_value = daily_avg
17-
1823
return self._attr_native_value
1924

2025
def customAttributes(self):
2126
"""Return the state attributes."""
27+
# Add any extra attributes you want to expose here
2228
attributes = {}
23-
# attributes["state_class"] = STATE_CLASS_TOTAL
2429
return attributes
2530

2631

2732
class BudgetDailyAverageSensor(FplMoneyEntity):
28-
"""budget daily average sensor"""
33+
"""Budget daily average sensor"""
34+
35+
_attr_device_class = SensorDeviceClass.MONETARY
36+
_attr_state_class = SensorStateClass.MEASUREMENT
2937

3038
def __init__(self, coordinator, config, account):
3139
super().__init__(coordinator, config, account, "Budget Daily Average")
3240

3341
@property
3442
def native_value(self):
3543
budget_billing_daily_avg = self.getData("budget_billing_daily_avg")
36-
3744
if budget_billing_daily_avg is not None:
3845
self._attr_native_value = budget_billing_daily_avg
39-
4046
return self._attr_native_value
4147

4248
def customAttributes(self):
4349
"""Return the state attributes."""
4450
attributes = {}
45-
# attributes["state_class"] = STATE_CLASS_TOTAL
4651
return attributes
4752

4853

4954
class ActualDailyAverageSensor(FplMoneyEntity):
5055
"""Actual daily average sensor"""
5156

57+
_attr_device_class = SensorDeviceClass.MONETARY
58+
_attr_state_class = SensorStateClass.MEASUREMENT
59+
5260
def __init__(self, coordinator, config, account):
5361
super().__init__(coordinator, config, account, "Actual Daily Average")
5462

5563
@property
5664
def native_value(self):
5765
daily_avg = self.getData("daily_avg")
58-
5966
if daily_avg is not None:
6067
self._attr_native_value = daily_avg
61-
6268
return self._attr_native_value
6369

6470
def customAttributes(self):
6571
"""Return the state attributes."""
6672
attributes = {}
67-
# attributes["state_class"] = STATE_CLASS_TOTAL
6873
return attributes

0 commit comments

Comments
 (0)