Skip to content

Fix missing contracts issue for multiple accounts on single ID - #400

Open
IsraelBenDavid wants to merge 1 commit into
GuyKh:mainfrom
IsraelBenDavid:fix-multiple-accounts
Open

Fix missing contracts issue for multiple accounts on single ID#400
IsraelBenDavid wants to merge 1 commit into
GuyKh:mainfrom
IsraelBenDavid:fix-multiple-accounts

Conversation

@IsraelBenDavid

Copy link
Copy Markdown

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.

@qodo-code-review

Copy link
Copy Markdown

Review Summary by Qodo

Add fallback mechanism for missing contracts in multi-account scenarios

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• 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
Diagram
flowchart 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
Loading

Grey Divider

File Changes

1. custom_components/iec/config_flow.py 🐞 Bug fix +22/-9

Add fallback mechanism for multi-account contract resolution

• Refactored _build_bp_number_to_contract() to initialize user_profile and bp_numbers
 variables upfront
• Extract BP numbers from user profile accounts into a set during initial API call
• Add fallback logic using get_customer() when masa API fails or returns empty accounts
• Improve error handling by logging fallback failures without early return
• Simplify type annotation for contracts variable

custom_components/iec/config_flow.py


Grey Divider

Qodo Logo

@qodo-code-review

qodo-code-review Bot commented Apr 4, 2026

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (2) 📎 Requirement gaps (0) 🎨 UX Issues (0)

Grey Divider


Action required

1. bp_numbers needs type annotation 📘 Rule violation ≡ Correctness
Description
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.
Code

custom_components/iec/config_flow.py[R123-124]

+    user_profile = None
+    bp_numbers = set()
Evidence
PR Compliance ID 2 requires strict mypy compatibility and full type-annotation; the modified code
adds an empty set() assigned to bp_numbers without a concrete type, a common strict-mode mypy
error requiring an explicit annotation.

AGENTS.md
custom_components/iec/config_flow.py[123-124]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## 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


2. Broad except lacks BLE001 📘 Rule violation ⚙ Maintainability
Description
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.
Code

custom_components/iec/config_flow.py[R145-146]

+        except Exception as err:
+            _LOGGER.debug("Fallback to get_customer failed: %s", err)
Evidence
PR Compliance ID 1 requires code to be free of Ruff violations; the new fallback block catches a
broad Exception without an explicit suppression or specific exception type, which violates Ruff
BLE001.

AGENTS.md
custom_components/iec/config_flow.py[145-146]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## 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


3. Whitespace-only blank line 🐞 Bug ⚙ Maintainability
Description
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.
Code

custom_components/iec/config_flow.py[122]

+    
Evidence
The modified function now contains an indentation-only blank line. The repository’s CI runs `ruff
check ., and Ruff is configured to enforce pycodestyle W` rules, which include whitespace-only
line checks (e.g., W293).

custom_components/iec/config_flow.py[117-125]
.ruff.toml[5-27]
.github/workflows/lint.yml[11-30]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### 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


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

Comment on lines +123 to +124
user_profile = None
bp_numbers = set()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

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

Comment on lines +145 to +146
except Exception as err:
_LOGGER.debug("Fallback to get_customer failed: %s", err)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

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] = {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

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

@GuyKh

GuyKh commented Apr 4, 2026

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants