Move options to SelectEntityDescription in goodwe (#80017)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
epenet 2022-10-26 13:54:57 +02:00 committed by GitHub
parent 458f3d4d13
commit 9d3442055b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,18 +14,17 @@ from .const import DOMAIN, KEY_DEVICE_INFO, KEY_INVERTER
_LOGGER = logging.getLogger(__name__)
INVERTER_OPERATION_MODES = [
"General mode",
"Off grid mode",
"Backup mode",
"Eco mode",
]
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",
],
)
@ -45,14 +44,14 @@ async def async_setup_entry(
# Inverter model does not support this setting
_LOGGER.debug("Could not read inverter operation mode")
else:
if 0 <= active_mode < len(INVERTER_OPERATION_MODES):
if (options := OPERATION_MODE.options) and 0 <= active_mode < len(options):
async_add_entities(
[
InverterOperationModeEntity(
device_info,
OPERATION_MODE,
inverter,
INVERTER_OPERATION_MODES[active_mode],
options[active_mode],
)
]
)
@ -74,12 +73,11 @@ class InverterOperationModeEntity(SelectEntity):
self.entity_description = description
self._attr_unique_id = f"{DOMAIN}-{description.key}-{inverter.serial_number}"
self._attr_device_info = device_info
self._attr_options = INVERTER_OPERATION_MODES
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(INVERTER_OPERATION_MODES.index(option))
await self._inverter.set_operation_mode(self.options.index(option))
self._attr_current_option = option
self.async_write_ha_state()