async_step_token is shared by the user and reauth sources but was written
only for the former. It called _abort_if_unique_id_configured() on every
path, and during reauth the unique ID belongs to the entry being repaired,
so the duplicate guard fired by design: authentication succeeded, the flow
aborted with already_configured, the new tokens were discarded, and the
coordinator's next poll re-raised the reauth repair. The integration was
unrecoverable through the UI short of delete-and-re-add, which costs entity
history and the restored keg-freshness baseline.
Branch on self.source at the terminal step, per HA's documented pattern for
flows shared between setup and reauth: reauth now guards with
_abort_if_unique_id_mismatch() and persists through
async_update_reload_and_abort(), which merges the new tokens into the
existing entry, keeps email and machine ID, reloads, and aborts with
reauth_successful. The user path keeps _abort_if_unique_id_configured()
unchanged.
Reauth also stops re-fetching /api/me when the entry already records a
machine ID, removing a round trip from a flow the user waits on and a
failure mode where a transient /api/me error turned a successful sign-in
into cannot_connect.
The helpers require core 2024.11, so declare that floor in hacs.json —
otherwise older cores would AttributeError mid-flow, which is worse than
the bug being fixed.
Adds the repo's first HA test harness (requirements-test.txt, opt-in) with
regression tests covering token persistence, entry reload, account
mismatch, case-insensitive email, and the explicit assertion that reauth
never aborts as already_configured. tests/test_keg_detection.py still runs
standalone under python3 -m unittest.
Version set to 0.3.1 rather than the 0.4.0 that PR #2 introduced; no
v0.4.0 tag was ever cut, so nothing was released under that number.
Co-authored-by: Cursor <cursoragent@cursor.com>
Breakthrough: discovered the web reCAPTCHA Enterprise key
(6LcZQiUo...) from perfectdraft.com with action "Magento/login"
is accepted by the /authentication/sign-in API endpoint when
generated from a real browser. Token refresh works via direct
AWS Cognito InitiateAuth (bypasses API gateway, no reCAPTCHA).
Cognito details extracted from JWT:
Pool: eu-west-1_UXWVyvlHR
Client: 57ddq2ppqg2jcpup06r2g1deur
Real API response fields mapped:
/api/me → perfectdraftMachines[].id
/api/perfectdraft_machines/{id} → details.temperature,
details.kegVolume, details.connectedState, details.doorClosed,
details.numberOfPoursSinceStartup, details.volumeOfLastPour,
details.firmwareVersion, details.serialNumber, setting.mode
Changes:
- api.py: Cognito direct refresh, correct sign-in endpoint
- config_flow.py: external step with web reCAPTCHA on HA page
- const.py: Cognito config, web reCAPTCHA key
- coordinator.py: real field name extraction
- sensor.py: 8 sensors with real field mappings (temperature,
keg remaining %, connection, door, pours, last pour, firmware, mode)
- Removed image.py (API doesn't expose keg images)
- Restored static/recaptcha.html with Enterprise JS
Made-with: Cursor
Decompiled PerfectDraft APK kept locally for reference.
Emulator approach failed: ARM64 native libs can't run on x86_64
emulator, and ARM64 emulation isn't supported by Android emulator
on x86_64 hosts.
Made-with: Cursor
test_harness.py exercises the full API flow without HA.
Credentials go in .credentials.json (gitignored).
Key findings from testing:
- reCAPTCHA is Enterprise, not standard v3
- Server-side anchor/reload tokens work but carry wrong action
- Playwright tokens carry correct action but get low trust score
- API validates both action string AND score threshold
- The exact action string is "Android_recaptchaThatWorks/login"
- Auth endpoint: POST /authentication/sign-in returns
AccessToken, IdToken, RefreshToken on success
Made-with: Cursor
Scaffolds the repo with agent workflow config (AGENTS.md, .gitignore),
openspec tooling, and the perfectdraft-integration change proposal
with design, specs (api-client, config-flow, entities, hacs-packaging),
and implementation tasks — ready for apply phase.
Made-with: Cursor