Skip to content

Commit

Permalink
Merge pull request #58 from alexandrocampos/master
Browse files Browse the repository at this point in the history
HA 2025.1.1 Version Broke The Connection to FPL #57
  • Loading branch information
dotKrad authored Jan 10, 2025
2 parents 7b76ee9 + 732f80c commit 3b2fa25
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 312 deletions.
55 changes: 31 additions & 24 deletions custom_components/fpl/fplEntity.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
"""Fpl Entity class"""

from datetime import datetime, timedelta

from homeassistant.components.sensor import SensorEntity, STATE_CLASS_MEASUREMENT
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass, # Imported if you need to set _attr_state_class
)
from homeassistant.helpers.update_coordinator import CoordinatorEntity

# Updated import for kWh unit:
from homeassistant.const import (
CURRENCY_DOLLAR,
DEVICE_CLASS_ENERGY,
ENERGY_KILO_WATT_HOUR,
DEVICE_CLASS_MONETARY,
UnitOfEnergy,
)

from .const import DOMAIN, VERSION, ATTRIBUTION


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

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

@property
def device_info(self):
"""Return device information for this entity."""
return {
"identifiers": {(DOMAIN, self.account)},
"name": f"FPL {self.account}",
"model": VERSION,
"manufacturer": "Florida Power & Light",
"configuration_url":"https://www.fpl.com/my-account/residential-dashboard.html"
"configuration_url": "https://www.fpl.com/my-account/residential-dashboard.html",
}

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

@property
def extra_state_attributes(self):
"""Return the state attributes."""
attributes = {
"attribution": ATTRIBUTION,
# "integration": "FPL",
}
attributes = {"attribution": ATTRIBUTION}
attributes.update(self.customAttributes())
return attributes

def getData(self, field):
"""call this method to retrieve sensor data"""
"""Get data from the coordinator for this sensor."""
if self.coordinator.data is not None:
account = self.coordinator.data.get(self.account)
if account is not None:
return account.get(field, None)
account_data = self.coordinator.data.get(self.account)
if account_data is not None:
return account_data.get(field, None)
return None


class FplEnergyEntity(FplEntity):
"""Represents a energy sensor"""
"""Represents an energy sensor (in kWh)."""

_attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR
# _attr_device_class = DEVICE_CLASS_ENERGY
_attr_device_class = SensorDeviceClass.ENERGY
# Switch from ENERGY_KILO_WATT_HOUR to UnitOfEnergy.KILO_WATT_HOUR
_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
_attr_icon = "mdi:flash"

@property
def last_reset_not_use(self) -> datetime:
"""Return the time when the sensor was last reset, if any."""

"""
Example method if you ever need a daily reset time.
Not typically used in the modern approach, but left here
if you'd like to implement older style sensor resets.
"""
today = datetime.today()
yesterday = today - timedelta(days=1)
return datetime.combine(yesterday, datetime.min.time())


class FplMoneyEntity(FplEntity):
"""Represents a money sensor"""
"""Represents a money sensor (in dollars)."""

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


class FplDateEntity(FplEntity):
"""Represents a date or days"""
"""Represents a date-based sensor."""

_attr_icon = "mdi:calendar"


class FplDayEntity(FplEntity):
"""Represents a date or days"""
"""Represents a sensor measured in days."""

_attr_native_unit_of_measurement = "days"
_attr_icon = "mdi:calendar"
27 changes: 16 additions & 11 deletions custom_components/fpl/sensor_AverageDailySensor.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,73 @@
"""Average daily sensors"""
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorStateClass,
)
from .fplEntity import FplMoneyEntity


class DailyAverageSensor(FplMoneyEntity):
"""average daily sensor, use budget value if available, otherwise use actual daily values"""
"""Average daily sensor, use budget value if available, otherwise use actual daily values"""

_attr_device_class = SensorDeviceClass.MONETARY
_attr_state_class = SensorStateClass.MEASUREMENT

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

@property
def native_value(self):
daily_avg = self.getData("daily_avg")

if daily_avg is not None:
self._attr_native_value = daily_avg

return self._attr_native_value

def customAttributes(self):
"""Return the state attributes."""
# Add any extra attributes you want to expose here
attributes = {}
# attributes["state_class"] = STATE_CLASS_TOTAL
return attributes


class BudgetDailyAverageSensor(FplMoneyEntity):
"""budget daily average sensor"""
"""Budget daily average sensor"""

_attr_device_class = SensorDeviceClass.MONETARY
_attr_state_class = SensorStateClass.MEASUREMENT

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

@property
def native_value(self):
budget_billing_daily_avg = self.getData("budget_billing_daily_avg")

if budget_billing_daily_avg is not None:
self._attr_native_value = budget_billing_daily_avg

return self._attr_native_value

def customAttributes(self):
"""Return the state attributes."""
attributes = {}
# attributes["state_class"] = STATE_CLASS_TOTAL
return attributes


class ActualDailyAverageSensor(FplMoneyEntity):
"""Actual daily average sensor"""

_attr_device_class = SensorDeviceClass.MONETARY
_attr_state_class = SensorStateClass.MEASUREMENT

def __init__(self, coordinator, config, account):
super().__init__(coordinator, config, account, "Actual Daily Average")

@property
def native_value(self):
daily_avg = self.getData("daily_avg")

if daily_avg is not None:
self._attr_native_value = daily_avg

return self._attr_native_value

def customAttributes(self):
"""Return the state attributes."""
attributes = {}
# attributes["state_class"] = STATE_CLASS_TOTAL
return attributes
Loading

0 comments on commit 3b2fa25

Please sign in to comment.