Skip to content

Commit

Permalink
Merge pull request #111 from tijsverkoyen/support-changing-unit-of-me…
Browse files Browse the repository at this point in the history
…asurement

Support changing unit of measurement
  • Loading branch information
tijsverkoyen authored Sep 20, 2023
2 parents d95aa23 + 09241f9 commit 09dca33
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 87 deletions.
12 changes: 2 additions & 10 deletions custom_components/fusion_solar/fusion_solar/energy_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def name(self) -> str:
return self._name

@property
def state(self) -> float:
def native_value(self) -> float:
# It seems like Huawei Fusion Solar returns some invalid data for the lifetime energy just before midnight
# Therefore we validate if the new value is higher than the current value
if ATTR_TOTAL_LIFETIME_ENERGY == self._attribute:
Expand Down Expand Up @@ -79,21 +79,13 @@ def state(self) -> float:
return float(self.coordinator.data[self._data_name][self._attribute])

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfEnergy.KILO_WATT_HOUR

@property
def state_class(self) -> str:
return SensorStateClass.TOTAL_INCREASING

@property
def native_value(self) -> str:
return self.state if self.state else ''

@property
def native_unit_of_measurement(self) -> str:
return self.unit_of_measurement

@property
def device_info(self) -> dict:
return self._device_info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def unique_id(self) -> str:
return f'{DOMAIN}-{self._station.code}-lifetime-{self._attribute}'

@property
def state(self) -> float:
def native_value(self) -> float:
key = f'{DOMAIN}-{self._station.code}'

if key not in self.coordinator.data:
Expand Down Expand Up @@ -60,7 +60,7 @@ def device_class(self) -> str:
return SensorDeviceClass.ENERGY

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfEnergy.KILO_WATT_HOUR

@property
Expand All @@ -80,7 +80,7 @@ def device_class(self) -> str:
return SensorDeviceClass.ENERGY

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfEnergy.KILO_WATT_HOUR

@property
Expand All @@ -100,7 +100,7 @@ def device_class(self) -> str:
return SensorDeviceClass.ENERGY

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfEnergy.KILO_WATT_HOUR

