Skip to content

Commit 07f2bce

Browse files
New release with options to select which devices and control polling enable
1 parent 5d8c502 commit 07f2bce

File tree

8 files changed

+131
-67
lines changed

8 files changed

+131
-67
lines changed

custom_components/ge_cloud/__init__.py

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
CONFIG_KIND,
2121
DATA_ACCOUNT,
2222
DATA_SERIALS,
23+
CONFIG_INVERTER_ENABLE,
24+
CONFIG_SMART_DEVICE_ENABLE,
25+
CONFIG_EVC_ENABLE,
26+
CONFIG_POLL_INVERTER,
2327
)
2428

2529
ACCOUNT_PLATFORMS = ["sensor", "number", "switch", "select"]
@@ -57,59 +61,67 @@ async def async_setup_dependencies(hass: HomeAssistant, config):
5761
"""Setup the coordinator and api client which will be shared by various entities"""
5862
account_id = config[CONFIG_ACCOUNT_ID]
5963
api_key = config[CONFIG_MAIN_API_KEY]
64+
inverter_enable = config.get(CONFIG_INVERTER_ENABLE, True)
65+
smart_device_enable = config.get(CONFIG_SMART_DEVICE_ENABLE)
66+
evc_enable = config.get(CONFIG_EVC_ENABLE)
67+
poll_inverter = config.get(CONFIG_POLL_INVERTER)
6068

