Skip to content

Fix NoneType crash in premium sensors and cart service calls#66

Open
TeTeHacko wants to merge 1 commit intodvejsada:masterfrom
TeTeHacko:fix/nulltype-premium-limits-and-cart-service
Open

Fix NoneType crash in premium sensors and cart service calls#66
TeTeHacko wants to merge 1 commit intodvejsada:masterfrom
TeTeHacko:fix/nulltype-premium-limits-and-cart-service

Conversation

@TeTeHacko
Copy link

Problem

Two related bugs cause crashes for users whose Rohlík API returns `"premiumLimits": null`
(users without Premium or with certain membership tiers):

Bug 1: `premiumLimits: null` crashes sensor and binary_sensor platforms

`dict.get(key, default)` only returns the default when the key is missing.
When the API explicitly returns `"premiumLimits": null`, `.get('premiumLimits', {})`
returns `None`, and the subsequent `.get('ordersWithoutPriceLimit', ...)` raises:

AttributeError: 'NoneType' object has no attribute 'get'

This prevents `binary_sensor.tomas_herceg_premium_membership` and
`sensor.tomas_herceg_no_limit_orders` / `sensor.tomas_herceg_free_express_deliveries`
from being added to HA at all.

Traceback from HA log:

ERROR homeassistant.components.binary_sensor] Error adding entity
binary_sensor.xxx_premium_membership for domain binary_sensor with platform rohlikcz
  File "binary_sensor.py", line 138, in extra_state_attributes

Bug 2: `add_to_cart` / `delete_from_cart` service calls return HTTP 500

Because these methods call `await self.async_update()` after the API call, which
triggers `publish_updates()` → sensor callbacks → the broken sensors above →
the exception propagates and the entire service call fails with:

HomeAssistantError: Failed to search for product: 'NoneType' object has no attribute 'get'

This means `search_and_add_to_cart` (and `add_to_cart`, `delete_from_cart`) always
return 500 for affected users, even though the underlying API call succeeded.

Traceback:

ERROR [custom_components.rohlikcz.services] Failed to search for product:
'NoneType' object has no attribute 'get'
  File "services.py", line 81, in async_search_and_add_product_service
  File "hub.py", line 99, in search_and_add
  File "hub.py", line 75, in add_to_cart
  File "hub.py", line 55, in async_update
  File "hub.py", line 68, in publish_updates
  File "binary_sensor.py", line 138, in extra_state_attributes

Fix

  1. `binary_sensor.py` / `sensor.py`: Use `or {}` instead of `, {}` default
    to handle explicit `null` values from the API.

  2. `hub.py`: Remove `await self.async_update()` from `add_to_cart` and
    `delete_from_cart`. The periodic update via `UpdateSensor` (every 10 min)
    is sufficient; triggering a full refresh as a side-effect of a service call
    is what causes the cascade failure.

Tested on

  • HA 2025.x, rohlikcz v0.4.0
  • User account without Rohlík Premium (API returns `"premiumLimits": null`)

- binary_sensor.py / sensor.py: use 'or {}' instead of default={}
  in .get('premiumLimits') to handle explicit null from API
- hub.py: remove await self.async_update() from add_to_cart and
  delete_from_cart to prevent cascade failure through broken sensors
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