@property
Expand All @@ -115,14 +115,14 @@ class FusionSolarLifetimePlantDataPowerProfitSensor(FusionSolarLifetimePlantData
def name(self) -> str:
return f'{self._station.readable_name} - Lifetime - Revenue'

@property
def device_class(self) -> str:
return SensorDeviceClass.MONETARY

@property
def state_class(self) -> str:
return SensorStateClass.TOTAL_INCREASING

@property
def icon(self) -> str | None:
return "mdi:cash"


class FusionSolarLifetimePlantDataPerpowerRatioSensor(FusionSolarLifetimePlantDataSensor):
_attribute = 'perpower_ratio'
Expand All @@ -144,25 +144,21 @@ def name(self) -> str:
return f'{self._station.readable_name} - Lifetime - CO2 emission reduction'

@property
def device_class(self) -> str:
return SensorDeviceClass.WEIGHT

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfMass.KILOGRAMS

@property
def state_class(self) -> str:
return SensorStateClass.TOTAL_INCREASING

@property
def state(self) -> float:
super_state = super().state
def native_value(self) -> float:
native_value = super().native_value

if super_state is None:
if native_value is None:
return None

return super_state * 1000
return native_value * 1000

@property
def icon(self) -> str | None:
Expand All @@ -177,25 +173,25 @@ def name(self) -> str:
return f'{self._station.readable_name} - Lifetime - Standard coal saved'

@property
def device_class(self) -> str:
return SensorDeviceClass.WEIGHT

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfMass.KILOGRAMS

@property
def state_class(self) -> str:
return SensorStateClass.TOTAL_INCREASING

@property
def state(self) -> float:
super_state = super().state
def native_value(self) -> float:
native_value = super().native_value

if super_state is None:
if native_value is None:
return None

return super_state * 1000
return native_value * 1000

@property
def icon(self) -> str | None:
return "mdi:weight"


class FusionSolarLifetimePlantDataReductionTotalTreeSensor(FusionSolarLifetimePlantDataSensor):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def name(self) -> str:
return f'{self._device.name} - {self._name}'

@property
def state(self) -> float:
def native_value(self) -> float:
if self._state == '__NOT_INITIALIZED__':
# check if data is available
self._handle_coordinator_update()
Expand Down Expand Up @@ -95,7 +95,7 @@ def device_class(self) -> str:
return SensorDeviceClass.VOLTAGE

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfElectricPotential.VOLT

@property
Expand All @@ -109,7 +109,7 @@ def device_class(self) -> str:
return SensorDeviceClass.CURRENT

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfElectricCurrent.AMPERE

@property
Expand All @@ -123,7 +123,7 @@ def device_class(self) -> str:
return SensorDeviceClass.ENERGY

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfEnergy.KILO_WATT_HOUR

@property
Expand All @@ -143,7 +143,7 @@ def device_class(self) -> str:
return SensorDeviceClass.TEMPERATURE

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfTemperature.CELSIUS

@property
Expand All @@ -157,21 +157,21 @@ def device_class(self) -> str:
return SensorDeviceClass.POWER_FACTOR

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return PERCENTAGE

@property
def state_class(self) -> str:
return SensorStateClass.MEASUREMENT

@property
def state(self) -> str:
state = super().state
def native_value(self) -> str:
native_value = super().native_value

if state is None:
if native_value is None:
return None

return state * 100
return native_value * 100


class FusionSolarRealtimeDeviceDataFrequencySensor(FusionSolarRealtimeDeviceDataSensor):
Expand All @@ -180,7 +180,7 @@ def device_class(self) -> str:
return SensorDeviceClass.FREQUENCY

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfFrequency.HERTZ

@property
Expand All @@ -194,7 +194,7 @@ def device_class(self) -> str:
return SensorDeviceClass.POWER

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfPower.KILO_WATT

@property
Expand All @@ -204,28 +204,38 @@ def state_class(self) -> str:

class FusionSolarRealtimeDeviceDataPowerInWattSensor(FusionSolarRealtimeDeviceDataPowerSensor):
@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return UnitOfPower.WATT


class FusionSolarRealtimeDeviceDataReactivePowerSensor(FusionSolarRealtimeDeviceDataSensor):
@property
def device_class(self) -> str:
return 'reactive_power'
return SensorDeviceClass.REACTIVE_POWER

@property
def unit_of_measurement(self) -> str:
return 'kVar'
def native_unit_of_measurement(self) -> str:
return 'var'

@property
def state_class(self) -> str:
return SensorStateClass.MEASUREMENT

@property
def native_value(self) -> float:
native_value = super().native_value

if native_value is None:
return None

return native_value * 1000



class FusionSolarRealtimeDeviceDataReactivePowerInVarSensor(FusionSolarRealtimeDeviceDataReactivePowerSensor):
@property
def unit_of_measurement(self) -> str:
return 'Var'
def native_unit_of_measurement(self) -> str:
return 'var'


class FusionSolarRealtimeDeviceDataApparentPowerSensor(FusionSolarRealtimeDeviceDataSensor):
Expand All @@ -234,7 +244,7 @@ def device_class(self) -> str:
return 'apparent_power'

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return 'kVA'

@property
Expand All @@ -248,7 +258,7 @@ def device_class(self) -> str:
return 'wind_speed'

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return 'm/s'

@property
Expand All @@ -262,7 +272,7 @@ def device_class(self) -> str:
return SensorDeviceClass.BATTERY

@property
def unit_of_measurement(self) -> str:
def native_unit_of_measurement(self) -> str:
return PERCENTAGE

@property
Expand All @@ -277,7 +287,7 @@ def device_class(self) -> str:

@property
def state(self) -> datetime:
state = super().state
state = super().native_value

if state is None:
return None
Expand All @@ -287,7 +297,7 @@ def state(self) -> datetime:

class FusionSolarRealtimeDeviceDataPercentageSensor(FusionSolarRealtimeDeviceDataSensor):
@property
def unit_of_measurement(self) -> str | None:
def native_unit_of_measurement(self) -> str | None:
return PERCENTAGE

@property
Expand Down
Loading

0 comments on commit 09dca33

Please sign in to comment.