Skip to content

Commit 538e3fd

Browse files
authored
Merge pull request #84 from RustyDust/sru_work
Rework some services to return more consistent responses:
2 parents 3d275d9 + bf07e92 commit 538e3fd

File tree

10 files changed

+190
-29
lines changed

10 files changed

+190
-29
lines changed

.github/workflows/hassfest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
pull_request:
66
schedule:
7-
- cron: "0 0 * * *"
7+
- cron: "0 0 * * 0"
88

99
jobs:
1010
validate:

README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,27 @@ data:
9999
mode: "automatic"
100100
```
101101
102+
##### Response
103+
The name of the mode that has been set:
104+
- `manual`
105+
- `automatic`
106+
- `timeofuse`
107+
108+
### <a name="set_operatingmode_num"></a>`set_operating_mode_num(mode=<mode>)`
109+
- Sets the operating mode of your SonnenBatterie.
110+
- Supported values for `<mode>` are:
111+
- `1`
112+
- `2`
113+
- `10`
114+
115+
##### Code snippet
116+
``` yaml
117+
action: sonnenbatterie.set_operating_mode_num
118+
data:
119+
device_id: "<your sb instance's device id>"
120+
mode: 2
121+
```
122+
102123
##### Response
103124
An `int` representing the mode that has been set:
104125
- 1: `manual`
@@ -276,6 +297,38 @@ data:
276297
}
277298
```
278299

300+
### <a name="get_operatingmode"></a>`get_operating_mode()`
301+
- Retrieves the current operating mode of your SonnenBatterie.
302+
303+
##### Code snippet
304+
``` yaml
305+
action: sonnenbatterie.get_operating_mode
306+
data:
307+
device_id: "<your sb instance's device id>"
308+
```
309+
310+
##### Response
311+
The name of the mode that has been set:
312+
- `manual`
313+
- `automatic`
314+
- `timeofuse`
315+
316+
### <a name="get_operatingmode_num"></a>`get_operating_mode_num()`
317+
- Retrieves the current operating mode of your SonnenBatterie in numeric form
318+
319+
##### Code snippet
320+
``` yaml
321+
action: sonnenbatterie.get_operating_mode_num
322+
data:
323+
device_id: "<your sb instance's device id>"
324+
```
325+
326+
##### Response
327+
An `int` representing the mode that has been set:
328+
- 1: `manual`
329+
- 2: `automatic`
330+
- 10: `timeofuse`
331+
279332
## Problems and/or unused/unavailable sensors
280333
Depending on the software on and the operating mode of your Sonnenbatterie some
281334
sonsors may not be available. The integration does its best to collect as many
@@ -300,4 +353,4 @@ Please put those logs along with the setting you want monitored into
300353
[hacsbadge]: https://img.shields.io/badge/HACS-Custom-orange.svg?style=for-the-badge&logo=homeassistantcommunitystore&logoColor=ccc
301354

302355
[hainstall]: https://my.home-assistant.io/redirect/config_flow_start/?domain=sonnenbatterie
303-
[hainstallbadge]: https://img.shields.io/badge/dynamic/json?style=for-the-badge&logo=home-assistant&logoColor=ccc&label=usage&suffix=%20installs&cacheSeconds=15600&url=https://analytics.home-assistant.io/custom_integrations.json&query=$.sonnenbatterie.total
356+
[hainstallbadge]: https://img.shields.io/badge/dynamic/json?style=for-the-badge&logo=home-assistant&logoColor=ccc&label=usage&suffix=%20installs&cacheSeconds=15600&url=https://analytics.home-assistant.io/custom_integrations.json&query=$.sonnenbatterie.total

custom_components/sonnenbatterie/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
}
4141
)
4242

43+
SCHEMA_SET_OPERATING_MODE_NUM = vol.Schema(
44+
{
45+
**cv.ENTITY_SERVICE_FIELDS,
46+
vol.Required(CONF_SERVICE_MODE, default=2): vol.In(CONF_OPERATING_MODES_NUM),
47+
}
48+
)
49+
4350
SCHEMA_SET_TOU_SCHEDULE_STRING = vol.Schema(
4451
{
4552
**cv.ENTITY_SERVICE_FIELDS,
@@ -155,6 +162,14 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
155162
supports_response=SupportsResponse.OPTIONAL,
156163
)
157164

165+
hass.services.async_register(
166+
DOMAIN,
167+
"set_operating_mode_num",
168+
services.set_operating_mode_num,
169+
schema=SCHEMA_SET_OPERATING_MODE_NUM,
170+
supports_response=SupportsResponse.OPTIONAL,
171+
)
172+
158173
hass.services.async_register(
159174
DOMAIN,
160175
"set_tou_schedule",
@@ -184,6 +199,13 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
184199
supports_response=SupportsResponse.OPTIONAL,
185200
)
186201

202+
hass.services.async_register(
203+
DOMAIN,
204+
"get_operating_mode_num",
205+
services.get_operating_mode_num,
206+
supports_response=SupportsResponse.OPTIONAL,
207+
)
208+
187209
# Done setting up the entry
188210
return True
189211

custom_components/sonnenbatterie/const.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
"timeofuse"
2929
]
3030

