Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """GoodWe PV inverter selection settings entities."""
2 
3 import logging
4 
5 from goodwe import Inverter, InverterError, OperationMode
6 
7 from homeassistant.components.select import SelectEntity, SelectEntityDescription
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import EntityCategory
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.device_registry import DeviceInfo
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .const import DOMAIN, KEY_DEVICE_INFO, KEY_INVERTER
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 
19 _MODE_TO_OPTION: dict[OperationMode, str] = {
20  OperationMode.GENERAL: "general",
21  OperationMode.OFF_GRID: "off_grid",
22  OperationMode.BACKUP: "backup",
23  OperationMode.ECO: "eco",
24  OperationMode.PEAK_SHAVING: "peak_shaving",
25  OperationMode.ECO_CHARGE: "eco_charge",
26  OperationMode.ECO_DISCHARGE: "eco_discharge",
27 }
28 
29 _OPTION_TO_MODE: dict[str, OperationMode] = {
30  value: key for key, value in _MODE_TO_OPTION.items()
31 }
32 
33 OPERATION_MODE = SelectEntityDescription(
34  key="operation_mode",
35  entity_category=EntityCategory.CONFIG,
36  translation_key="operation_mode",
37 )
38 
39 
41  hass: HomeAssistant,
42  config_entry: ConfigEntry,
43  async_add_entities: AddEntitiesCallback,
44 ) -> None:
45  """Set up the inverter select entities from a config entry."""
46  inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER]
47  device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO]
48 
49  supported_modes = await inverter.get_operation_modes(False)
50  # read current operating mode from the inverter
51  try:
52  active_mode = await inverter.get_operation_mode()
53  except (InverterError, ValueError):
54  # Inverter model does not support this setting
55  _LOGGER.debug("Could not read inverter operation mode")
56  else:
58  [
60  device_info,
61  OPERATION_MODE,
62  inverter,
63  [v for k, v in _MODE_TO_OPTION.items() if k in supported_modes],
64  _MODE_TO_OPTION[active_mode],
65  )
66  ]
67  )
68 
69 
71  """Entity representing the inverter operation mode."""
72 
73  _attr_should_poll = False
74  _attr_has_entity_name = True
75 
76  def __init__(
77  self,
78  device_info: DeviceInfo,
79  description: SelectEntityDescription,
80  inverter: Inverter,
81  supported_options: list[str],
82  current_mode: str,
83  ) -> None:
84  """Initialize the inverter operation mode setting entity."""
85  self.entity_descriptionentity_description = description
86  self._attr_unique_id_attr_unique_id = f"{DOMAIN}-{description.key}-{inverter.serial_number}"
87  self._attr_device_info_attr_device_info = device_info
88  self._attr_options_attr_options = supported_options
89  self._attr_current_option_attr_current_option = current_mode
90  self._inverter: Inverter = inverter
91 
92  async def async_update(self) -> None:
93  """Get the current value from inverter."""
94  value = await self._inverter.get_operation_mode()
95  self._attr_current_option_attr_current_option = _MODE_TO_OPTION[value]
96 
97  async def async_select_option(self, option: str) -> None:
98  """Change the selected option."""
99  await self._inverter.set_operation_mode(_OPTION_TO_MODE[option])
100  self._attr_current_option_attr_current_option = option
101  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, DeviceInfo device_info, SelectEntityDescription description, Inverter inverter, list[str] supported_options, str current_mode)
Definition: select.py:83
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:44