6169
_LOGGER.info("Create API Client for account {}".format(account_id))
6270
client = GECloudApiClient(account_id, api_key)
6371
hass.data[DOMAIN][account_id][DATA_CLIENT] = client
64-
serials = await client.async_get_devices()
65-
_LOGGER.info("Got serials {}".format(serials))
6672
hass.data[DOMAIN][account_id][DATA_SERIALS] = {}
67-
for serial in serials:
68-
hass.data[DOMAIN][account_id][DATA_SERIALS][serial] = {}
69-
_LOGGER.info(
70-
"Create Inverter Cloud coordinator for account {} serial {}".format(
71-
account_id, serial
72-
)
73-
)
74-
await async_setup_cloud_coordinator(hass, account_id, serial, type="inverter")
75-
76-
smart_devices = await client.async_get_smart_devices()
77-
_LOGGER.info("Got smart devices {}".format(smart_devices))
78-
for device in smart_devices:
79-
uuid = device.get("uuid", None)
80-
if uuid:
81-
hass.data[DOMAIN][account_id][DATA_SERIALS][uuid] = {}
73+
74+
if inverter_enable:
75+
serials = await client.async_get_devices()
76+
_LOGGER.info("Got inverter serials {}".format(serials))
77+
for serial in serials:
78+
hass.data[DOMAIN][account_id][DATA_SERIALS][serial] = {}
8279
_LOGGER.info(
83-
"Create Smart Device Cloud coordinator for account {} UUID {}".format(
84-
account_id, uuid
80+
"Create Inverter Cloud coordinator for account {} serial {}".format(
81+
account_id, serial
8582
)
8683
)
87-
await async_setup_cloud_coordinator(
88-
hass,
89-
account_id,
90-
uuid,
91-
type="smart_device",
92-
device_name=device.get("alias", None),
93-
)
84+
await async_setup_cloud_coordinator(hass, account_id, serial, type="inverter", polling=poll_inverter)
85+
86+
if smart_device_enable:
87+
smart_devices = await client.async_get_smart_devices()
88+
_LOGGER.info("Got smart devices {}".format(smart_devices))
89+
for device in smart_devices:
90+
uuid = device.get("uuid", None)
91+
if uuid:
92+
hass.data[DOMAIN][account_id][DATA_SERIALS][uuid] = {}
93+
_LOGGER.info(
94+
"Create Smart Device Cloud coordinator for account {} UUID {}".format(
95+
account_id, uuid
96+
)
97+
)
98+
await async_setup_cloud_coordinator(
99+
hass,
100+
account_id,
101+
uuid,
102+
type="smart_device",
103+
device_name=device.get("alias", None),
104+
)
94105

95-
evc_devices = await client.async_get_evc_devices()
96-
_LOGGER.info("Got EVC devices {}".format(evc_devices))
97-
for device in evc_devices:
98-
uuid = device.get("uuid", None)
99-
if uuid:
100-
hass.data[DOMAIN][account_id][DATA_SERIALS][uuid] = {}
101-
_LOGGER.info(
102-
"Create EVC Cloud coordinator for account {} UUID {}".format(
103-
account_id, uuid
106+
if evc_enable:
107+
evc_devices = await client.async_get_evc_devices()
108+
_LOGGER.info("Got EVC devices {}".format(evc_devices))
109+
for device in evc_devices:
110+
uuid = device.get("uuid", None)
111+
if uuid:
112+
hass.data[DOMAIN][account_id][DATA_SERIALS][uuid] = {}
113+
_LOGGER.info(
114+
"Create EVC Cloud coordinator for account {} UUID {}".format(
115+
account_id, uuid
116+
)
117+
)
118+
await async_setup_cloud_coordinator(
119+
hass,
120+
account_id,
121+
uuid,
122+
type="evc_device",
123+
device_name=device.get("alias", None),
104124
)
105-
)
106-
await async_setup_cloud_coordinator(
107-
hass,
108-
account_id,
109-
uuid,
110-
type="evc_device",
111-
device_name=device.get("alias", None),
112-
)
113125

114126

115127
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

custom_components/ge_cloud/api.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async def async_send_evc_command(self, uuid, command, params):
5959
datain=params,
6060
)
6161
_LOGGER.info(
62-
"Write comamnd {} params {} returns {}".format(
62+
"Write EVC comamnd {} params {} returns {}".format(
6363
command, params, data
6464
)
6565
)
@@ -71,7 +71,7 @@ async def async_send_evc_command(self, uuid, command, params):
7171
await asyncio.sleep(1 * (retry + 1))
7272
if data is None:
7373
_LOGGER.error(
74-
"Failed to send command {} params {}".format(command, params)
74+
"Failed to send EVC command {} params {}".format(command, params)
7575
)
7676
return data
7777

@@ -94,7 +94,7 @@ async def async_read_inverter_setting(self, serial, setting_id):
9494
break
9595
await asyncio.sleep(1 * (retry + 1))
9696
if data is None:
97-
_LOGGER.warning("Failed to read setting id {}".format(setting_id))
97+
_LOGGER.warning("Failed to read inverter setting id {}".format(setting_id))
9898
return data
9999

100100
async def async_write_inverter_setting(self, serial, setting_id, value):
@@ -110,7 +110,7 @@ async def async_write_inverter_setting(self, serial, setting_id, value):
110110
datain={"value": str(value), "context": "homeassistant"},
111111
)
112112
_LOGGER.info(
113-
"Write setting id {} value {} returns {}".format(
113+
"Write inverter setting id {} value {} returns {}".format(
114114
setting_id, value, data
115115
)
116116
)
@@ -135,7 +135,7 @@ async def async_get_inverter_settings(self, serial, first=False, previous={}):
135135
GE_API_INVERTER_SETTINGS, serial
136136
)
137137
_LOGGER.info(
138-
"Register list for serial {} is {}".format(
138+
"Register list for inverter serial {} is {}".format(
139139
serial, self.register_list[serial]
140140
)
141141
)
@@ -407,7 +407,7 @@ async def async_get_device_info(self, serial):
407407
if this_serial and this_serial == serial:
408408
_LOGGER.info("Got device {} info {}".format(serial, inverter))
409409
return inverter
410-
return None
410+
return {}
411411

412412
async def async_get_devices(self):
413413
"""
@@ -424,7 +424,7 @@ async def async_get_devices(self):
424424
_LOGGER.info("Got inverter {}".format(inverter))
425425
serial = inverter.get("serial", None)
426426
if serial:
427-
_LOGGER.info("Got serial {}".format(serial))
427+
_LOGGER.info("Got inverter serial {}".format(serial))
428428
serials.append(serial)
429429

430430
return serials

custom_components/ge_cloud/config_flow.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
CONFIG_KIND_ACCOUNT,
2020
DATA_SCHEMA_ACCOUNT,
2121
CONFIG_ACCOUNT_ID,
22+
CONFIG_INVERTER_ENABLE,
23+
CONFIG_SMART_DEVICE_ENABLE,
24+
CONFIG_EVC_ENABLE,
25+
CONFIG_POLL_INVERTER,
2226
)
2327
from .api import GECloudApiClient
2428

@@ -35,15 +39,30 @@ async def async_validate_main_config(data):
3539

3640
account_id = data[CONFIG_ACCOUNT_ID]
3741
api_key = data[CONFIG_MAIN_API_KEY]
42+
inverter_enable = data[CONFIG_INVERTER_ENABLE]
43+
smart_device_enable = data[CONFIG_SMART_DEVICE_ENABLE]
44+
evc_enable = data[CONFIG_EVC_ENABLE]
45+
poll_inverter = data[CONFIG_POLL_INVERTER]
3846
_LOGGER.info(
3947
"Validating main config for account {} api_key {}".format(account_id, api_key)
4048
)
4149
api = GECloudApiClient(account_id, api_key)
42-
serials = await api.async_get_devices()
43-
_LOGGER.info("Got serials {}".format(serials))
44-
if serials is None:
45-
errors[CONFIG_MAIN_API_KEY] = "invalid_api_key"
50+
inverter_serials = []
51+
smart_devices = []
52+
evc_devices = []
53+
54+
if inverter_enable:
55+
inverter_serials = await api.async_get_devices()
56+
_LOGGER.info("Got inverter serials {}".format(inverter_serials))
57+
if smart_device_enable:
58+
smart_devices = await api.async_get_smart_devices()
59+
_LOGGER.info("Got smart devices {}".format(smart_devices))
60+
if evc_enable:
61+
evc_devices = await api.async_get_evc_devices()
62+
_LOGGER.info("Got evc_devices {}".format(evc_devices))
4663

64+
if not inverter_serials and not smart_devices and not evc_devices:
65+
errors[CONFIG_MAIN_API_KEY] = "invalid_api_key"
4766
return errors
4867

4968

custom_components/ge_cloud/const.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import homeassistant.helpers.config_validation as cv
33

44
DOMAIN = "ge_cloud"
5-
INTEGRATION_VERSION = "1.1.11"
6-
CONFIG_VERSION = 1
5+
INTEGRATION_VERSION = "1.1.12"
6+
CONFIG_VERSION = 2
77

88
CONFIG_KIND = "kind"
99
CONFIG_KIND_ACCOUNT = "account"
@@ -15,10 +15,18 @@
1515
DATA_ACCOUNT_COORDINATOR = "ACCOUNT_COORDINATOR"
1616
CONFIG_MAIN_API_KEY = "api_key"
1717
CONFIG_ACCOUNT_ID = "account_id"
18+
CONFIG_INVERTER_ENABLE = "inverter_enable"
19+
CONFIG_SMART_DEVICE_ENABLE = "device_enable"
20+
CONFIG_EVC_ENABLE = "evc_enable"
21+
CONFIG_POLL_INVERTER = "poll_inverter"
1822

1923
DATA_SCHEMA_ACCOUNT = {
2024
vol.Required(CONFIG_ACCOUNT_ID, default="home"): str,
2125
vol.Required(CONFIG_MAIN_API_KEY, default="api_key"): str,
26+
vol.Required(CONFIG_INVERTER_ENABLE, default=True): bool,
27+
vol.Required(CONFIG_SMART_DEVICE_ENABLE, default=True): bool,
28+
vol.Required(CONFIG_EVC_ENABLE, default=True): bool,
29+
vol.Required(CONFIG_POLL_INVERTER, default=True): bool,
2230
}
2331

2432
GE_API_URL = "https://api.givenergy.cloud/v1/"

custom_components/ge_cloud/coordinator.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CloudCoordinator(DataUpdateCoordinator):
2626
"""My custom coordinator."""
2727

2828
def __init__(
29-
self, hass, account_id, serial, api, type="inverter", device_name=None
29+
self, hass, account_id, serial, api, type="inverter", device_name=None, polling=True
3030
):
3131
"""Initialize my coordinator."""
3232
super().__init__(
@@ -45,6 +45,12 @@ def __init__(
4545
self.data = {}
4646
self.update_count = 0
4747

48+
if serial.startswith("EMS"):
49+
_LOGGER.info("Setting up EMS {}, will always poll".format(serial))
50+
self.polling = True
51+
else:
52+
self.polling = polling
53+
4854
if device_name:
4955
self.device_name = device_name
5056
else:
@@ -66,13 +72,15 @@ async def _async_update_data(self, first=False):
6672
self.data["info"] = await self.api.async_get_device_info(self.serial)
6773
self.data["status"] = await self.api.async_get_inverter_status(self.serial)
6874
self.data["meter"] = await self.api.async_get_inverter_meter(self.serial)
75+
6976
# Update registers every 5 minutes, other data every minute
70-
if (self.update_count % 5) == 0:
77+
if first or (self.update_count == 0) or (self.polling and (self.update_count % 5) == 0):
7178
self.data["settings"] = await self.api.async_get_inverter_settings(
7279
self.serial, first=first, previous=self.data.get("settings", {})
7380
)
81+
7482
if self.type == "smart_device":
75-
if (self.update_count % 5) == 0:
83+
if first or (self.update_count == 0) or (self.polling and (self.update_count % 5) == 0):
7684
self.data["smart_device"] = await self.api.async_get_smart_device(
7785
self.serial
7886
)
@@ -81,9 +89,11 @@ async def _async_update_data(self, first=False):
8189
if self.type == "evc_device":
8290
self.data["evc_device"] = await self.api.async_get_evc_device(self.serial)
8391
self.data["point"] = await self.api.async_get_evc_device_data(self.serial)
84-
if (self.update_count % 10) == 0:
92+
93+
if first or (self.update_count == 0) or (self.polling and (self.update_count % 10) == 0):
8594
self.data["sessions"] = await self.api.async_get_evc_sessions(self.serial)
86-
if (self.update_count % 5) == 0:
95+
96+
if first or (self.update_count == 0) or (self.polling and (self.update_count % 5) == 0):
8797
self.data["commands"] = await self.api.async_get_evc_commands(self.serial)
8898

8999
_LOGGER.info("Coordinator data Update for device {}".format(self.device_name))
@@ -93,7 +103,7 @@ async def _async_update_data(self, first=False):
93103

94104

95105
async def async_setup_cloud_coordinator(
96-
hass, account_id: str, serial, type="inverter", device_name=None
106+
hass, account_id: str, serial, type="inverter", device_name=None, polling=True
97107
):
98108
hass.data[DOMAIN][account_id][DATA_SERIALS][serial][DATA_ACCOUNT_COORDINATOR] = (
99109
CloudCoordinator(
@@ -103,6 +113,7 @@ async def async_setup_cloud_coordinator(
103113
hass.data[DOMAIN][account_id][DATA_CLIENT],
104114
type=type,
105115
device_name=device_name,
116+
polling=polling,
106117
)
107118
)
108119
_LOGGER.info(

custom_components/ge_cloud/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"iot_class": "cloud_polling",
1717
"requirements": ["pytz"],
1818
"ssdp": [],
19-
"version": "1.1.11",
19+
"version": "1.1.12",
2020
"zeroconf": []
2121
}

custom_components/ge_cloud/strings.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
"data": {
99
"account_id": "Your account ID",
1010
"api_key": "Your Api key",
11-
"other": "Other"
11+
"inverter_enable": "Scan for Inverters",
12+
"device_enable": "Scan for Smart Devices",
13+
"evc_enable": "Scan for EVC Devices",
14+
"poll_inverter": "Enable Inverter Polling (every 5 minutes)"
1215
},
1316
"data_description": {
1417
"account_id": "Any identifier for your account (in case you have more than one)",
15-
"api_key": "This can be found in the GE Cloud portal under API Key"
18+
"api_key": "This can be found in the GE Cloud portal under API Key",
19+
"inverter_enable": "Enable to scan for inverters",
20+
"device_enable": "Enable to scan for Smart Devices",
21+
"evc_enable": "Enable to scan for EVC Devices",
22+
"poll_inverter": "Enable polling for normal use, disable for EMC setup"
1623
}
1724
}
1825
}

custom_components/ge_cloud/translations/en.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
"data": {
99
"account_id": "Your account ID",
1010
"api_key": "Your Api key",
11-
"other": "Other"
11+
"inverter_enable": "Scan for Inverters",
12+
"device_enable": "Scan for Smart Devices",
13+
"evc_enable": "Scan for EVC Devices",
14+
"poll_inverter": "Enable Inverter Polling (every 5 minutes)"
1215
},
1316
"data_description": {
1417
"account_id": "Any identifier for your account (in case you have more than one)",
15-
"api_key": "This can be found in the GE Cloud portal under API Key"
18+
"api_key": "This can be found in the GE Cloud portal under API Key",
19+
"inverter_enable": "Enable to scan for inverters",
20+
"device_enable": "Enable to scan for Smart Devices",
21+
"evc_enable": "Enable to scan for EVC Devices",
22+
"poll_inverter": "Enable polling for normal use, disable for EMC setup"
1623
}
1724
}
1825
}

0 commit comments

Comments
 (0)