4
4
import logging
5
5
import traceback
6
6
7
- from homeassistant .core import Context , SupportsResponse
7
+ from homeassistant .core import Context
8
8
9
- from .const import LOGGER_PATH
9
+ from .const import LOGGER_PATH , SERVICE_RESPONSE_NONE , SERVICE_RESPONSE_ONLY
10
10
11
11
_LOGGER = logging .getLogger (LOGGER_PATH + ".function" )
12
12
@@ -332,14 +332,7 @@ async def service_call(cls, domain, name, **kwargs):
332
332
elif default :
333
333
hass_args [keyword ] = default
334
334
335
- if "return_response" in hass_args and hass_args ["return_response" ] == True and "blocking" not in hass_args :
336
- hass_args ["blocking" ] = True
337
- elif "return_response" not in hass_args and cls .hass .services .supports_response (domain , name ) == SupportsResponse .ONLY :
338
- hass_args ["return_response" ] = True
339
- if "blocking" not in hass_args :
340
- hass_args ["blocking" ] = True
341
-
342
- return await cls .hass .services .async_call (domain , name , kwargs , ** hass_args )
335
+ return await cls .hass_services_async_call (domain , name , kwargs , ** hass_args )
343
336
344
337
@classmethod
345
338
async def service_completions (cls , root ):
@@ -413,19 +406,35 @@ async def service_call(*args, **kwargs):
413
406
if len (args ) != 0 :
414
407
raise TypeError (f"service { domain } .{ service } takes only keyword arguments" )
415
408
416
- if "return_response" in hass_args and hass_args ["return_response" ] == True and "blocking" not in hass_args :
417
- hass_args ["blocking" ] = True
418
- elif "return_response" not in hass_args and cls .hass .services .supports_response (domain , service ) == SupportsResponse .ONLY :
419
- hass_args ["return_response" ] = True
420
- if "blocking" not in hass_args :
421
- hass_args ["blocking" ] = True
422
-
423
- return await cls .hass .services .async_call (domain , service , kwargs , ** hass_args )
409
+ return await cls .hass_services_async_call (domain , service , kwargs , ** hass_args )
424
410
425
411
return service_call
426
412
427
413
return service_call_factory (domain , service )
428
414
415
+ @classmethod
416
+ async def hass_services_async_call (cls , domain , service , kwargs , ** hass_args ):
417
+ """Call a hass async service."""
418
+ if SERVICE_RESPONSE_ONLY is None :
419
+ # backwards compatibility < 2023.7
420
+ await cls .hass .services .async_call (domain , service , kwargs , ** hass_args )
421
+ else :
422
+ # allow service responses >= 2023.7
423
+ if (
424
+ "return_response" in hass_args
425
+ and hass_args ["return_response" ]
426
+ and "blocking" not in hass_args
427
+ ):
428
+ hass_args ["blocking" ] = True
429
+ elif (
430
+ "return_response" not in hass_args
431
+ and cls .hass .services .supports_response (domain , service ) == SERVICE_RESPONSE_ONLY
432
+ ):
433
+ hass_args ["return_response" ] = True
434
+ if "blocking" not in hass_args :
435
+ hass_args ["blocking" ] = True
436
+ return await cls .hass .services .async_call (domain , service , kwargs , ** hass_args )
437
+
429
438
@classmethod
430
439
async def run_coro (cls , coro , ast_ctx = None ):
431
440
"""Run coroutine task and update unique task on start and exit."""
@@ -466,7 +475,9 @@ def create_task(cls, coro, ast_ctx=None):
466
475
return cls .hass .loop .create_task (cls .run_coro (coro , ast_ctx = ast_ctx ))
467
476
468
477
@classmethod
469
- def service_register (cls , global_ctx_name , domain , service , callback , supports_response = SupportsResponse .NONE ):
478
+ def service_register (
479
+ cls , global_ctx_name , domain , service , callback , supports_response = SERVICE_RESPONSE_NONE
480
+ ):
470
481
"""Register a new service callback."""
471
482
key = f"{ domain } .{ service } "
472
483
if key not in cls .service_cnt :
@@ -478,7 +489,12 @@ def service_register(cls, global_ctx_name, domain, service, callback, supports_r
478
489
f"{ global_ctx_name } : can't register service { key } ; already defined in { cls .service2global_ctx [key ]} "
479
490
)
480
491
cls .service_cnt [key ] += 1
481
- cls .hass .services .async_register (domain , service , callback , supports_response = supports_response )
492
+ if SERVICE_RESPONSE_ONLY is None :
493
+ # backwards compatibility < 2023.7
494
+ cls .hass .services .async_register (domain , service , callback )
495
+ else :
496
+ # allow service responses >= 2023.7
497
+ cls .hass .services .async_register (domain , service , callback , supports_response = supports_response )
482
498
483
499
@classmethod
484
500
def service_remove (cls , global_ctx_name , domain , service ):
0 commit comments