Replace broken QR login with manual JSESSIONID cookie
Samsung rebuilt account.samsung.com as a JS SPA backed by /iam/oauth2, so the integration's HTML-scraping QR flow can no longer find the signin URL and /accounts/v1/FMM2/signInWithQrCode now 404s. The STF backend (chkLogin.do, getDeviceList.do, ...) is unchanged. Replace the multi-step QR config flow with a single form that asks the user to paste the JSESSIONID cookie copied from a logged-in browser session at smartthingsfind.samsung.com. validate_jsessionid hits chkLogin.do to verify the cookie before accepting it. Also bundle in earlier compatibility fixes for newer HA: store devices in hass.data so the coordinator can look them up by entry_id, and use async_on_update on the device_tracker entity. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
13bd1dabe0
commit
13ff5a534e
7 changed files with 126 additions and 133 deletions
|
|
@ -1,11 +1,11 @@
|
|||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
import voluptuous as vol
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.config_entries import (
|
||||
ConfigEntry,
|
||||
ConfigFlowResult,
|
||||
OptionsFlowWithConfigEntry
|
||||
OptionsFlowWithConfigEntry,
|
||||
)
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
|
|
@ -15,130 +15,65 @@ from .const import (
|
|||
CONF_ACTIVE_MODE_SMARTTAGS,
|
||||
CONF_ACTIVE_MODE_SMARTTAGS_DEFAULT,
|
||||
CONF_ACTIVE_MODE_OTHERS,
|
||||
CONF_ACTIVE_MODE_OTHERS_DEFAULT
|
||||
CONF_ACTIVE_MODE_OTHERS_DEFAULT,
|
||||
)
|
||||
from .utils import do_login_stage_one, do_login_stage_two, gen_qr_code_base64
|
||||
import asyncio
|
||||
from .utils import validate_jsessionid
|
||||
import logging
|
||||
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SmartThingsFindConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for SmartThings Find."""
|
||||
"""Handle a config flow for SmartThings Find.
|
||||
|
||||
Samsung's account.samsung.com login was rebuilt as a JS SPA (IAM/OAuth2
|
||||
with bot protection), so the original QR-code login flow no longer works.
|
||||
Until that is reverse-engineered we fall back to letting the user paste
|
||||
the JSESSIONID cookie they obtain from a normal browser session at
|
||||
https://smartthingsfind.samsung.com/.
|
||||
"""
|
||||
|
||||
VERSION = 1
|
||||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
||||
|
||||
reauth_entry: ConfigEntry | None = None
|
||||
|
||||
task_stage_one: asyncio.Task | None = None
|
||||
task_stage_two: asyncio.Task | None = None
|
||||
|
||||
qr_url = None
|
||||
session = None
|
||||
|
||||
jsessionid = None
|
||||
|
||||
|
||||
error = None
|
||||
|
||||
async def do_stage_one(self):
|
||||
_LOGGER.debug("Running login stage 1")
|
||||
try:
|
||||
stage_one_res = await do_login_stage_one(self.hass)
|
||||
if not stage_one_res is None:
|
||||
self.session, self.qr_url = stage_one_res
|
||||
else:
|
||||
self.error = "Login stage 1 failed. Check logs for details."
|
||||
_LOGGER.warn("Login stage 1 failed")
|
||||
_LOGGER.debug("Login stage 1 done")
|
||||
except Exception as e:
|
||||
self.error = "Login stage 1 failed. Check logs for details."
|
||||
_LOGGER.error(f"Exception in stage 1: {e}", exc_info=True)
|
||||
|
||||
async def do_stage_two(self):
|
||||
_LOGGER.debug("Running login stage 2")
|
||||
try:
|
||||
stage_two_res = await do_login_stage_two(self.session)
|
||||
if not stage_two_res is None:
|
||||
self.jsessionid = stage_two_res
|
||||
_LOGGER.info("Login successful")
|
||||
else:
|
||||
self.error = "Login stage 2 failed. Check logs for details."
|
||||
_LOGGER.warning("Login stage 2 failed")
|
||||
_LOGGER.debug("Login stage 2 done")
|
||||
except Exception as e:
|
||||
self.error = "Login stage 2 failed. Check logs for details."
|
||||
_LOGGER.error(f"Exception in stage 2: {e}", exc_info=True)
|
||||
|
||||
# First step: Get QR Code login URL
|
||||
async def async_step_user(self, user_input=None):
|
||||
_LOGGER.debug("Entering login stage 1")
|
||||
if not self.task_stage_one:
|
||||
self.task_stage_one = self.hass.async_create_task(self.do_stage_one())
|
||||
if not self.task_stage_one.done():
|
||||
return self.async_show_progress(
|
||||
progress_action="task_stage_one",
|
||||
progress_task=self.task_stage_one
|
||||
)
|
||||
# At this point stage 1 is completed
|
||||
if self.error:
|
||||
# An error occurred, cancel the flow by moving to the finish-
|
||||
# form which shows the error
|
||||
return self.async_show_progress_done(next_step_id="finish")
|
||||
# No error -> Proceed to stage 2
|
||||
return self.async_show_progress_done(next_step_id="auth_stage_two")
|
||||
|
||||
# Second step: Wait until QR scanned an log in
|
||||
async def async_step_auth_stage_two(self, user_input=None):
|
||||
if not self.task_stage_two:
|
||||
self.task_stage_two = self.hass.async_create_task(self.do_stage_two())
|
||||
if not self.task_stage_two.done():
|
||||
return self.async_show_progress(
|
||||
progress_action="task_stage_two",
|
||||
progress_task=self.task_stage_two,
|
||||
description_placeholders={
|
||||
"qr_code": gen_qr_code_base64(self.qr_url),
|
||||
"url": self.qr_url,
|
||||
"code": self.qr_url.split('/')[-1],
|
||||
}
|
||||
)
|
||||
return self.async_show_progress_done(next_step_id="finish")
|
||||
|
||||
async def async_step_finish(self, user_input=None):
|
||||
if self.error:
|
||||
return self.async_show_form(step_id="finish", errors={'base': self.error})
|
||||
|
||||
data={CONF_JSESSIONID: self.jsessionid}
|
||||
|
||||
async def _async_handle_jsessionid(self, jsessionid: str) -> ConfigFlowResult:
|
||||
data = {CONF_JSESSIONID: jsessionid}
|
||||
if self.reauth_entry:
|
||||
# Finish step was called by reauth-flow. Do not create a new entry,
|
||||
# instead update the existing entry
|
||||
return self.async_update_reload_and_abort(
|
||||
self.reauth_entry,
|
||||
data=data
|
||||
)
|
||||
|
||||
return self.async_update_reload_and_abort(self.reauth_entry, data=data)
|
||||
return self.async_create_entry(title="SmartThings Find", data=data)
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
errors: dict[str, str] = {}
|
||||
if user_input is not None:
|
||||
jsessionid = (user_input.get(CONF_JSESSIONID) or "").strip()
|
||||
if not jsessionid:
|
||||
errors["base"] = "empty_cookie"
|
||||
elif await validate_jsessionid(self.hass, jsessionid):
|
||||
return await self._async_handle_jsessionid(jsessionid)
|
||||
else:
|
||||
errors["base"] = "invalid_auth"
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema({vol.Required(CONF_JSESSIONID): str}),
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_reauth(self, user_input=None):
|
||||
self.reauth_entry = self.hass.config_entries.async_get_entry(
|
||||
self.context["entry_id"]
|
||||
)
|
||||
return await self.async_step_reauth_confirm()
|
||||
|
||||
async def async_step_reauth_confirm(self, user_input=None):
|
||||
if user_input is None:
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema({}),
|
||||
)
|
||||
return await self.async_step_user()
|
||||
|
||||
|
||||
async def async_step_reconfigure(self, user_input: dict[str, Any] | None = None):
|
||||
return await self.async_step_reauth_confirm(self)
|
||||
|
||||
self.reauth_entry = self.hass.config_entries.async_get_entry(
|
||||
self.context["entry_id"]
|
||||
)
|
||||
return await self.async_step_user()
|
||||
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(
|
||||
|
|
@ -146,8 +81,8 @@ class SmartThingsFindConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
) -> config_entries.OptionsFlow:
|
||||
"""Create the options flow."""
|
||||
return SmartThingsFindOptionsFlowHandler(config_entry)
|
||||
|
||||
|
||||
|
||||
|
||||
class SmartThingsFindOptionsFlowHandler(OptionsFlowWithConfigEntry):
|
||||
"""Handle an options flow."""
|
||||
|
||||
|
|
@ -186,4 +121,4 @@ class SmartThingsFindOptionsFlowHandler(OptionsFlowWithConfigEntry):
|
|||
): bool,
|
||||
}
|
||||
)
|
||||
return self.async_show_form(step_id="init", data_schema=data_schema)
|
||||
return self.async_show_form(step_id="init", data_schema=data_schema)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue