Add support for 4 MPTT inverter (#86901)

This commit is contained in:
mletenay 2023-02-13 12:49:11 +01:00 committed by GitHub
parent b0146618cb
commit ea94a2fbfd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 23 deletions

View file

@ -1,7 +1,7 @@
"""GoodWe PV inverter selection settings entities."""
import logging
from goodwe import Inverter, InverterError
from goodwe import Inverter, InverterError, OperationMode
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
@ -15,17 +15,26 @@ from .const import DOMAIN, KEY_DEVICE_INFO, KEY_INVERTER
_LOGGER = logging.getLogger(__name__)
_MODE_TO_OPTION: dict[OperationMode, str] = {
OperationMode.GENERAL: "general",
OperationMode.OFF_GRID: "off_grid",
OperationMode.BACKUP: "backup",
OperationMode.ECO: "eco",
OperationMode.PEAK_SHAVING: "peak_shaving",
OperationMode.ECO_CHARGE: "eco_charge",
OperationMode.ECO_DISCHARGE: "eco_discharge",
}
_OPTION_TO_MODE: dict[str, OperationMode] = {
value: key for key, value in _MODE_TO_OPTION.items()
}
OPERATION_MODE = SelectEntityDescription(
key="operation_mode",
name="Inverter operation mode",
icon="mdi:solar-power",
entity_category=EntityCategory.CONFIG,
options=[
"General mode",
"Off grid mode",
"Backup mode",
"Eco mode",
],
translation_key="operation_mode",
)
@ -38,6 +47,7 @@ async def async_setup_entry(
inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER]
device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO]
supported_modes = await inverter.get_operation_modes(False)
# read current operating mode from the inverter
try:
active_mode = await inverter.get_operation_mode()
@ -45,17 +55,17 @@ async def async_setup_entry(
# Inverter model does not support this setting
_LOGGER.debug("Could not read inverter operation mode")
else:
if (options := OPERATION_MODE.options) and 0 <= active_mode < len(options):
async_add_entities(
[
InverterOperationModeEntity(
device_info,
OPERATION_MODE,
inverter,
options[active_mode],
)
]
)
async_add_entities(
[
InverterOperationModeEntity(
device_info,
OPERATION_MODE,
inverter,
[v for k, v in _MODE_TO_OPTION.items() if k in supported_modes],
_MODE_TO_OPTION[active_mode],
)
]
)
class InverterOperationModeEntity(SelectEntity):
@ -68,17 +78,19 @@ class InverterOperationModeEntity(SelectEntity):
device_info: DeviceInfo,
description: SelectEntityDescription,
inverter: Inverter,
supported_options: list[str],
current_mode: str,
) -> None:
"""Initialize the inverter operation mode setting entity."""
self.entity_description = description
self._attr_unique_id = f"{DOMAIN}-{description.key}-{inverter.serial_number}"
self._attr_device_info = device_info
self._attr_options = supported_options
self._attr_current_option = current_mode
self._inverter: Inverter = inverter
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
await self._inverter.set_operation_mode(self.options.index(option))
await self._inverter.set_operation_mode(_OPTION_TO_MODE[option])
self._attr_current_option = option
self.async_write_ha_state()