Skip to content

Comments

Avoid limit_current service call passing None-values and clarify error messages#284

Merged
sveinse merged 2 commits intocustom-components:masterfrom
steinmn:fix-none-values-in-limit-current-service
Aug 15, 2025
Merged

Avoid limit_current service call passing None-values and clarify error messages#284
sveinse merged 2 commits intocustom-components:masterfrom
steinmn:fix-none-values-in-limit-current-service

Conversation

@steinmn
Copy link
Contributor

@steinmn steinmn commented Aug 15, 2025

Fixes #283

@steinmn
Copy link
Contributor Author

steinmn commented Aug 15, 2025

Seems slightly paranoid to fix it both in api.py and in service.py, but on the other hand set_limit_current might be used elsewhere in the future, so might as well try to futureproof a bit.

Comment on lines 284 to 291
if available_current := service_call.data.get("available_current"):
limit_args["availableCurrent"] = available_current
if available_current_phase1 := service_call.data.get("available_current_phase1"):
limit_args["availableCurrentPhase1"] = available_current_phase1
if available_current_phase2 := service_call.data.get("available_current_phase2"):
limit_args["availableCurrentPhase2"] = available_current_phase2
if available_current_phase3 := service_call.data.get("available_current_phase3"):
limit_args["availableCurrentPhase3"] = available_current_phase3
Copy link
Collaborator

Choose a reason for hiding this comment

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

This won't work if the value is being set to 0. It needs to be explicitly tested for None.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦 tried to be a little bit too elegant, fixed now

if not (has_availablecurrent ^ all(has_availablecurrentphases)):
raise ValueError(
"Either availableCurrent or all of availableCurrentPhase1, "
"Either availableCurrent OR all of availableCurrentPhase1, "
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not overly fond of using capital letters in the middle of an English sentence

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wanted to add emphasis to make it clearer that it's an exclusive or, but I'm fine either way

Comment on lines 557 to 562
if has_availablecurrent and any(has_availablecurrentphases):
raise ValueError(
"A combination of both availableCurrent and any of "
"availableCurrentPhase1, availableCurrentPhase2, "
"availableCurrentPhase3 is not allowed, choose one"
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This error is a little confusing to me. Perhaps it's better to test that if any of the three (with any()) have used, and all of them must be present. Error: "All of availableCurrent1, availableCurrent2 and availableCurrent2 must be present".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified

@steinmn steinmn requested a review from sveinse August 15, 2025 15:48
Copy link
Collaborator

@sveinse sveinse left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you for fixing.

@sveinse sveinse merged commit 722589c into custom-components:master Aug 15, 2025
3 checks passed
@steinmn steinmn deleted the fix-none-values-in-limit-current-service branch August 15, 2025 16:40
@sveinse sveinse added this to the v0.8.2 milestone Aug 17, 2025
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.

Error when calling limit_current: '<=' not supported between instances of 'int' and 'NoneType' after recent update

2 participants