Skip to content

Commit 0a265d9

Browse files
committed
Store country_code for login optimisation
1 parent 1a5159c commit 0a265d9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

custom_components/sonoff/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .core.const import (
3131
CONF_APPID,
3232
CONF_APPSECRET,
33+
CONF_COUNTRY_CODE,
3334
CONF_DEFAULT_CLASS,
3435
CONF_DEVICEKEY,
3536
CONF_RFBRIDGE,
@@ -175,11 +176,18 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
175176
hass.data[DOMAIN][config_entry.entry_id] = registry = XRegistry(session)
176177

177178
mode = config_entry.options.get(CONF_MODE, "auto")
179+
data = config_entry.data
178180

179181
# if has cloud password and not auth
180-
if not registry.cloud.auth and config_entry.data.get(CONF_PASSWORD):
182+
if not registry.cloud.auth and data.get(CONF_PASSWORD):
181183
try:
182-
await registry.cloud.login(**config_entry.data)
184+
await registry.cloud.login(**data)
185+
# store country_code for future requests optimisation
186+
if not data.get(CONF_COUNTRY_CODE):
187+
hass.config_entries.async_update_entry(
188+
config_entry,
189+
data={**data, CONF_COUNTRY_CODE: registry.cloud.country_code},
190+
)
183191
except Exception as e:
184192
_LOGGER.warning(f"Can't login in {mode} mode: {repr(e)}")
185193
if mode == "cloud":

custom_components/sonoff/core/ewelink/cloud.py

+4
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ def headers(self) -> dict:
345345
def token(self) -> str:
346346
return self.region + ":" + self.auth["at"]
347347

348+
@property
349+
def country_code(self) -> str:
350+
return self.auth["user"]["countryCode"]
351+
348352
async def login(
349353
self, username: str, password: str, country_code: str = "+86", app: int = 0
350354
) -> bool:

0 commit comments

Comments
 (0)