-
Notifications
You must be signed in to change notification settings - Fork 134
Fix multiple bugs in services and const #519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix multiple bugs in services and const #519
Conversation
- Fix weekly service calling yearly() instead of weekly()
- Fix area validator not raising vol.Invalid (missing raise keyword)
- Fix year schema default using wrong strftime format ("Y" -> "%Y")
- Fix typo in AREA_TO_COUNTRY: "PL " -> "PL" (trailing space)
- Fix typo in error message: "in not in on of" -> "is not in one of"
|
Solid changes. Thanks! |
|
Hi, @Hellowlol What timelane you have to merge this PR? |
|
Hi, @Metroseksuaali |
|
Hi @dana-se, thanks for reporting this! The error you're seeing is actually a pre-existing bug in the check_setting validator on line 21 of services.py, which our PR did not modify. Here's what's happening: The validator iterates over the input string character by character:
When you pass area: SE4, Python iterates the characters "S", "E", "4" none of which match any key in _REGIONS, so validation fails even though SE4 is a valid area. Before this PR, the bug was invisible because vol.Invalid() was called without raise, meaning the error was created but never actually raised validation silently passed regardless of input. Our fix correctly added raise, which exposed this underlying issue. I'll push a fix for the validator logic shortly. |
The check_setting validator had two pre-existing bugs exposed by the raise fix in the previous commit: 1. cv.ensure_list was passed as an argument to check_setting but never called, so the area value remained a string instead of becoming a list 2. The validator iterated characters of the string (e.g. "S","E","4") instead of checking the whole area code, causing all areas to fail validation Replace check_setting with _validate_areas which properly converts the input to a list via cv.ensure_list and validates each area code as a whole string against _REGIONS.
|
@dana-se Yeah my tests goes trough now. I don't know why those errors were not on my instanse before but i tested everything again and it should work as intented now |
|
@Metroseksuaali , Yes, this new code worked. |
|
@dana-se I save the history for past weeks and calculate it. I tried to do it using the entity but did not manage to fix it completely. I think it has something to do with _parse_json. But that again is not anything that I have changed and I'm not sure yet how to fix it. I thought the weekly and daily bugs were caused because of a copy-paste error. Need to try to understand this code more to get it working. |
DAILY, WEEKLY, and MONTHLY all share the same data_type value "AggregatePrices", so _parse_json could not distinguish between them. Added aggregation parameter throughout the fetch chain to select the correct JSON data source for each aggregation level. Also fixed fetch() to parse non-hourly responses instead of returning raw JSON.
|
Lol i think it did not need much. So
These all now return correct data as far as i know. |
|
@Metroseksuaali For me your code returned correct data except `Logger: homeassistant.helpers.script.websocket_api_script websocket_api script: Error executing script. Unexpected error for call_service at pos 1: 'int' object has no attribute 'replace' |
Aggregate price API sometimes returns prices as int instead of float, causing AttributeError on .replace() call. Handle both int and float numeric types.
|
@dana-se I did not know api will return int values. Now it works |
|
@Metroseksuaali Yes, now even service: nordpool.daily data: currency: EUR area: FI works. But now the Example for an automation that get the last months average price, don't work. alias: Example automation action call with storing with parsing and storing result
triggers: null
actions:
- action: nordpool.yearly
data:
currency: NOK
area: NO2
year: "2024"
response_variable: np_result
- action: input_text.set_value
target:
entity_id: input_text.test
data:
value: "{{np_result.prices[0].averagePerArea.NO2 | float}}"
mode: single |
|
@dana-se The response structure has changed because non-hourly services now go through start: "2025-12-31T23:00:00+00:00"
end: "2021-12-30T23:00:00+00:00"
updated: "2024-03-26T13:32:39.733019+00:00"
currency: NOK
areas:
NO2:
values:
- start: "2025-12-31T23:00:00+00:00"
end: "2026-01-30T23:00:00+00:00"
value: 1230.05
- start: "2024-12-31T23:00:00+00:00"
end: "2025-12-30T23:00:00+00:00"
value: 767.23So the automation template needs to be updated from: value: "{{np_result.prices[0].averagePerArea.NO2 | float}}"To: value: "{{np_result.areas.NO2.values[0].value | float}}"I also noticed the README.md (line 221-241) contains the old example with the raw API path. This should be updated to match the new response structure. I can update the README as part of this PR. |
Actions no longer return raw API JSON. Updated documentation with service table, response format, template path examples, and automation examples for yearly and monthly average prices.
|
@Metroseksuaali Thanks again for your god job. 2026-02-01 09:07:49.279 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: builtin_function_or_method object has no element 0 when rendering '{{np_result.areas.FI.values[0].value | float}}'
2026-02-01 09:07:49.280 ERROR (MainThread) [homeassistant.components.automation.get_monthly_average_price_from_nordpool] Get monthly average price from Nordpool: Error executing script. Error for call_service at pos 2: Error rendering data template: UndefinedError: builtin_function_or_method object has no element 0
2026-02-01 09:07:49.285 ERROR (MainThread) [homeassistant.components.automation.get_monthly_average_price_from_nordpool] Error while executing automation automation.get_monthly_average_price_from_nordpool: Error rendering data template: UndefinedError: builtin_function_or_method object has no element 0 |
Use bracket notation ["values"] instead of .values to avoid conflict with Python's built-in dict.values() method in Jinja2 templates.
|
@dana-se The error you got with the monthly automation is caused by Jinja2 interpreting The fix is to use bracket notation instead of dot notation: # This does NOT work:
value: "{{ np_result.areas.FI.values[0].value | float }}"
# This works:
value: "{{ np_result.areas.FI["values"][0].value | float }}"I've updated the README examples with the correct notation. Sorry for the confusion! |
|
@Metroseksuaali Now the automations works. So thanks again for the quick response and solutions |
Summary
Fix multiple bugs in the services module and const.py.
Bug fixes:
services.py:88-weekly()service was callingyearly()method instead ofweekly()services.py:23- Missingraisekeyword in area validator - invalid areas were silently acceptedservices.py:43- Wrong strftime format"Y"instead of"%Y"causing regex validation errorconst.py:93- Poland area code had trailing space"PL "instead of"PL"All bugs were verified by testing in Home Assistant before fixing.
Related to #467