Add update-credentials command (#620)

* Add change credentials command

* Rename command and add prompts for credential update
This commit is contained in:
Teemu R 2024-01-05 02:25:15 +01:00 committed by GitHub
parent 554fe0a96e
commit 2d8a8d9511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -997,5 +997,28 @@ async def turn_on_behavior(dev: SmartBulb, type, last, preset):
return await dev.set_turn_on_behavior(settings) return await dev.set_turn_on_behavior(settings)
@cli.command()
@pass_dev
@click.option(
"--username", required=True, prompt=True, help="New username to set on the device"
)
@click.option(
"--password", required=True, prompt=True, help="New password to set on the device"
)
async def update_credentials(dev, username, password):
"""Update device credentials for authenticated devices."""
# Importing here as this is not really a public interface for now
from kasa.tapo import TapoDevice
if not isinstance(dev, TapoDevice):
raise NotImplementedError(
"Credentials can only be updated on authenticated devices."
)
click.confirm("Do you really want to replace the existing credentials?", abort=True)
return await dev.update_credentials(username, password)
if __name__ == "__main__": if __name__ == "__main__":
cli() cli()

View File

@ -321,3 +321,18 @@ class TapoDevice(SmartDevice):
raise raise
_LOGGER.debug("Received an expected for wifi join, but this is expected") _LOGGER.debug("Received an expected for wifi join, but this is expected")
async def update_credentials(self, username: str, password: str):
"""Update device credentials.
This will replace the existing authentication credentials on the device.
"""
t = self.internal_state["time"]
payload = {
"account": {
"username": base64.b64encode(username.encode()).decode(),
"password": base64.b64encode(password.encode()).decode(),
},
"time": t,
}
return await self.protocol.query({"set_qs_info": payload})