Skip to content

Commit

Permalink
Make switch, select and number translatable
Browse files Browse the repository at this point in the history
  • Loading branch information
wlcrs committed Oct 29, 2023
1 parent 140238b commit 6921c27
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 65 deletions.
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ async def _compute_device_infos(
name="Inverter",
manufacturer="Huawei",
model=bridge.model_name,
serial_number=bridge.serial_number,
via_device=connecting_inverter_device_id, # type: ignore[typeddict-item]
)

Expand Down
11 changes: 3 additions & 8 deletions number.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ class HuaweiSolarNumberEntityDescription(NumberEntityDescription):
dynamic_minimum_key: str | None = None
dynamic_maximum_key: str | None = None

def __post_init__(self):
"""Defaults the translation_key to the number key."""
self.translation_key = self.translation_key or self.key.replace('#','_').lower()

ENERGY_STORAGE_NUMBER_DESCRIPTIONS: tuple[HuaweiSolarNumberEntityDescription, ...] = (
HuaweiSolarNumberEntityDescription(
key=rn.STORAGE_MAXIMUM_CHARGING_POWER,
native_min_value=0,
static_maximum_key=rn.STORAGE_MAXIMUM_CHARGE_POWER,
name="Maximum charging power",
icon="mdi:battery-positive",
native_unit_of_measurement=POWER_WATT,
entity_category=EntityCategory.CONFIG,
Expand All @@ -56,7 +58,6 @@ class HuaweiSolarNumberEntityDescription(NumberEntityDescription):
key=rn.STORAGE_MAXIMUM_DISCHARGING_POWER,
native_min_value=0,
static_maximum_key=rn.STORAGE_MAXIMUM_DISCHARGE_POWER,
name="Maximum discharging power",
icon="mdi:battery-negative",
native_unit_of_measurement=POWER_WATT,
entity_category=EntityCategory.CONFIG,
Expand All @@ -66,7 +67,6 @@ class HuaweiSolarNumberEntityDescription(NumberEntityDescription):
native_min_value=90,
native_max_value=100,
native_step=0.1,
name="End-of-charge SOC",
icon="mdi:battery-positive",
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.CONFIG,
Expand All @@ -77,7 +77,6 @@ class HuaweiSolarNumberEntityDescription(NumberEntityDescription):
native_max_value=20,
dynamic_maximum_key=rn.STORAGE_CAPACITY_CONTROL_SOC_PEAK_SHAVING,
native_step=0.1,
name="End-of-discharge SOC",
icon="mdi:battery-negative",
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.CONFIG,
Expand All @@ -87,7 +86,6 @@ class HuaweiSolarNumberEntityDescription(NumberEntityDescription):
native_min_value=0,
native_max_value=100,
native_step=0.1,
name="Backup power SOC",
icon="mdi:battery-negative",
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.CONFIG,
Expand All @@ -98,7 +96,6 @@ class HuaweiSolarNumberEntityDescription(NumberEntityDescription):
native_min_value=20,
native_max_value=100,
native_step=0.1,
name="Grid charge cutoff SOC",
icon="mdi:battery-charging-50",
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.CONFIG,
Expand All @@ -107,7 +104,6 @@ class HuaweiSolarNumberEntityDescription(NumberEntityDescription):
key=rn.STORAGE_POWER_OF_CHARGE_FROM_GRID,
native_min_value=0,
dynamic_maximum_key=rn.STORAGE_MAXIMUM_POWER_OF_CHARGE_FROM_GRID,
name="Grid charge maximum power",
icon="mdi:battery-negative",
native_unit_of_measurement=POWER_WATT,
entity_category=EntityCategory.CONFIG,
Expand All @@ -119,7 +115,6 @@ class HuaweiSolarNumberEntityDescription(NumberEntityDescription):
dynamic_minimum_key=rn.STORAGE_DISCHARGING_CUTOFF_CAPACITY,
native_max_value=100,
native_step=0.1,
name="Peak Shaving SOC",
icon="mdi:battery-arrow-up",
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.CONFIG,
Expand Down
41 changes: 14 additions & 27 deletions select.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ class HuaweiSolarSelectEntityDescription(Generic[T], SelectEntityDescription):
is_available_key: str | None = None
check_is_available_func: Callable[[Any], bool] | None = None

def __post_init__(self):
"""Defaults the translation_key to the select key."""
self.translation_key = self.translation_key or self.key.replace('#','_').lower()


ENERGY_STORAGE_SWITCH_DESCRIPTIONS: tuple[HuaweiSolarSelectEntityDescription, ...] = (
HuaweiSolarSelectEntityDescription(
key=rn.STORAGE_EXCESS_PV_ENERGY_USE_IN_TOU,
name="Excess PV energy use in TOU",
icon="mdi:battery-charging-medium",
entity_category=EntityCategory.CONFIG,
),
Expand All @@ -53,7 +56,6 @@ class HuaweiSolarSelectEntityDescription(Generic[T], SelectEntityDescription):
CAPACITY_CONTROL_SWITCH_DESCRIPTIONS: tuple[HuaweiSolarSelectEntityDescription, ...] = (
HuaweiSolarSelectEntityDescription(
key=rn.STORAGE_CAPACITY_CONTROL_MODE,
name="Capacity Control Mode",
icon="mdi:battery-arrow-up",
entity_category=EntityCategory.CONFIG,
# Active capacity control is only available is 'Charge from grid' is enabled
Expand Down Expand Up @@ -148,10 +150,10 @@ class HuaweiSolarSelectEntity(CoordinatorEntity, HuaweiSolarEntity, SelectEntity
entity_description: HuaweiSolarSelectEntityDescription

def _friendly_format(self, value: IntEnum):
return value.name.replace("_", " ").capitalize()
return value.name.lower()

def _to_enum(self, value: str):
return getattr(self._register_unit, value.replace(" ", "_").upper())
return getattr(self._register_unit, value.upper())

def __init__(
self,
Expand Down Expand Up @@ -240,48 +242,33 @@ def __init__(
self.bridge = bridge
self.entity_description = HuaweiSolarSelectEntityDescription(
key=rn.STORAGE_WORKING_MODE_SETTINGS,
name="Working Mode",
entity_category=EntityCategory.CONFIG,
)
self._attr_device_info = device_info
self._attr_unique_id = f"{bridge.serial_number}_{self.entity_description.key}"

self._attr_current_option = self.coordinator.data[self.entity_description.key].value.name.lower()
# The options depend on the type of battery
self.options_to_values = {}
available_options = [swm.name for swm in rv.StorageWorkingModesC]
if bridge.battery_type == rv.StorageProductModel.HUAWEI_LUNA2000:
self.options_to_values = {
"Maximise Self Consumption": rv.StorageWorkingModesC.MAXIMISE_SELF_CONSUMPTION,
"Time Of Use": rv.StorageWorkingModesC.TIME_OF_USE_LUNA2000,
"Fully Fed To Grid": rv.StorageWorkingModesC.FULLY_FED_TO_GRID,
}
available_options.remove(rv.StorageWorkingModesC.TIME_OF_USE_LG.name)
elif bridge.battery_type == rv.StorageProductModel.LG_RESU:
self.options_to_values = {
"Maximise Self Consumption": rv.StorageWorkingModesC.MAXIMISE_SELF_CONSUMPTION,
"Time Of Use": rv.StorageWorkingModesC.TIME_OF_USE_LG,
"Fully Fed To Grid": rv.StorageWorkingModesC.FULLY_FED_TO_GRID,
}

self._update_current_option()
available_options.remove(rv.StorageWorkingModesC.TIME_OF_USE_LUNA2000.name)

self._attr_options = list(self.options_to_values.keys())

def _update_current_option(self):
current_value = self.coordinator.data[self.entity_description.key].value
for key, value in self.options_to_values.items():
if value == current_value:
self._attr_current_option = key
self._attr_options = [option.lower() for option in available_options]

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._update_current_option()
self._attr_current_option = self.coordinator.data[self.entity_description.key].value.name.lower()
self.async_write_ha_state()


async def async_select_option(self, option) -> None:
"""Change the selected option."""

await self.bridge.set(
rn.STORAGE_WORKING_MODE_SETTINGS, self.options_to_values[option]
rn.STORAGE_WORKING_MODE_SETTINGS, getattr(rv.StorageWorkingModesC, option.upper())
)
self._attr_current_option = option

Expand Down
2 changes: 0 additions & 2 deletions sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,15 +1106,13 @@ def get_pv_entity_descriptions(count: int) -> list[HuaweiSolarSensorEntityDescri
[
HuaweiSolarSensorEntityDescription(
key=getattr(rn, f"PV_{idx:02}_VOLTAGE"),
name=f"PV {idx} voltage",
icon="mdi:lightning-bolt",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
HuaweiSolarSensorEntityDescription(
key=getattr(rn, f"PV_{idx:02}_CURRENT"),
name=f"PV {idx} current",
icon="mdi:lightning-bolt-outline",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
Expand Down
Loading

0 comments on commit 6921c27

Please sign in to comment.