Skip to content

Handle missing or incomplete API responses in get_device() - #14

Open
aviadra wants to merge 1 commit into
Guy293:masterfrom
aviadra:fix/defensive-api-parsing
Open

aviadra wants to merge 1 commit into
Guy293:masterfrom
aviadra:fix/defensive-api-parsing

Conversation

@aviadra

@aviadra aviadra commented Apr 8, 2026

Copy link
Copy Markdown

Summary

When the Tami4 device is temporarily disconnected from the Strauss cloud (e.g. due to WiFi issues), the /api/v3/customer/mainPage/{psn} endpoint returns an error response (HTTP 422 DeviceNotConnectedException) that lacks the expected keys. This causes cascading crashes:

  1. KeyError: 'drinks' — response has no drinks key
  2. KeyError: 'dynamicData' — response has no dynamicData key
  3. TypeError: unsupported operand type(s) for /: 'NoneType' and 'int' — upcoming_replacement is None, and QualityInfo.__post_init__ tries to divide it by 1000

Changes

Tami4EdgeAPI.py:

  • Use .get() with sensible defaults for drinks, dynamicData, filterInfo, uvInfo, and their sub-keys
  • Default milli_litters_passed to 0 when missing or null

water_quality.py:

  • Guard the upcoming_replacement timestamp conversion against None
  • Update type hint to Optional[date] since the field can legitimately be absent

Impact

These changes are backwards-compatible — when the API returns complete data, behavior is identical. When data is missing (device offline, API errors), the library now returns safe defaults instead of crashing.

This directly fixes the Home Assistant tami4 integration failing with Failed setup, will retry when the device is temporarily unreachable.

Made with Cursor

The Strauss cloud API returns error responses (e.g. 422
DeviceNotConnectedException) that lack the expected keys like "drinks",
"dynamicData", "filterInfo", and "uvInfo". This causes KeyError crashes
in `get_device()` and TypeError crashes in `QualityInfo.__post_init__`
when `upcoming_replacement` is None.

- Use `.get()` with sensible defaults for all response fields
- Guard `upcoming_replacement` timestamp conversion against None
- Default `milli_litters_passed` to 0 when missing or null
- Update type hint to reflect that `upcoming_replacement` can be None

Made-with: Cursor
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.

1 participant