|
| 1 | +from homeassistant.config_entries import ConfigEntry |
1 | 2 | from homeassistant.const import CONF_API_KEY, CONF_TOKEN, Platform
|
2 | 3 | from homeassistant.core import HomeAssistant
|
3 | 4 | from homeassistant.helpers import aiohttp_client
|
| 5 | +from homeassistant.helpers import device_registry as dr |
4 | 6 |
|
5 |
| -from custom_components.solis_cloud_control.coordinator import SolisCloudControlConfigEntry, SolisCloudControlCoordinator |
| 7 | +from custom_components.solis_cloud_control.coordinator import SolisCloudControlCoordinator |
6 | 8 |
|
7 | 9 | from .api import SolisCloudControlApiClient
|
8 | 10 | from .const import CONF_INVERTER_SN
|
9 | 11 |
|
10 | 12 | PLATFORMS: list[Platform] = [Platform.SELECT]
|
11 | 13 |
|
12 | 14 |
|
13 |
| -async def async_setup_entry(hass: HomeAssistant, entry: SolisCloudControlConfigEntry) -> bool: |
| 15 | +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
14 | 16 | api_key = entry.data[CONF_API_KEY]
|
15 | 17 | api_token = entry.data[CONF_TOKEN]
|
16 | 18 | inverter_sn = entry.data[CONF_INVERTER_SN]
|
17 | 19 |
|
18 | 20 | session = aiohttp_client.async_get_clientsession(hass)
|
19 |
| - api_client = SolisCloudControlApiClient(api_key, api_token, inverter_sn, session) |
20 |
| - |
21 |
| - coordinator = SolisCloudControlCoordinator( |
22 |
| - hass, |
23 |
| - entry, |
24 |
| - api_client, |
25 |
| - ) |
| 21 | + api_client = SolisCloudControlApiClient(api_key, api_token, session) |
26 | 22 |
|
| 23 | + coordinator = SolisCloudControlCoordinator(hass, entry, api_client) |
27 | 24 | entry.runtime_data = coordinator
|
28 | 25 |
|
| 26 | + device_registry = dr.async_get(hass) |
| 27 | + device_registry.async_get_or_create( |
| 28 | + config_entry_id=entry.entry_id, |
| 29 | + identifiers={(entry.domain, inverter_sn)}, |
| 30 | + manufacturer="Solis", |
| 31 | + name=f"Inverter {inverter_sn}", |
| 32 | + ) |
| 33 | + |
29 | 34 | await coordinator.async_config_entry_first_refresh()
|
30 | 35 |
|
31 | 36 | await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
32 |
| - |
33 |
| - # hass.async_create_task( |
34 |
| - # async_load_platform( |
35 |
| - # hass, |
36 |
| - # "select", |
37 |
| - # DOMAIN, |
38 |
| - # {"client": api_client, "inverter_sn": inverter_sn}, |
39 |
| - # entry, |
40 |
| - # ) |
41 |
| - # ) |
42 |
| - |
43 |
| - # async def async_service_read(call: ServiceCall) -> ServiceResponse: |
44 |
| - # cid = call.data.get("cid") |
45 |
| - # try: |
46 |
| - # result = await api_client.read(cid) |
47 |
| - # _LOGGER.info("Read state for '%s': '%s'", cid, result) |
48 |
| - # return {"value": result} |
49 |
| - # except SolisCloudControlApiError as err: |
50 |
| - # raise HomeAssistantError(str(err)) from err |
51 |
| - |
52 |
| - # async def async_service_control(call: ServiceCall) -> None: |
53 |
| - # cid = call.data.get("cid") |
54 |
| - # value = call.data.get("value") |
55 |
| - # try: |
56 |
| - # _LOGGER.info("Control '%s' state with value: '%s'", cid, value) |
57 |
| - # await api_client.control(cid, value) |
58 |
| - # except SolisCloudControlApiError as err: |
59 |
| - # raise HomeAssistantError(str(err)) from err |
60 |
| - |
61 |
| - # async def async_service_set_storage_mode(call: ServiceCall) -> ServiceResponse: |
62 |
| - # storage_mode = call.data.get("storage_mode", "Self Use") |
63 |
| - # battery_reserve = call.data.get("battery_reserve", "ON") |
64 |
| - # allow_grid_charging = call.data.get("allow_grid_charging", "OFF") |
65 |
| - |
66 |
| - # value_int = 0 |
67 |
| - |
68 |
| - # if storage_mode == "Self Use": |
69 |
| - # value_int |= 1 << STORAGE_MODE_BIT_SELF_USE |
70 |
| - # elif storage_mode == "Feed In Priority": |
71 |
| - # value_int |= 1 << STORAGE_MODE_BIT_FEED_IN_PRIORITY |
72 |
| - |
73 |
| - # if battery_reserve == "ON": |
74 |
| - # value_int |= 1 << STORAGE_MODE_BIT_BACKUP_MODE |
75 |
| - |
76 |
| - # if allow_grid_charging == "ON": |
77 |
| - # value_int |= 1 << STORAGE_MODE_BIT_GRID_CHARGING |
78 |
| - |
79 |
| - # value = str(value_int) |
80 |
| - |
81 |
| - # try: |
82 |
| - # _LOGGER.info( |
83 |
| - # "Setting storage mode: mode='%s', battery_reserve='%s', allow_grid_charging='%s', value='%s'", |
84 |
| - # storage_mode, |
85 |
| - # battery_reserve, |
86 |
| - # allow_grid_charging, |
87 |
| - # value, |
88 |
| - # ) |
89 |
| - # await api_client.control(STORAGE_MODE_CID, value) |
90 |
| - # return {"value": value} |
91 |
| - # except SolisCloudControlApiError as err: |
92 |
| - # raise HomeAssistantError(str(err)) from err |
93 |
| - |
94 |
| - # async def async_service_set_charge_slot1(call: ServiceCall) -> ServiceResponse: |
95 |
| - # from_time = call.data.get("from_time") |
96 |
| - # to_time = call.data.get("to_time") |
97 |
| - # current = call.data.get("current") |
98 |
| - # soc = call.data.get("soc") |
99 |
| - |
100 |
| - # from_time_formatted = from_time.strftime("%H:%M") |
101 |
| - # to_time_formatted = to_time.strftime("%H:%M") |
102 |
| - |
103 |
| - # time_range = f"{from_time_formatted}-{to_time_formatted}" |
104 |
| - # response = {"time_range": time_range} |
105 |
| - |
106 |
| - # try: |
107 |
| - # _LOGGER.info("Setting charge slot 1 time range: '%s'", time_range) |
108 |
| - # await api_client.control(CHARGE_SLOT1_TIME_CID, time_range) |
109 |
| - |
110 |
| - # if current is not None: |
111 |
| - # current_str = str(current) |
112 |
| - # _LOGGER.info("Setting charge slot 1 current: '%s'", current_str) |
113 |
| - # await api_client.control(CHARGE_SLOT1_CURRENT_CID, current_str) |
114 |
| - # response["current"] = current |
115 |
| - |
116 |
| - # if soc is not None: |
117 |
| - # soc_str = str(soc) |
118 |
| - # _LOGGER.info("Setting charge slot 1 SOC: '%s'", soc_str) |
119 |
| - # await api_client.control(CHARGE_SLOT1_SOC_CID, soc_str) |
120 |
| - # response["soc"] = soc |
121 |
| - |
122 |
| - # return response |
123 |
| - # except SolisCloudControlApiError as err: |
124 |
| - # raise HomeAssistantError(str(err)) from err |
125 |
| - |
126 |
| - # async def async_service_set_discharge_slot1(call: ServiceCall) -> ServiceResponse: |
127 |
| - # from_time = call.data.get("from_time") |
128 |
| - # to_time = call.data.get("to_time") |
129 |
| - # current = call.data.get("current") |
130 |
| - # soc = call.data.get("soc") |
131 |
| - |
132 |
| - # from_time_formatted = from_time.strftime("%H:%M") |
133 |
| - # to_time_formatted = to_time.strftime("%H:%M") |
134 |
| - |
135 |
| - # time_range = f"{from_time_formatted}-{to_time_formatted}" |
136 |
| - # response = {"time_range": time_range} |
137 |
| - |
138 |
| - # try: |
139 |
| - # _LOGGER.info("Setting discharge slot 1 time range: '%s'", time_range) |
140 |
| - # await api_client.control(DISCHARGE_SLOT1_TIME_CID, time_range) |
141 |
| - |
142 |
| - # if current is not None: |
143 |
| - # current_str = str(current) |
144 |
| - # _LOGGER.info("Setting discharge slot 1 current: '%s'", current_str) |
145 |
| - # await api_client.control(DISCHARGE_SLOT1_CURRENT_CID, current_str) |
146 |
| - # response["current"] = current |
147 |
| - |
148 |
| - # if soc is not None: |
149 |
| - # soc_str = str(soc) |
150 |
| - # _LOGGER.info("Setting discharge slot 1 SOC: '%s'", soc_str) |
151 |
| - # await api_client.control(DISCHARGE_SLOT1_SOC_CID, soc_str) |
152 |
| - # response["soc"] = soc |
153 |
| - |
154 |
| - # return response |
155 |
| - # except SolisCloudControlApiError as err: |
156 |
| - # raise HomeAssistantError(str(err)) from err |
157 |
| - |
158 |
| - # async def async_service_disable_charge_slot1(call: ServiceCall) -> None: # noqa: ARG001 |
159 |
| - # try: |
160 |
| - # _LOGGER.info("Disabling charge slot 1") |
161 |
| - # await api_client.control(CHARGE_SLOT1_TIME_CID, "00:00-00:00") |
162 |
| - # except SolisCloudControlApiError as err: |
163 |
| - # raise HomeAssistantError(str(err)) from err |
164 |
| - |
165 |
| - # async def async_service_disable_discharge_slot1(call: ServiceCall) -> None: # noqa: ARG001 |
166 |
| - # try: |
167 |
| - # _LOGGER.info("Disabling discharge slot 1") |
168 |
| - # await api_client.control(DISCHARGE_SLOT1_TIME_CID, "00:00-00:00") |
169 |
| - # except SolisCloudControlApiError as err: |
170 |
| - # raise HomeAssistantError(str(err)) from err |
171 |
| - |
172 |
| - # hass.services.async_register( |
173 |
| - # DOMAIN, |
174 |
| - # READ_SERVICE_NAME, |
175 |
| - # async_service_read, |
176 |
| - # supports_response=SupportsResponse.ONLY, |
177 |
| - # schema=READ_SERVICE_SCHEMA, |
178 |
| - # ) |
179 |
| - # hass.services.async_register( |
180 |
| - # DOMAIN, |
181 |
| - # CONTROL_SERVICE_NAME, |
182 |
| - # async_service_control, |
183 |
| - # supports_response=SupportsResponse.NONE, |
184 |
| - # schema=CONTROL_SERVICE_SCHEMA, |
185 |
| - # ) |
186 |
| - # hass.services.async_register( |
187 |
| - # DOMAIN, |
188 |
| - # SET_STORAGE_MODE_SERVICE_NAME, |
189 |
| - # async_service_set_storage_mode, |
190 |
| - # supports_response=SupportsResponse.ONLY, |
191 |
| - # schema=SET_STORAGE_MODE_SERVICE_SCHEMA, |
192 |
| - # ) |
193 |
| - # hass.services.async_register( |
194 |
| - # DOMAIN, |
195 |
| - # SET_CHARGE_SLOT1_SERVICE_NAME, |
196 |
| - # async_service_set_charge_slot1, |
197 |
| - # supports_response=SupportsResponse.ONLY, |
198 |
| - # schema=SET_CHARGE_SLOT1_SERVICE_SCHEMA, |
199 |
| - # ) |
200 |
| - # hass.services.async_register( |
201 |
| - # DOMAIN, |
202 |
| - # SET_DISCHARGE_SLOT1_SERVICE_NAME, |
203 |
| - # async_service_set_discharge_slot1, |
204 |
| - # supports_response=SupportsResponse.ONLY, |
205 |
| - # schema=SET_DISCHARGE_SLOT1_SERVICE_SCHEMA, |
206 |
| - # ) |
207 |
| - # hass.services.async_register( |
208 |
| - # DOMAIN, |
209 |
| - # DISABLE_CHARGE_SLOT1_SERVICE_NAME, |
210 |
| - # async_service_disable_charge_slot1, |
211 |
| - # supports_response=SupportsResponse.NONE, |
212 |
| - # ) |
213 |
| - # hass.services.async_register( |
214 |
| - # DOMAIN, |
215 |
| - # DISABLE_DISCHARGE_SLOT1_SERVICE_NAME, |
216 |
| - # async_service_disable_discharge_slot1, |
217 |
| - # supports_response=SupportsResponse.NONE, |
218 |
| - # ) |
| 37 | + entry.async_on_unload(entry.add_update_listener(async_reload_entry)) |
219 | 38 |
|
220 | 39 | return True
|
221 | 40 |
|
222 | 41 |
|
223 |
| -async def async_unload_entry(hass: HomeAssistant, entry: SolisCloudControlConfigEntry) -> bool: |
| 42 | +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
224 | 43 | return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
| 44 | + |
| 45 | + |
| 46 | +async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: |
| 47 | + await async_unload_entry(hass, entry) |
| 48 | + await async_setup_entry(hass, entry) |
0 commit comments