Skip to content

Commit

Permalink
Store country_code for login optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Sep 17, 2024
1 parent 1a5159c commit 0a265d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 10 additions & 2 deletions custom_components/sonoff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .core.const import (
CONF_APPID,
CONF_APPSECRET,
CONF_COUNTRY_CODE,
CONF_DEFAULT_CLASS,
CONF_DEVICEKEY,
CONF_RFBRIDGE,
Expand Down Expand Up @@ -175,11 +176,18 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
hass.data[DOMAIN][config_entry.entry_id] = registry = XRegistry(session)

mode = config_entry.options.get(CONF_MODE, "auto")
data = config_entry.data

# if has cloud password and not auth
if not registry.cloud.auth and config_entry.data.get(CONF_PASSWORD):
if not registry.cloud.auth and data.get(CONF_PASSWORD):
try:
await registry.cloud.login(**config_entry.data)
await registry.cloud.login(**data)
# store country_code for future requests optimisation
if not data.get(CONF_COUNTRY_CODE):
hass.config_entries.async_update_entry(
config_entry,
data={**data, CONF_COUNTRY_CODE: registry.cloud.country_code},
)
except Exception as e:
_LOGGER.warning(f"Can't login in {mode} mode: {repr(e)}")
if mode == "cloud":
Expand Down
4 changes: 4 additions & 0 deletions custom_components/sonoff/core/ewelink/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ def headers(self) -> dict:
def token(self) -> str:
return self.region + ":" + self.auth["at"]

@property
def country_code(self) -> str:
return self.auth["user"]["countryCode"]

async def login(
self, username: str, password: str, country_code: str = "+86", app: int = 0
) -> bool:
Expand Down

0 comments on commit 0a265d9

Please sign in to comment.