Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend update to support new login method of keyid / apikey #497

Merged
merged 9 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions custom_components/wyzeapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .const import (
DOMAIN, CONF_CLIENT, ACCESS_TOKEN, REFRESH_TOKEN,
REFRESH_TIME, WYZE_NOTIFICATION_TOGGLE, BULB_LOCAL_CONTROL,
DEFAULT_LOCAL_CONTROL
DEFAULT_LOCAL_CONTROL, KEY_ID, API_KEY
)
from .token_manager import TokenManager

Expand Down Expand Up @@ -73,6 +73,8 @@ async def async_setup(
ACCESS_TOKEN: domainconfig[ACCESS_TOKEN],
REFRESH_TOKEN: domainconfig[REFRESH_TOKEN],
REFRESH_TIME: domainconfig[REFRESH_TIME],
KEY_ID: domainconfig[KEY_ID],
API_KEY: domainconfig[API_KEY]
},
)
)
Expand All @@ -85,6 +87,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b

hass.data.setdefault(DOMAIN, {})

key_id = config_entry.data.get(KEY_ID)
api_key = config_entry.data.get(API_KEY)

client = await Wyzeapy.create()
token = None
if config_entry.data.get(ACCESS_TOKEN):
Expand All @@ -101,14 +106,16 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
await client.login(
config_entry.data.get(CONF_USERNAME),
config_entry.data.get(CONF_PASSWORD),
key_id,
api_key,
token,
)
except Exception as e:
_LOGGER.error("Wyzeapi: Could not login. Please re-login through integration configuration")
_LOGGER.error(e)
raise ConfigEntryAuthFailed("Unable to login, please re-login.") from None

hass.data[DOMAIN][config_entry.entry_id] = {CONF_CLIENT: client}
hass.data[DOMAIN][config_entry.entry_id] = {CONF_CLIENT: client, "key_id": KEY_ID, "api_key": API_KEY}

options_dict = {BULB_LOCAL_CONTROL: config_entry.options.get(BULB_LOCAL_CONTROL, DEFAULT_LOCAL_CONTROL)}
hass.config_entries.async_update_entry(config_entry, options=options_dict)
Expand Down
19 changes: 13 additions & 6 deletions custom_components/wyzeapi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
from wyzeapy import Wyzeapy, exceptions

from .const import (
DOMAIN, ACCESS_TOKEN, REFRESH_TOKEN,
REFRESH_TIME, BULB_LOCAL_CONTROL,
DEFAULT_LOCAL_CONTROL
DOMAIN, ACCESS_TOKEN, REFRESH_TOKEN,
REFRESH_TIME, BULB_LOCAL_CONTROL,
DEFAULT_LOCAL_CONTROL,
KEY_ID, API_KEY
)

_LOGGER = logging.getLogger(__name__)

STEP_USER_DATA_SCHEMA = vol.Schema({CONF_USERNAME: str, CONF_PASSWORD: str})
STEP_USER_DATA_SCHEMA = vol.Schema({CONF_USERNAME: str, CONF_PASSWORD: str, KEY_ID: str, API_KEY: str})
STEP_2FA_DATA_SCHEMA = vol.Schema({CONF_ACCESS_TOKEN: str})


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Wyze Home Assistant Integration."""

Expand All @@ -36,6 +36,8 @@ def __init__(self):
"""Initialize."""
self.email = None
self.password = None
self.key_id = None
self.api_key = None

async def get_client(self):
if not self.client:
Expand All @@ -57,7 +59,10 @@ async def async_step_user(
# noinspection PyBroadException
try:
await self.client.login(
user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
user_input[KEY_ID],
user_input[API_KEY],
)
except CannotConnect:
errors["base"] = "cannot_connect"
Expand All @@ -66,6 +71,8 @@ async def async_step_user(
except exceptions.TwoFactorAuthenticationEnabled:
self.user_params[CONF_USERNAME] = user_input[CONF_USERNAME]
self.user_params[CONF_PASSWORD] = user_input[CONF_PASSWORD]
self.user_params[KEY_ID] = user_input[KEY_ID]
self.user_params[API_KEY] = user_input[API_KEY]
return await self.async_step_2fa()
else:
if self.hass.config_entries.async_entries(DOMAIN):
Expand Down
2 changes: 2 additions & 0 deletions custom_components/wyzeapi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
ACCESS_TOKEN = "access_token"
REFRESH_TOKEN = "refresh_token"
REFRESH_TIME = "refresh_time"
KEY_ID = "key_id"
API_KEY = "api_key"

WYZE_NOTIFICATION_TOGGLE = f"{DOMAIN}.wyze.notification.toggle"

Expand Down
4 changes: 2 additions & 2 deletions custom_components/wyzeapi/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/JoshuaMulliken/ha-wyzeapi/issues",
"requirements": [
"wyzeapy==0.5.19"
"wyzeapy==0.5.20"
],
"version": "0.1.19"
"version": "0.1.20"
}
8 changes: 6 additions & 2 deletions custom_components/wyzeapi/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"title": "[%key:common::config_flow::data::title%]",
"data": {
"username": "[%key:common::config_flow::data::username%]",
"password": "[%key:common::config_flow::data::password%]"
"password": "[%key:common::config_flow::data::password%]",
"keyid": "[%key:common::config_flow::data::key_id%]",
"apikey": "[%key:common::config_flow::data::api_key%]"
}
},
"2fa": {
Expand Down Expand Up @@ -36,7 +38,9 @@
"title": "[%key:common::config_flow::data::title%]",
"data": {
"username": "[%key:common::config_flow::data::username%]",
"password": "[%key:common::config_flow::data::password%]"
"password": "[%key:common::config_flow::data::password%]",
"keyid": "[%key:common::config_flow::data::key_id%]",
"apikey": "[%key:common::config_flow::data::api_key%]"
}
},
"2fa": {
Expand Down
1 change: 1 addition & 0 deletions custom_components/wyzeapi/token_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ async def inner_function(*args, **kwargs):
raise ConfigEntryAuthFailed("Unable to login, please re-login.") from err

return inner_function

35 changes: 20 additions & 15 deletions custom_components/wyzeapi/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
"user": {
"title": "Enter Wyze Login Credentials",
"data": {
"username": "Email",
"password": "Password",
"username": "Username"
"key_id": "Keyid",
"api_key": "Apikey"
}
},
"2fa": {
Expand All @@ -27,25 +29,19 @@
}
},
"options": {
"abort": {
"already_configured": "Device is already configured"
},
"error": {
"cannot_connect": "Failed to connect",
"invalid_auth": "Invalid authentication",
"unknown": "Unexpected error"
},
"step": {
"init":{
"data":{
"bulb_local_control": "Use Local Control for Color Bulbs and Light Strips"
"init": {
"data": {
"bulb_local_control": "Use Local Control for Color Bulbs and Light Strips"
}
},
},
"user": {
"title": "Enter Wyze Login Credentials",
"data": {
"username": "Email",
"password": "Password",
"username": "Username"
"key_id": "Keyid",
"api_key": "Apikey"
}
},
"2fa": {
Expand All @@ -54,6 +50,15 @@
"verification_code": "2FA Verification Code"
}
}
},
"error": {
"cannot_connect": "Failed to connect",
"invalid_auth": "Invalid authentication",
"unknown": "Unexpected error"
},
"abort": {
"already_configured": "Device is already configured",
"reauth_successful": "Reauthentication Successful"
}
}
}
}
Loading