11from __future__ import annotations
22from .constants import DOMAIN , PLATFORMS
33
4- from homeassistant .core import HomeAssistant
4+ from homeassistant .core import HomeAssistant , SupportsResponse
55from homeassistant .helpers .typing import ConfigType
66from homeassistant .helpers import service
77from homeassistant .helpers .update_coordinator import (
@@ -53,15 +53,23 @@ async def async_reboot(call):
5353
5454 async def async_exec (call ):
5555 parts = call .data ["command" ].split (" " )
56- for entry_id in await service .async_extract_config_entry_ids (hass , call ):
57- device = hass .data [DOMAIN ]["devices" ][entry_id ]
58- if device .is_api_supported ("file" ):
59- await device .do_file_exec (
60- parts [0 ],
61- parts [1 :],
62- call .data .get ("environment" , {}),
63- call .data .get ("extra" , {})
64- )
56+ ids = await service .async_extract_config_entry_ids (hass , call )
57+ response = {}
58+ for entry_id in ids :
59+ if coordinator := hass .data [DOMAIN ]["devices" ].get (entry_id ):
60+ if coordinator .is_api_supported ("file" ):
61+ args = parts [1 :]
62+ if "arguments" in call .data :
63+ args = call .data ["arguments" ].strip ().split ("\n " )
64+ response [entry_id ] = await coordinator .do_file_exec (
65+ parts [0 ],
66+ args ,
67+ call .data .get ("environment" , {}),
68+ call .data .get ("extra" , {})
69+ )
70+ if len (ids ) == 1 :
71+ return response .get (list (ids )[0 ])
72+ return response
6573
6674 async def async_init (call ):
6775 parts = call .data ["name" ].split (" " )
@@ -73,9 +81,24 @@ async def async_init(call):
7381 call .data .get ("action" , {})
7482 )
7583
84+ async def async_ubus (call ):
85+ response = {}
86+ ids = await service .async_extract_config_entry_ids (hass , call )
87+ for entry_id in ids :
88+ if coordinator := hass .data [DOMAIN ]["devices" ].get (entry_id ):
89+ response [entry_id ] = await coordinator .do_ubus_call (
90+ call .data .get ("subsystem" ),
91+ call .data .get ("method" ),
92+ call .data .get ("parameters" , {}),
93+ )
94+ if len (ids ) == 1 :
95+ return response .get (list (ids )[0 ])
96+ return response
97+
7698 hass .services .async_register (DOMAIN , "reboot" , async_reboot )
77- hass .services .async_register (DOMAIN , "exec" , async_exec )
99+ hass .services .async_register (DOMAIN , "exec" , async_exec , supports_response = SupportsResponse . OPTIONAL )
78100 hass .services .async_register (DOMAIN , "init" , async_init )
101+ hass .services .async_register (DOMAIN , "ubus" , async_ubus , supports_response = SupportsResponse .ONLY )
79102
80103 return True
81104
0 commit comments