home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.78k stars 30.88k forks source link

ViCare: KeyError: 'vicare_programs' #127068

Closed barche closed 1 month ago

barche commented 1 month ago

The problem

Changing the program on my V200 heatpump from "standby" to "comfort" throws KeyError: 'vicare_programs' and does not change the program. Stack trace and workaround below.

What version of Home Assistant Core has the issue?

core-2024.9.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Viessmann ViCare

Link to integration documentation on our website

https://www.home-assistant.io/integrations/vicare/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Stack trace of the problem. This trace was taken on a copy of vicare from the dev branch of HA, but it behaves the same on the latest release.


2024-09-29 14:21:20.908 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [3427149760] Unexpected exception
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 245, in handle_call_service
    response = await hass.services.async_call(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2761, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2804, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 996, in entity_service_call
    single_response = await _handle_entity_call(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1068, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/climate/__init__.py", line 778, in async_handle_set_preset_mode_service
    await self.async_set_preset_mode(preset_mode)
  File "/usr/src/homeassistant/homeassistant/components/climate/__init__.py", line 786, in async_set_preset_mode
    await self.hass.async_add_executor_job(self.set_preset_mode, preset_mode)
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/vicare/climate.py", line 300, in set_preset_mode
    preset_mode, self._attributes["vicare_programs"]
                 ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: 'vicare_programs'

Additional information

When repopulating self._attributes["vicare_programs"] just before line 300 in vicare/climate.py like this, changing the program works again:

if "vicare_programs" not in self._attributes:
            self._attributes["vicare_programs"] = self._api.getPrograms()

I haven't been able to figure out why vicare_programs didn't exist, log output verifies that it does get populated correctly in init, but it somehow disappears again before changing the program.

home-assistant[bot] commented 1 month ago

Hey there @cfenner, mind taking a look at this issue as it has been labeled with an integration (vicare) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `vicare` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign vicare` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


vicare documentation vicare source (message by IssueLinks)

CFenner commented 1 month ago

Unfortunately it seems the vicare_programs are reset by the update function (each minute or so).

https://github.com/home-assistant/core/blob/0f4c50e83cc11d92994c57ef12893ec42c758d24/homeassistant/components/vicare/climate.py#L192-L198

CFenner commented 1 month ago

@barche feel free to verify if https://github.com/home-assistant/core/pull/127128 solves the issue for you as well.

barche commented 1 month ago

Thanks for the quick fix, I confirm this solves the issue. I still have trouble changing program sometimes, but that's something I have to debug separately, I noticed that even when sending commands directly using PyViCare the program doesn't always change.