Fix missing contracts issue for multiple accounts on single ID - #400
Fix missing contracts issue for multiple accounts on single ID#400IsraelBenDavid wants to merge 1 commit into
Conversation
Review Summary by QodoAdd fallback mechanism for missing contracts in multi-account scenarios
WalkthroughsDescription• Add fallback to get_customer() when masa API fails or returns empty accounts • Prevent "No active contracts found" error for multi-account users • Improve robustness by handling API failures gracefully • Extract BP numbers collection into reusable logic Diagramflowchart LR
A["get_masa_contact_account_user_profile"] -->|Success with accounts| B["Extract BP numbers"]
A -->|Fails or empty| C["Fallback: get_customer"]
C -->|Success| B
C -->|Fails| D["Return empty"]
B -->|BP numbers found| E["Fetch contracts"]
B -->|No BP numbers| D
File Changes1. custom_components/iec/config_flow.py
|
Code Review by Qodo
1. bp_numbers needs type annotation
|
| user_profile = None | ||
| bp_numbers = set() |
There was a problem hiding this comment.
1. bp_numbers needs type annotation 📘 Rule violation ≡ Correctness
The new bp_numbers = set() introduces an untyped empty collection, which typically fails mypy strict (it becomes set[Any] / requires an explicit annotation). This can break strict type-checking for the config flow code path.
Agent Prompt
## Issue description
`bp_numbers` is initialized as an empty set without a type annotation (`bp_numbers = set()`), which is commonly rejected by mypy in strict mode.
## Issue Context
This code runs in the config flow helper `_build_bp_number_to_contract()` and should pass strict type checking.
## Fix Focus Areas
- custom_components/iec/config_flow.py[123-124]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| except Exception as err: | ||
| _LOGGER.debug("Fallback to get_customer failed: %s", err) |
There was a problem hiding this comment.
2. Broad except lacks ble001 📘 Rule violation ⚙ Maintainability
A new except Exception as err: was added without # noqa: BLE001 or a narrower exception type, which will trigger Ruff’s BLE001 rule. This will cause linting/CI failures under the project’s formatting/style requirements.
Agent Prompt
## Issue description
The fallback block introduces `except Exception as err:` without `# noqa: BLE001` (or narrowing to specific exceptions), which is a Ruff BLE001 violation.
## Issue Context
Other broad exception handlers in this function use `# noqa: BLE001`, indicating the repo enforces this Ruff rule.
## Fix Focus Areas
- custom_components/iec/config_flow.py[145-146]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| ) -> tuple[dict[str, list[int]], dict[str, str]]: | ||
| bp_number_to_contract: dict[str, set[int]] = defaultdict(set) | ||
| contract_labels: dict[str, str] = {} | ||
|
|
There was a problem hiding this comment.
3. Whitespace-only blank line 🐞 Bug ⚙ Maintainability
A whitespace-only blank line was introduced inside _build_bp_number_to_contract(), which Ruff will flag as a pycodestyle whitespace violation and fail the CI lint job.
Agent Prompt
### Issue description
A whitespace-only blank line was added inside `_build_bp_number_to_contract()` (line contains only indentation). Ruff/pycodestyle flags this and CI will fail.
### Issue Context
CI runs `python3 -m ruff check .` and `.ruff.toml` enables pycodestyle `W` rules.
### Fix Focus Areas
- custom_components/iec/config_flow.py[119-125]
- .ruff.toml[5-27]
- .github/workflows/lint.yml[11-30]
### Proposed fix
Delete the indentation on the blank line (make it a truly empty line) or remove the blank line entirely.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
|
Have you tried the latest beta? It should be fixed there |
| return {}, {} | ||
|
|
||
| if not user_profile: | ||
| # Fallback: If masa API failed or returned empty accounts, try get_customer |
There was a problem hiding this comment.
This is a bit problematic, since I expect to use MASA API for shared-accounts logic (even for multiple accounts).
Is this your case? Let's discuss it
Added a fallback to get_customer() in _build_bp_number_to_contract. This fixes the No active contracts found error during OTP login for users who have multiple BP accounts under the same ID, which causes get_masa_contact_account_user_profile() to return empty accounts.