44import logging
55import traceback
66
7- from homeassistant .core import Context , SupportsResponse
7+ from homeassistant .core import Context
88
9- from .const import LOGGER_PATH
9+ from .const import LOGGER_PATH , SERVICE_RESPONSE_NONE , SERVICE_RESPONSE_ONLY
1010
1111_LOGGER = logging .getLogger (LOGGER_PATH + ".function" )
1212
@@ -332,14 +332,7 @@ async def service_call(cls, domain, name, **kwargs):
332332 elif default :
333333 hass_args [keyword ] = default
334334
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 )
343336
344337 @classmethod
345338 async def service_completions (cls , root ):
@@ -413,19 +406,35 @@ async def service_call(*args, **kwargs):
413406 if len (args ) != 0 :
414407 raise TypeError (f"service { domain } .{ service } takes only keyword arguments" )
415408
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 )
424410
425411 return service_call
426412
427413 return service_call_factory (domain , service )
428414
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+
429438 @classmethod
430439 async def run_coro (cls , coro , ast_ctx = None ):
431440 """Run coroutine task and update unique task on start and exit."""
@@ -466,7 +475,9 @@ def create_task(cls, coro, ast_ctx=None):
466475 return cls .hass .loop .create_task (cls .run_coro (coro , ast_ctx = ast_ctx ))
467476
468477 @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+ ):
470481 """Register a new service callback."""
471482 key = f"{ domain } .{ service } "
472483 if key not in cls .service_cnt :
@@ -478,7 +489,12 @@ def service_register(cls, global_ctx_name, domain, service, callback, supports_r
478489 f"{ global_ctx_name } : can't register service { key } ; already defined in { cls .service2global_ctx [key ]} "
479490 )
480491 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 )
482498
483499 @classmethod
484500 def service_remove (cls , global_ctx_name , domain , service ):
0 commit comments