Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def _send_state_command(self, state_command: str) -> None:
"""Instruct the API to change the state of the device."""
# If the user tries to open or close, say, a gateway, throw an exception:
if not self.state:
raise RequestError(
"Cannot change state of device type: {0}".format(self.device_type)
)
await self._api.request(
"put",
"Accounts/{0}/Devices/{1}/actions".format(
self._api.account_id, self.device_id
),
json={"action_type": state_command},
api_version=DEVICES_API_VERSION
)
for attempt in (0, DEFAULT_REQUEST_RETRIES):
try:
async with self._websession.request(
method, url, headers=headers, params=params, json=json, **kwargs
) as resp:
data = await resp.json(content_type=None)
resp.raise_for_status()
return data
except ClientError as err:
if "401" in str(err) and login_request:
raise InvalidCredentialsError(
"Invalid username/password"
)
if attempt == DEFAULT_REQUEST_RETRIES - 1:
raise RequestError(
"Error requesting data from {0}: {1}".format(
url, data.get("description", str(err))
)
)
wait_for = min(2 ** attempt, 5)
_LOGGER.warning(
"Device update failed; trying again in %s seconds", wait_for
)
await asyncio.sleep(wait_for)