77from aiohttp import ClientResponse
88
99from tahoma_api .exceptions import BadCredentialsException , TooManyRequestsException
10- from tahoma_api .models import Command , CommandMode , Device , Scenario , State
10+ from tahoma_api .models import Command , Device , Scenario , State
1111
1212JSON = Union [Dict [str , Any ], List [Dict [str , Any ]]]
1313
@@ -44,7 +44,7 @@ async def login(self) -> bool:
4444 Caller must provide one of [userId+userPassword, userId+ssoToken, accessToken, jwt]
4545 """
4646 payload = {"userId" : self .username , "userPassword" : self .password }
47- response = await self .__post ("login" , payload )
47+ response = await self .__post ("login" , data = payload )
4848
4949 if response .get ("success" ):
5050 self .__roles = response .get ("roles" )
@@ -103,30 +103,6 @@ async def fetch_event_listener(self, listener_id: str) -> List[Any]:
103103
104104 return response
105105
106- async def execute_action_group (
107- self ,
108- actions : List [Command ],
109- label : str = "python-tahoma-api" ,
110- mode : Optional [CommandMode ] = None ,
111- ) -> List [Any ]:
112- """ Execute a non-persistent action group.
113- The executed action group does not have to be persisted on the server before
114- use.
115- Per-session rate-limit : 50 calls per 24 HOURS period for all operations of the
116- same category (exec)
117- """
118- payload = {"label" : label , "actions" : actions }
119- supported_modes = ["geolocated" , "highPriority" , "internal" ]
120- endpoint = "exec/apply"
121-
122- # TODO Change mode / supported_modes to ENUM
123- if mode in supported_modes :
124- endpoint = f"{ endpoint } /{ mode } "
125-
126- response = await self .__post (endpoint , payload )
127-
128- return response
129-
130106 async def get_current_execution (self , exec_id : str ) -> List [Any ]:
131107 """ Get an action group execution currently running """
132108 response = await self .__get (f"/exec/current/{ exec_id } " )
@@ -141,6 +117,31 @@ async def get_current_executions(self) -> List[Any]:
141117
142118 return response
143119
120+ async def execute_command (
121+ self ,
122+ device_url : str ,
123+ command : Union [Command , str ],
124+ label : Optional [str ] = "python-tahoma-api" ,
125+ ) -> str :
126+ """ Send a command """
127+ if isinstance (command , str ):
128+ command = Command (command )
129+ return await self .execute_commands (device_url , [command ], label )
130+
131+ async def execute_commands (
132+ self ,
133+ device_url : str ,
134+ commands : List [Command ],
135+ label : Optional [str ] = "python-tahoma-api" ,
136+ ) -> str :
137+ """ Send several commands in one call """
138+ payload = {
139+ "label" : label ,
140+ "actions" : [{"deviceURL" : device_url , "commands" : commands }],
141+ }
142+ response = await self .__post ("exec/apply" , payload )
143+ return response ["execId" ]
144+
144145 async def get_scenarios (self ) -> List [Scenario ]:
145146 """ List the scenarios """
146147 response = await self .__get ("actionGroups" )
@@ -157,10 +158,12 @@ async def __get(self, endpoint: str) -> Any:
157158 await self .check_response (response )
158159 return await response .json ()
159160
160- async def __post (self , endpoint : str , payload : Optional [JSON ] = None ,) -> Any :
161+ async def __post (
162+ self , endpoint : str , payload : Optional [JSON ] = None , data : Optional [JSON ] = None
163+ ) -> Any :
161164 """ Make a POST request to the TaHoma API """
162165 async with self .session .post (
163- f"{ self .api_url } { endpoint } " , data = payload
166+ f"{ self .api_url } { endpoint } " , data = data , json = payload
164167 ) as response :
165168 await self .check_response (response )
166169 return await response .json ()
0 commit comments