31+
CONF_OPERATING_MODES_NUM = [
32+
"1",
33+
"2",
34+
"10"
35+
]
36+
3137
CONF_CHARGE_WATT = "power"
3238
CONF_COORDINATOR = "coordinator"
3339
CONF_INVERTER_MAX = "inverter_max"

custom_components/sonnenbatterie/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"iot_class": "local_polling",
99
"issue_tracker": "https://github.com/weltmeyer/ha_sonnenbatterie/issues",
1010
"requirements": ["requests","sonnenbatterie>=0.5.2"],
11-
"version": "2025.01.03"
11+
"version": "2025.02.01"
1212
}

custom_components/sonnenbatterie/service.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
DOMAIN,
1818
LOGGER,
1919
SB_OPERATING_MODES,
20+
SB_OPERATING_MODES_NUM,
2021
SONNENBATTERIE_ISSUE_URL,
2122
)
2223

@@ -55,9 +56,7 @@ async def charge_battery(self, call: ServiceCall) -> ServiceResponse:
5556
power = 0
5657
# Make sure we have an sb2 object
5758
sb_conn = self._get_sb_connection(call.data)
58-
# await sb_conn.login()
5959
response = await sb_conn.sb2.charge_battery(power)
60-
# await sb_conn.logout()
6160
return {
6261
"charge": response,
6362
}
@@ -68,19 +67,15 @@ async def discharge_battery(self, call: ServiceCall) -> ServiceResponse:
6867
if power < 0:
6968
power = 0
7069
sb_conn = self._get_sb_connection(call.data)
71-
# await sb_conn.login()
7270
response = await sb_conn.sb2.discharge_battery(power)
73-
# await sb_conn.logout()
7471
return {
7572
"discharge": response,
7673
}
7774

7875
async def set_battery_reserve(self, call: ServiceCall) -> ServiceResponse:
7976
value = call.data.get(CONF_SERVICE_VALUE)
8077
sb_conn = self._get_sb_connection(call.data)
81-
# await sb_conn.login()
8278
response = int((await sb_conn.sb2.set_battery_reserve(value))["EM_USOC"])
83-
# await sb_conn.logout()
8479
return {
8580
"battery_reserve": response,
8681
}
@@ -89,21 +84,25 @@ async def set_config_item(self, call: ServiceCall) -> ServiceResponse:
8984
item = call.data.get(CONF_SERVICE_ITEM)
9085
value = call.data.get(CONF_SERVICE_VALUE)
9186
sb_conn = self._get_sb_connection(call.data)
92-
# await sb_conn.login()
9387
response = await sb_conn.sb2.set_config_item(item, value)
94-
# await sb_conn.logout()
9588
return {
9689
"response": response,
9790
}
9891

9992
async def set_operating_mode(self, call: ServiceCall) -> ServiceResponse:
10093
mode = SB_OPERATING_MODES.get(call.data.get('mode'))
10194
sb_conn = self._get_sb_connection(call.data)
102-
# await sb_conn.login()
10395
response = await sb_conn.set_operating_mode(mode)
104-
# await sb_conn.logout()
10596
return {
106-
"mode": response,
97+
"mode": SB_OPERATING_MODES_NUM.get(str(response), "UNKNOWN")
98+
}
99+
100+
async def set_operating_mode_num(self, call: ServiceCall) -> ServiceResponse:
101+
mode = call.data.get('mode')
102+
sb_conn = self._get_sb_connection(call.data)
103+
response = await sb_conn.set_operating_mode(mode)
104+
return {
105+
"mode": response
107106
}
108107

109108
async def set_tou_schedule(self, call: ServiceCall) -> ServiceResponse:
@@ -122,38 +121,37 @@ async def set_tou_schedule(self, call: ServiceCall) -> ServiceResponse:
122121
raise HomeAssistantError(f"Schedule is not a valid schedule: '{schedule}'") from t
123122

