Skip to content

Commit

Permalink
Merge pull request #366 from ic-dev21/Energy-sensor-cleanup
Browse files Browse the repository at this point in the history
Energy sensor cleanup
  • Loading branch information
valleedelisle authored Jan 31, 2024
2 parents 938ac8f + dcaf38a commit 61e35db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
13 changes: 9 additions & 4 deletions custom_components/hilo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,19 @@ def fix_utility_sensor(self, entity, state):
if not attrs.get("source"):
LOG.debug(f"No source entity defined on {entity}: {current_state}")
return
parent_unit = self._hass.states.get(attrs.get("source"))

parent_unit_state = self._hass.states.get(attrs.get("source"))
parent_unit = (
"kWh"
if parent_unit_state is None
else parent_unit_state.attributes.get("unit_of_measurement")
)
if not parent_unit:
LOG.warning(f"Unable to find state for parent unit: {current_state}")
return

new_attrs = {
ATTR_UNIT_OF_MEASUREMENT: parent_unit.as_dict()
.get("attributes", {})
.get(ATTR_UNIT_OF_MEASUREMENT),
ATTR_UNIT_OF_MEASUREMENT: parent_unit, # note ic-dev21: now uses parent_unit directly
ATTR_DEVICE_CLASS: SensorDeviceClass.ENERGY,
}
if not all(a in attrs.keys() for a in new_attrs.keys()):
Expand Down
27 changes: 18 additions & 9 deletions custom_components/hilo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,22 @@ class EnergySensor(IntegrationSensor):
_attr_device_class = SensorDeviceClass.ENERGY
_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
_attr_state_class = SensorStateClass.TOTAL_INCREASING
_attr_suggested_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
_attr_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
_attr_icon = "mdi:lightning-bolt"

def __init__(self, device):
self._device = device
self._attr_name = f"Hilo Energy {slugify(device.name)}"
self._attr_unique_id = f"hilo_energy_{slugify(device.name)}"
self._unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
self._unit_prefix = None
if device.type == "Meter":
self._attr_name = HILO_ENERGY_TOTAL
self._unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
if device.type == "Thermostat" or device.type == "FloorThermostat":
self._unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
self._source = f"sensor.{slugify(device.name)}_power"
# ic-dev21: Set initial state and last_valid_state, removes log errors and unavailable states
initial_state = 0
self._attr_native_value = initial_state
self._attr_last_valid_state = initial_state
self._attr_last_reset = dt_util.utcnow()

super().__init__(
integration_method=METHOD_LEFT,
Expand All @@ -265,15 +267,13 @@ def __init__(self, device):
unit_prefix="k",
unit_time="h",
)
self._state = 0
self._last_period = 0
LOG.debug(
f"Setting up EnergySensor entity: {self._attr_name} with source {self._source}"
)

@property
def unit_of_measurement(self):
return self._unit_of_measurement
return self._attr_unit_of_measurement

async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
Expand Down Expand Up @@ -499,14 +499,23 @@ class HiloRewardSensor(HiloEntity, RestoreEntity, SensorEntity):

_attr_device_class = SensorDeviceClass.MONETARY
_attr_state_class = SensorStateClass.TOTAL_INCREASING
_attr_suggested_unit_of_measurement = "CAD"

def __init__(self, hilo, device, scan_interval):
self._attr_name = "Recompenses Hilo"

# Check if currency is configured, set a default if not
currency = hilo._hass.config.currency
if currency:
self._attr_native_unit_of_measurement = currency
else:
# Set a default currency or handle the case where currency is not configured
self._attr_native_unit_of_measurement = "CAD"

super().__init__(hilo, name=self._attr_name, device=device)
self._attr_unique_id = slugify(self._attr_name)
LOG.debug(f"Setting up RewardSensor entity: {self._attr_name}")
self.scan_interval = timedelta(seconds=REWARD_SCAN_INTERVAL)
self._attr_native_unit_of_measurement = hilo._hass.config.currency
self._state = 0
self._history = []
self.async_update = Throttle(self.scan_interval)(self._async_update)
Expand Down

0 comments on commit 61e35db

Please sign in to comment.