Skip to content

Commit

Permalink
Scale energy sensor values by a factor of 10 (#248)
Browse files Browse the repository at this point in the history
* Scale energy sensor values by a factor of 10

* Only scale energy values when using alternate format
  • Loading branch information
mill1000 authored Sep 30, 2024
1 parent 899a47e commit 6e3e492
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions custom_components/midea_ac/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ async def async_setup_entry(
SensorDeviceClass.ENERGY,
UnitOfEnergy.KILO_WATT_HOUR,
"total_energy_usage",
state_class=SensorStateClass.TOTAL),
state_class=SensorStateClass.TOTAL,
scale=.1 if coordinator.device.use_alternate_energy_format else 1.0),
MideaEnergySensor(coordinator,
"current_energy_usage",
SensorDeviceClass.ENERGY,
UnitOfEnergy.KILO_WATT_HOUR,
"current_energy_usage",
state_class=SensorStateClass.TOTAL_INCREASING),
state_class=SensorStateClass.TOTAL_INCREASING,
scale=.1 if coordinator.device.use_alternate_energy_format else 1.0),
MideaEnergySensor(coordinator,
"real_time_power_usage",
SensorDeviceClass.POWER,
Expand Down Expand Up @@ -83,14 +85,16 @@ def __init__(self,
unit: str,
translation_key: Optional[str] = None,
*,
state_class: SensorStateClass = SensorStateClass.MEASUREMENT) -> None:
state_class: SensorStateClass = SensorStateClass.MEASUREMENT,
scale: float = 1.0) -> None:
MideaCoordinatorEntity.__init__(self, coordinator)

self._prop = prop
self._device_class = device_class
self._state_class = state_class
self._unit = unit
self._attr_translation_key = translation_key
self._scale = scale

@property
def device_info(self) -> dict:
Expand Down Expand Up @@ -136,7 +140,12 @@ def native_unit_of_measurement(self) -> str:
@property
def native_value(self) -> float | None:
"""Return the current native value."""
return getattr(self._device, self._prop, None)
value = getattr(self._device, self._prop, None)

if value is None:
return None

return value * self._scale


class MideaEnergySensor(MideaSensor):
Expand Down

0 comments on commit 6e3e492

Please sign in to comment.