Fix UnboundLocalError in check_tarif when generate_energy_meters is disabled - #1015
Fix UnboundLocalError in check_tarif when generate_energy_meters is disabled#1015njloof wants to merge 2 commits into
Conversation
…isabled When the 'Generate energy meters' option is disabled in the integration configuration, the check_tarif() method crashes with: UnboundLocalError: cannot access local variable 'tarif_config' where it is not associated with a value Root cause: the variable tarif_config (along with energy_used, plan_name, base_sensor, and tarif) is only assigned inside the 'if self.generate_energy_meters:' block, but the code that uses these variables (the for loop iterating tarif_config.items(), the threshold check, the high_times check, and the tarif select update) was placed outside that block at a lower indentation level. This means every coordinator update cycle crashes when energy meters are disabled, flooding the logs with errors. Fix: move all code that depends on variables defined within the 'if self.generate_energy_meters:' block to be inside that block. When the option is disabled, check_tarif() now simply returns without action. Tested on Home Assistant 2026.8.2 (Green) with both generate_energy_meters enabled and disabled — no errors in either case.
|
Can you share logging? I cannot reproduce it here on multiple hosts. |
|
Sorry about that -- good callout. Reproduced on Home Assistant 2026.8.2 (Home Assistant Green, HAOS 18.2) The error fires every coordinator update cycle (~10 seconds), flooding the logs: When generate_energy_meters is False but track_unknown_sources is True, Steps to reproduce:
|
Track unknown source is meant to be used with energy meters and is not defaulted to true, as it generates one as well. I am seriously thinking of removing that checkbox entirely as now HA tracks this natively (and has since.. 2025.9 I think?) unless you specifically need the power meter itself for something. |
|
Nope, not using the unknown source data tracking anyway -- so if the simpler solution is just to kill that option, feel free to decline this patch |
When the 'Generate energy meters' option is disabled in the integration configuration, the check_tarif() method crashes with:
UnboundLocalError: cannot access local variable 'tarif_config' where
it is not associated with a value
Root cause: the variable tarif_config (along with energy_used, plan_name, base_sensor, and tarif) is only assigned inside the 'if self.generate_energy_meters:' block, but the code that uses these variables (the for loop iterating tarif_config.items(), the threshold check, the high_times check, and the tarif select update) was placed outside that block at a lower indentation level.
This means every coordinator update cycle crashes when energy meters are disabled, flooding the logs with errors.
Fix: move all code that depends on variables defined within the 'if self.generate_energy_meters:' block to be inside that block. When the option is disabled, check_tarif() now simply returns without action.
Tested on Home Assistant 2026.8.2 (Green) with both generate_energy_meters enabled and disabled — no errors in either case.