124123
sb_conn = self._get_sb_connection(call.data)
125-
# await sb_conn.login()
126124
response = await sb_conn.sb2.set_tou_schedule_string(schedule)
127-
# await sb_conn.logout()
128125
return {
129126
"schedule": response["EM_ToU_Schedule"],
130127
}
131128

132129
# noinspection PyUnusedLocal
133130
async def get_tou_schedule(self, call: ServiceCall) -> ServiceResponse:
134131
sb_conn = self._get_sb_connection(call.data)
135-
# await sb_conn.login()
136132
response = await sb_conn.sb2.get_tou_schedule_string()
137-
# await sb_conn.logout()
138133
return {
139134
"schedule": response,
140135
}
141136

142137
# noinspection PyUnusedLocal
143138
async def get_battery_reserve(self, call: ServiceCall) -> ServiceResponse:
144139
sb_conn = self._get_sb_connection(call.data)
145-
# await sb_conn.login()
146140
response = await sb_conn.sb2.get_battery_reserve()
147-
# await sb_conn.logout()
148141
return {
149142
"backup_reserve": response,
150143
}
151144

152145
async def get_operating_mode(self, call: ServiceCall) -> ServiceResponse:
153146
sb_conn = self._get_sb_connection(call.data)
154-
# await sb_conn.login()
155147
response = await sb_conn.sb2.get_operating_mode()
156-
# await sb_conn.logout()
148+
return {
149+
"operating_mode": SB_OPERATING_MODES_NUM.get(str(response), "UNKNOWN")
150+
}
151+
152+
async def get_operating_mode_num(self, call: ServiceCall) -> ServiceResponse:
153+
sb_conn = self._get_sb_connection(call.data)
154+
response = await sb_conn.sb2.get_operating_mode()
157155
return {
158156
"operating_mode": response,
159157
}

custom_components/sonnenbatterie/services.yaml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,28 @@ set_operating_mode:
1010
example: "timeofuse"
1111
default: "automatic"
1212
selector:
13-
text:
13+
select:
14+
options:
15+
- "manual"
16+
- "automatic"
17+
- "timeofuse"
18+
set_operating_mode_num:
19+
fields:
20+
device_id:
21+
required: true
22+
selector:
23+
device:
24+
integration: sonnenbatterie
25+
mode:
26+
required: true
27+
example: 10
28+
default: 2
29+
selector:
30+
select:
31+
options:
32+
- "1"
33+
- "2"
34+
- "10"
1435
charge_battery:
1536
fields:
1637
device_id:
@@ -100,3 +121,10 @@ get_operating_mode:
100121
selector:
101122
device:
102123
integration: sonnenbatterie
124+
get_operating_mode_num:
125+
fields:
126+
device_id:
127+
required: true
128+
selector:
129+
device:
130+
integration: sonnenbatterie

custom_components/sonnenbatterie/translations/de.json

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@
194194
}
195195
}
196196
},
197+
"set_operating_mode_num": {
198+
"name": "Setze numerischen Sonnenbatterie-Betriebsmodus",
199+
"description": "Setzt den numerischen Betriebsmodus der Sonnenbatterie",
200+
"fields": {
201+
"device_id": {
202+
"description": "HomeAssistant ID des Geräts",
203+
"name": "Device ID",
204+
"example": "1234567890"
205+
},
206+
"mode": {
207+
"description": "Der zu setzende numerische Betriebsmodus ('1', '2', '10')",
208+
"name": "Betriebsmodus",
209+
"example": "2"
210+
}
211+
}
212+
},
197213
"charge_battery": {
198214
"name": "Sonnenbatterie laden",
199215
"description": "Erzwingt das Laden der Sonnenbatterie mit der angegebenen Leistung",
@@ -303,7 +319,18 @@
303319
},
304320
"get_operating_mode": {
305321
"name": "Betriebsmodus auslesen",
306-
"description": "Liefert den aktuellen Betriebsmodus der Sonnebatterie in numerischer Form",
322+
"description": "Liefert den aktuellen Betriebsmodus der Sonnenbatterie in Textform",
323+
"fields": {
324+
"device_id": {
325+
"description": "HomeAssistant ID des Geräts",
326+
"name": "Device ID",
327+
"example": "1234567890"
328+
}
329+
}
330+
},
331+
"get_operating_mode_num": {
332+
"name": "Betriebsmodus auslesen (numerisch)",
333+
"description": "Liefert den aktuellen Betriebsmodus der Sonnenbatterie in numerischer Form",
307334
"fields": {
308335
"device_id": {
309336
"description": "HomeAssistant ID des Geräts",

0 commit comments

Comments
 (0)