mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Use unit enums in saj (#83818)
This commit is contained in:
parent
a48741d891
commit
87d24d4cbe
@ -19,13 +19,12 @@ from homeassistant.const import (
|
|||||||
CONF_PASSWORD,
|
CONF_PASSWORD,
|
||||||
CONF_TYPE,
|
CONF_TYPE,
|
||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
ENERGY_KILO_WATT_HOUR,
|
|
||||||
EVENT_HOMEASSISTANT_STOP,
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
MASS_KILOGRAMS,
|
UnitOfEnergy,
|
||||||
POWER_WATT,
|
UnitOfMass,
|
||||||
TEMP_CELSIUS,
|
UnitOfPower,
|
||||||
TEMP_FAHRENHEIT,
|
UnitOfTemperature,
|
||||||
TIME_HOURS,
|
UnitOfTime,
|
||||||
)
|
)
|
||||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
@ -44,11 +43,11 @@ INVERTER_TYPES = ["ethernet", "wifi"]
|
|||||||
|
|
||||||
SAJ_UNIT_MAPPINGS = {
|
SAJ_UNIT_MAPPINGS = {
|
||||||
"": None,
|
"": None,
|
||||||
"h": TIME_HOURS,
|
"h": UnitOfTime.HOURS,
|
||||||
"kg": MASS_KILOGRAMS,
|
"kg": UnitOfMass.KILOGRAMS,
|
||||||
"kWh": ENERGY_KILO_WATT_HOUR,
|
"kWh": UnitOfEnergy.KILO_WATT_HOUR,
|
||||||
"W": POWER_WATT,
|
"W": UnitOfPower.WATT,
|
||||||
"°C": TEMP_CELSIUS,
|
"°C": UnitOfTemperature.CELSIUS,
|
||||||
}
|
}
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
@ -191,7 +190,7 @@ class SAJsensor(SensorEntity):
|
|||||||
self._attr_state_class = SensorStateClass.TOTAL_INCREASING
|
self._attr_state_class = SensorStateClass.TOTAL_INCREASING
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self) -> str:
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
if self._inverter_name:
|
if self._inverter_name:
|
||||||
return f"saj_{self._inverter_name}_{self._sensor.name}"
|
return f"saj_{self._inverter_name}_{self._sensor.name}"
|
||||||
@ -204,19 +203,23 @@ class SAJsensor(SensorEntity):
|
|||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_unit_of_measurement(self):
|
def native_unit_of_measurement(self) -> str | None:
|
||||||
"""Return the unit the value is expressed in."""
|
"""Return the unit the value is expressed in."""
|
||||||
return SAJ_UNIT_MAPPINGS[self._sensor.unit]
|
return SAJ_UNIT_MAPPINGS[self._sensor.unit]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self) -> SensorDeviceClass | None:
|
||||||
"""Return the device class the sensor belongs to."""
|
"""Return the device class the sensor belongs to."""
|
||||||
if self.native_unit_of_measurement == POWER_WATT:
|
if self.native_unit_of_measurement == UnitOfPower.WATT:
|
||||||
return SensorDeviceClass.POWER
|
return SensorDeviceClass.POWER
|
||||||
if self.native_unit_of_measurement == ENERGY_KILO_WATT_HOUR:
|
if self.native_unit_of_measurement == UnitOfEnergy.KILO_WATT_HOUR:
|
||||||
return SensorDeviceClass.ENERGY
|
return SensorDeviceClass.ENERGY
|
||||||
if self.native_unit_of_measurement in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
|
if self.native_unit_of_measurement in (
|
||||||
|
UnitOfTemperature.CELSIUS,
|
||||||
|
UnitOfTemperature.FAHRENHEIT,
|
||||||
|
):
|
||||||
return SensorDeviceClass.TEMPERATURE
|
return SensorDeviceClass.TEMPERATURE
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def per_day_basis(self) -> bool:
|
def per_day_basis(self) -> bool:
|
||||||
@ -250,6 +253,6 @@ class SAJsensor(SensorEntity):
|
|||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self) -> str:
|
||||||
"""Return a unique identifier for this sensor."""
|
"""Return a unique identifier for this sensor."""
|
||||||
return f"{self._serialnumber}_{self._sensor.name}"
|
return f"{self._serialnumber}_{self._sensor.name}"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user