@@ -100,7 +100,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
100100 hass .data [DOMAIN ][config_entry .entry_id ][CONF_COORDINATOR ] = sb_coordinator
101101
102102 inverter_power = sb_coordinator .latestData ['battery_system' ]['battery_system' ]['system' ]['inverter_capacity' ]
103- LOGGER .debug (f"inverter_power: { inverter_power } " )
103+ max_tou_power = sb_coordinator .latestData .get ('commissioning_settings' ,{}).get ('data' , {}).get ('attributes' , {}).get ('tou_max_power_limit' , '22000' )
104+ LOGGER .debug (f"inverter_power: { inverter_power } , tou_power: { max_tou_power } " )
105+ hass .data [DOMAIN ][config_entry .entry_id ][CONF_INVERTER_MAX ] = inverter_power
106+ hass .data [DOMAIN ][config_entry .entry_id ][CONF_TOU_MAX ] = int (max_tou_power )
104107
105108 # noinspection PyPep8Naming
106109 SCHEMA_CHARGE_BATTERY = vol .Schema (
@@ -114,97 +117,97 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
114117 # Initialize our services
115118 services = SonnenbatterieService (hass , config_entry , sb_coordinator )
116119
117- # Set up base data in hass object
118- # hass.data.setdefault(DOMAIN, {})
119- hass .data [DOMAIN ][config_entry .entry_id ][CONF_INVERTER_MAX ] = inverter_power
120-
121120 # Setup our sensors, services and whatnot
122121 await hass .config_entries .async_forward_entry_setups (config_entry , PLATFORMS )
123122
124- # service registration
125- hass .services .async_register (
126- DOMAIN ,
127- "charge_battery" ,
128- services .charge_battery ,
129- schema = SCHEMA_CHARGE_BATTERY ,
130- supports_response = SupportsResponse .OPTIONAL ,
131- )
132-
133- hass .services .async_register (
134- DOMAIN ,
135- "discharge_battery" ,
136- services .discharge_battery ,
137- schema = SCHEMA_CHARGE_BATTERY ,
138- supports_response = SupportsResponse .OPTIONAL ,
139- )
140-
141- hass .services .async_register (
142- DOMAIN ,
143- "set_battery_reserve" ,
144- services .set_battery_reserve ,
145- schema = SCHEMA_SET_BATTERY_RESERVE ,
146- supports_response = SupportsResponse .OPTIONAL ,
147- )
148-
149- hass .services .async_register (
150- DOMAIN ,
151- "set_config_item" ,
152- services .set_config_item ,
153- schema = SCHEMA_SET_CONFIG_ITEM ,
154- supports_response = SupportsResponse .OPTIONAL ,
155- )
156-
157- hass .services .async_register (
158- DOMAIN ,
159- "set_operating_mode" ,
160- services .set_operating_mode ,
161- schema = SCHEMA_SET_OPERATING_MODE ,
162- supports_response = SupportsResponse .OPTIONAL ,
163- )
164-
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-
173- hass .services .async_register (
174- DOMAIN ,
175- "set_tou_schedule" ,
176- services .set_tou_schedule ,
177- schema = SCHEMA_SET_TOU_SCHEDULE_STRING ,
178- supports_response = SupportsResponse .OPTIONAL ,
179- )
180-
181- hass .services .async_register (
182- DOMAIN ,
183- "get_tou_schedule" ,
184- services .get_tou_schedule ,
185- supports_response = SupportsResponse .OPTIONAL ,
186- )
187-
188- hass .services .async_register (
189- DOMAIN ,
190- "get_battery_reserve" ,
191- services .get_battery_reserve ,
192- supports_response = SupportsResponse .OPTIONAL ,
193- )
194-
195- hass .services .async_register (
196- DOMAIN ,
197- "get_operating_mode" ,
198- services .get_operating_mode ,
199- supports_response = SupportsResponse .OPTIONAL ,
200- )
123+ if sb_coordinator .latestData .get ('api_configuration' ,{}).get ('IN_LocalAPIWriteActive' , '0' ) == '1' :
124+ # service registration
125+ hass .services .async_register (
126+ DOMAIN ,
127+ "charge_battery" ,
128+ services .charge_battery ,
129+ schema = SCHEMA_CHARGE_BATTERY ,
130+ supports_response = SupportsResponse .OPTIONAL ,
131+ )
132+
133+ hass .services .async_register (
134+ DOMAIN ,
135+ "discharge_battery" ,
136+ services .discharge_battery ,
137+ schema = SCHEMA_CHARGE_BATTERY ,
138+ supports_response = SupportsResponse .OPTIONAL ,
139+ )
140+
141+ hass .services .async_register (
142+ DOMAIN ,
143+ "set_battery_reserve" ,
144+ services .set_battery_reserve ,
145+ schema = SCHEMA_SET_BATTERY_RESERVE ,
146+ supports_response = SupportsResponse .OPTIONAL ,
147+ )
148+
149+ hass .services .async_register (
150+ DOMAIN ,
151+ "set_config_item" ,
152+ services .set_config_item ,
153+ schema = SCHEMA_SET_CONFIG_ITEM ,
154+ supports_response = SupportsResponse .OPTIONAL ,
155+ )
156+
157+ hass .services .async_register (
158+ DOMAIN ,
159+ "set_operating_mode" ,
160+ services .set_operating_mode ,
161+ schema = SCHEMA_SET_OPERATING_MODE ,
162+ supports_response = SupportsResponse .OPTIONAL ,
163+ )
164+
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+
173+ hass .services .async_register (
174+ DOMAIN ,
175+ "set_tou_schedule" ,
176+ services .set_tou_schedule ,
177+ schema = SCHEMA_SET_TOU_SCHEDULE_STRING ,
178+ supports_response = SupportsResponse .OPTIONAL ,
179+ )
180+
181+ hass .services .async_register (
182+ DOMAIN ,
183+ "get_tou_schedule" ,
184+ services .get_tou_schedule ,
185+ supports_response = SupportsResponse .OPTIONAL ,
186+ )
187+
188+ hass .services .async_register (
189+ DOMAIN ,
190+ "get_battery_reserve" ,
191+ services .get_battery_reserve ,
192+ supports_response = SupportsResponse .OPTIONAL ,
193+ )
194+
195+ hass .services .async_register (
196+ DOMAIN ,
197+ "get_operating_mode" ,
198+ services .get_operating_mode ,
199+ supports_response = SupportsResponse .OPTIONAL ,
200+ )
201+
202+ hass .services .async_register (
203+ DOMAIN ,
204+ "get_operating_mode_num" ,
205+ services .get_operating_mode_num ,
206+ supports_response = SupportsResponse .OPTIONAL ,
207+ )
201208
202- hass .services .async_register (
203- DOMAIN ,
204- "get_operating_mode_num" ,
205- services .get_operating_mode_num ,
206- supports_response = SupportsResponse .OPTIONAL ,
207- )
209+ else :
210+ LOGGER .info (f"JSON-API write access not enabled - disabling SERVICE functions" )
208211
209212 # Done setting up the entry
210213 return True
0 commit comments