forked from CJNE/ha-myenergi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.py
180 lines (151 loc) · 5.54 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""Sensor platform for myenergi."""
import voluptuous as vol
from homeassistant.components.select import SelectEntity
from homeassistant.helpers import entity_platform
from pymyenergi.eddi import EDDI_MODES
from pymyenergi.libbi import LIBBI_MODE_NAMES
from pymyenergi.libbi import LIBBI_MODES
from pymyenergi.libbi import MODE_NORMAL
from pymyenergi.libbi import MODE_STOPPED
from pymyenergi.zappi import CHARGE_MODES
from .const import DOMAIN
from .entity import MyenergiEntity
ATTR_BOOST_AMOUNT = "amount"
ATTR_BOOST_TIME = "time"
ATTR_BOOST_TARGET = "target"
ATTR_BOOST_WHEN = "when"
BOOST_SCHEMA = {
vol.Required(ATTR_BOOST_AMOUNT): vol.All(
vol.Coerce(float),
vol.Range(min=1, max=100),
vol.Range(min=1, max=100),
),
}
EDDI_BOOST_SCHEMA = {
vol.Required(ATTR_BOOST_TARGET): str,
vol.Required(ATTR_BOOST_TIME): vol.Coerce(float),
}
SMART_BOOST_SCHEMA = {
vol.Required(ATTR_BOOST_AMOUNT): vol.All(
vol.Coerce(float),
vol.Range(min=1, max=100),
),
vol.Required(ATTR_BOOST_WHEN): str,
}
async def async_setup_entry(hass, entry, async_add_devices):
"""Setup select platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
platform = entity_platform.async_get_current_platform()
devices = []
# Don't cause a refresh when fetching devices
all_devices = await coordinator.client.get_devices("all", False)
for device in all_devices:
# Zappi only selects
if device.kind == "zappi":
platform.async_register_entity_service(
"myenergi_boost",
BOOST_SCHEMA,
"start_boost",
)
platform.async_register_entity_service(
"myenergi_smart_boost",
SMART_BOOST_SCHEMA,
"start_smart_boost",
)
platform.async_register_entity_service(
"myenergi_stop_boost",
{},
"stop_boost",
)
devices.append(ZappiChargeModeSelect(coordinator, device, entry))
elif device.kind == "eddi":
platform.async_register_entity_service(
"myenergi_eddi_boost",
EDDI_BOOST_SCHEMA,
"start_eddi_boost",
)
devices.append(EddiOperatingModeSelect(coordinator, device, entry))
elif device.kind == "libbi":
devices.append(LibbiOperatingModeSelect(coordinator, device, entry))
async_add_devices(devices)
class EddiOperatingModeSelect(MyenergiEntity, SelectEntity):
"""myenergi Sensor class."""
def __init__(self, coordinator, device, config_entry):
super().__init__(coordinator, device, config_entry)
@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return (
f"{self.config_entry.entry_id}-{self.device.serial_number}-operating_mode"
)
@property
def name(self):
"""Return the name of the sensor."""
return f"myenergi {self.device.name} Operating Mode"
@property
def current_option(self):
"""Return the state of the sensor."""
if self.device.status == "Stopped":
return "Stopped"
return "Normal"
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
await self.device.set_operating_mode(option)
self.async_schedule_update_ha_state()
@property
def options(self):
return EDDI_MODES
class ZappiChargeModeSelect(MyenergiEntity, SelectEntity):
"""myenergi Sensor class."""
def __init__(self, coordinator, device, config_entry):
super().__init__(coordinator, device, config_entry)
@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return f"{self.config_entry.entry_id}-{self.device.serial_number}-charge_mode"
@property
def name(self):
"""Return the name of the sensor."""
return f"myenergi {self.device.name} Charge Mode"
@property
def icon(self):
"""Return the icon of the select."""
return "mdi:ev-station"
@property
def current_option(self):
"""Return the state of the sensor."""
return self.device.charge_mode
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
await self.device.set_charge_mode(option)
self.async_schedule_update_ha_state()
@property
def options(self):
return CHARGE_MODES[1:]
class LibbiOperatingModeSelect(MyenergiEntity, SelectEntity):
"""myenergi Sensor class."""
def __init__(self, coordinator, device, config_entry):
super().__init__(coordinator, device, config_entry)
@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return (
f"{self.config_entry.entry_id}-{self.device.serial_number}-operating_mode"
)
@property
def name(self):
"""Return the name of the sensor."""
return f"myenergi {self.device.name} Operating Mode"
@property
def current_option(self):
"""Return the state of the sensor."""
if self.device.local_mode == LIBBI_MODE_NAMES[MODE_STOPPED]:
return LIBBI_MODES[MODE_STOPPED]
return LIBBI_MODES[MODE_NORMAL]
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
await self.device.set_operating_mode(option)
self.async_schedule_update_ha_state()
@property
def options(self):
return LIBBI_MODES