1
1
"""Fpl Entity class"""
2
-
3
2
from datetime import datetime , timedelta
4
3
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
+ )
6
9
from homeassistant .helpers .update_coordinator import CoordinatorEntity
7
10
11
+ # Updated import for kWh unit:
8
12
from homeassistant .const import (
9
13
CURRENCY_DOLLAR ,
10
- DEVICE_CLASS_ENERGY ,
11
- ENERGY_KILO_WATT_HOUR ,
12
- DEVICE_CLASS_MONETARY ,
14
+ UnitOfEnergy ,
13
15
)
16
+
14
17
from .const import DOMAIN , VERSION , ATTRIBUTION
15
18
16
19
@@ -33,73 +36,77 @@ def unique_id(self):
33
36
34
37
@property
35
38
def name (self ):
39
+ """Return a friendly name for this entity."""
36
40
return f"{ DOMAIN .upper ()} { self .account } { self .sensorName } "
37
41
38
42
@property
39
43
def device_info (self ):
44
+ """Return device information for this entity."""
40
45
return {
41
46
"identifiers" : {(DOMAIN , self .account )},
42
47
"name" : f"FPL { self .account } " ,
43
48
"model" : VERSION ,
44
49
"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" ,
46
51
}
47
52
48
53
def customAttributes (self ) -> dict :
49
- """override this method to set custom attributes"""
54
+ """Override this method in child classes to add custom attributes. """
50
55
return {}
51
56
52
57
@property
53
58
def extra_state_attributes (self ):
54
59
"""Return the state attributes."""
55
- attributes = {
56
- "attribution" : ATTRIBUTION ,
57
- # "integration": "FPL",
58
- }
60
+ attributes = {"attribution" : ATTRIBUTION }
59
61
attributes .update (self .customAttributes ())
60
62
return attributes
61
63
62
64
def getData (self , field ):
63
- """call this method to retrieve sensor data """
65
+ """Get data from the coordinator for this sensor. """
64
66
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 )
68
70
return None
69
71
70
72
71
73
class FplEnergyEntity (FplEntity ):
72
- """Represents a energy sensor"""
74
+ """Represents an energy sensor (in kWh). """
73
75
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
76
79
_attr_icon = "mdi:flash"
77
80
78
81
@property
79
82
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
+ """
82
88
today = datetime .today ()
83
89
yesterday = today - timedelta (days = 1 )
84
90
return datetime .combine (yesterday , datetime .min .time ())
85
91
86
92
87
93
class FplMoneyEntity (FplEntity ):
88
- """Represents a money sensor"""
94
+ """Represents a money sensor (in dollars). """
89
95
96
+ # If you prefer "USD" as the native unit, just replace CURRENCY_DOLLAR with "USD".
90
97
_attr_native_unit_of_measurement = CURRENCY_DOLLAR
91
- _attr_device_class = DEVICE_CLASS_MONETARY
98
+ _attr_device_class = SensorDeviceClass . MONETARY
92
99
_attr_icon = "mdi:currency-usd"
93
100
94
101
95
102
class FplDateEntity (FplEntity ):
96
- """Represents a date or days """
103
+ """Represents a date-based sensor. """
97
104
98
105
_attr_icon = "mdi:calendar"
99
106
100
107
101
108
class FplDayEntity (FplEntity ):
102
- """Represents a date or days"""
109
+ """Represents a sensor measured in days. """
103
110
104
111
_attr_native_unit_of_measurement = "days"
105
112
_attr_icon = "mdi:calendar"
0 commit comments