-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.py
113 lines (92 loc) · 3.36 KB
/
select.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""Select entities for SlxChargingController"""
from __future__ import annotations
from dataclasses import dataclass
from collections.abc import Callable
import logging
from typing import Final, Any
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .coordinator import SLXChgCtrlUpdateCoordinator
from .entity import SlxChgCtrlEntity
from .const import (
DOMAIN,
KEY_CHARGE_MODE,
CMD_CHARGE_MODE,
ENT_CHARGE_MODE,
CHARGER_MODES,
KEY_CHARGE_METHOD,
CMD_CHARGE_METHOD,
ENT_CHARGE_METHOD,
CHARGE_METHODS,
)
_LOGGER = logging.getLogger(__name__)
@dataclass
class SLXSelectEntityDescription(SelectEntityDescription):
"""Extend default SelectEntityDescription to include options and command which should be called in coordinator"""
command: str | None = None
data_entry: str | None = None
default_options: list | None = None
SELECT_DESCRIPTIONS: Final[tuple[SLXSelectEntityDescription, ...]] = (
SLXSelectEntityDescription(
key=KEY_CHARGE_MODE,
command=CMD_CHARGE_MODE,
data_entry=ENT_CHARGE_MODE,
name="Charge mode",
default_options=CHARGER_MODES,
icon="mdi:ev-station",
),
SLXSelectEntityDescription(
key=KEY_CHARGE_METHOD,
command=CMD_CHARGE_METHOD,
data_entry=ENT_CHARGE_METHOD,
name="Charge method",
default_options=CHARGE_METHODS,
icon="mdi:ev-station",
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up sensor platform."""
coordinator = hass.data[DOMAIN][config_entry.unique_id]
entities = []
for description in SELECT_DESCRIPTIONS:
entities.append(SlxChrCtrlSelect(coordinator, description))
async_add_entities(entities)
return True
class SlxChrCtrlSelect(SelectEntity, SlxChgCtrlEntity):
def __init__(
self,
coordinator: SLXChgCtrlUpdateCoordinator,
description: SLXSelectEntityDescription,
) -> None:
super().__init__(coordinator)
self._description = description
self._key = self._description.key
self._command = description.command
self._data_entry = description.data_entry
self._attr_unique_id = f"{DOMAIN}_{self._key}"
self._attr_icon = self._description.icon
self._attr_name = f"{self._description.name}"
self._attr_options = description.default_options
@property
def current_option(self) -> str | None:
coordinator: SLXChgCtrlUpdateCoordinator = self.coordinator
data = coordinator.data
if self._data_entry in data and data is not None:
state = data[self._data_entry]
return str(state)
return None
async def async_select_option(self, option: Any) -> None:
coordinator: SLXChgCtrlUpdateCoordinator = self.coordinator
try:
await getattr(coordinator, self._command)(option)
self.async_write_ha_state()
except (ValueError, KeyError, AttributeError) as err:
_LOGGER.warning(
"Could not set status for %s error: %s", self._attr_name, err
)