Skip to content

Commit

Permalink
Merge pull request #6 from gentslava/feature/reauth
Browse files Browse the repository at this point in the history
Feature - Reauth
  • Loading branch information
gentslava authored Dec 22, 2023
2 parents 465c339 + 4a75a01 commit 76e93fd
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 73 deletions.
47 changes: 34 additions & 13 deletions custom_components/elektronny_gorod/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
CONF_PHONE,
CONF_CONTRACT,
CONF_SMS,
CONF_OPERATOR_ID
CONF_OPERATOR_ID,
CONF_ACCOUNT_ID,
CONF_SUBSCRIBER_ID
)
from .api import ElektronnyGorodAPI
from .helpers import find
Expand Down Expand Up @@ -132,16 +134,31 @@ async def async_step_sms(
# Verify the SMS code
if self.access_token:
# If code is verified, create config entry
name = await self.get_name()
return self.async_create_entry(
title = name,
data = {
CONF_NAME: name,
CONF_ACCESS_TOKEN: self.access_token,
CONF_REFRESH_TOKEN: self.refresh_token,
CONF_OPERATOR_ID: self.operator_id
}
)
account = await self.get_account()
data = {
CONF_NAME: account[CONF_NAME],
CONF_ACCOUNT_ID: account[CONF_ACCOUNT_ID],
CONF_SUBSCRIBER_ID: account[CONF_SUBSCRIBER_ID],
CONF_ACCESS_TOKEN: self.access_token,
CONF_REFRESH_TOKEN: self.refresh_token,
CONF_OPERATOR_ID: self.operator_id,
}

for entry in self._async_current_entries():
if (data[CONF_ACCESS_TOKEN] == entry.data[CONF_ACCESS_TOKEN]):
LOGGER.info(f"Entry {entry.data} already exists")
return self.async_abort(reason = "already_configured")
if (
data[CONF_NAME] == entry.data[CONF_NAME]
and data[CONF_ACCOUNT_ID] == entry.data[CONF_ACCOUNT_ID]
and data[CONF_SUBSCRIBER_ID] == entry.data[CONF_SUBSCRIBER_ID]
):
LOGGER.info(f"Reauth entry {entry.data} with params {data}")
self.hass.config_entries.async_update_entry(entry, data = data)
await self.hass.config_entries.async_reload(entry.entry_id)
return self.async_abort(reason = "reauth_successful")

return self.async_create_entry(title = account[CONF_NAME], data = data)

# Authentication failed
errors[CONF_SMS] = "invalid_code"
Expand All @@ -152,8 +169,12 @@ async def async_step_sms(
errors = errors
)

async def get_name(self) -> str:
async def get_account(self) -> dict:
await self.api.update_access_token(self.access_token)
profile = await self.api.query_profile()
subscriber = profile["subscriber"]
return f"{subscriber['name']} (Account ID: {subscriber['accountId']})"
return {
CONF_NAME: f"{subscriber['name']} (Account ID: {subscriber['accountId']})",
CONF_ACCOUNT_ID: subscriber["accountId"],
CONF_SUBSCRIBER_ID: subscriber["id"]
}
4 changes: 3 additions & 1 deletion custom_components/elektronny_gorod/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
CONF_PHONE: Final = "phone"
CONF_CONTRACT: Final = "contract"
CONF_SMS: Final = "sms"
CONF_OPERATOR_ID: Final = "operatorId"
CONF_OPERATOR_ID: Final = "operator_id"
CONF_ACCOUNT_ID: Final = "account_id"
CONF_SUBSCRIBER_ID: Final = "subscriber_id"

CONF_WIDTH: Final = 300
CONF_HEIGHT: Final = 300
55 changes: 28 additions & 27 deletions custom_components/elektronny_gorod/strings.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
{
"title": "Elektronny Gorod Connect",
"config": {
"step": {
"user": {
"data": {
"phone": "Phone number or contract"
}
},
"contract": {
"data": {
"contract": "Contracts"
"title": "Elektronny Gorod Connect",
"config": {
"step": {
"user": {
"data": {
"phone": "Phone number or contract"
}
},
"contract": {
"data": {
"contract": "Contracts"
},
"description": "Choose your contract"
},
"sms": {
"data": {
"sms": "SMS"
},
"description": "Enter the code from SMS"
}
},
"description": "Choose your contract"
},
"sms": {
"data": {
"sms": "SMS"
"error": {
"invalid_code": "Invalid code",
"limit_exceeded": "The call limit has been exceeded. Please try again later.",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"description": "Enter the code from SMS"
}
},
"error": {
"invalid_code": "Invalid code",
"limit_exceeded": "The call limit has been exceeded. Please try again later.",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
}
}
}
}
3 changes: 2 additions & 1 deletion custom_components/elektronny_gorod/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"title": "Elektronny Gorod Connect",
"config": {
"abort": {
"already_configured": "Device is already configured"
"already_configured": "Device is already configured",
"reauth_successful": "Re-authentication was successful"
},
"error": {
"invalid_code": "Invalid code",
Expand Down
63 changes: 32 additions & 31 deletions custom_components/elektronny_gorod/translations/ru.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
{
"title": "Электронный город Connect",
"config": {
"abort": {
"already_configured": "Устройство уже настроено"
},
"error": {
"invalid_code": "Неверный код",
"limit_exceeded": "Превышен лимит обращений. Пожалуйста, повторите попытку позже.",
"unknown": "Неожиданная ошибка"
},
"step": {
"user": {
"data": {
"phone": "Номер телефона или догорвора",
"access_token": "Access token (при наличии)"
}
},
"contract": {
"data": {
"contract": "Договоры"
},
"description": "Выберите договор"
},
"sms": {
"data": {
"sms": "СМС"
},
"description": "Введите код из СМС"
}
}
}
"title": "Электронный город Connect",
"config": {
"abort": {
"already_configured": "Устройство уже настроено",
"reauth_successful": "Повторная аутентификация прошла успешно"
},
"error": {
"invalid_code": "Неверный код",
"limit_exceeded": "Превышен лимит обращений. Пожалуйста, повторите попытку позже.",
"unknown": "Неожиданная ошибка"
},
"step": {
"user": {
"data": {
"phone": "Номер телефона или догорвора",
"access_token": "Access token (при наличии)"
}
},
"contract": {
"data": {
"contract": "Договоры"
},
"description": "Выберите договор"
},
"sms": {
"data": {
"sms": "СМС"
},
"description": "Введите код из СМС"
}
}
}
}

0 comments on commit 76e93fd

Please sign in to comment.