Skip to content
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

Fix NoneType comparison #303

Merged
merged 2 commits into from
Aug 28, 2024

Conversation

haugene
Copy link
Contributor

@haugene haugene commented Aug 28, 2024

Ref conversation in home-assistant/core#124595

Running Home Assistant with pyTibber==0.30.1 gives me an error:

2024-08-27 12:53:36.477 ERROR (MainThread) [tibber.home] Error in rt_subscribe
Traceback (most recent call last):
  File "/home/vscode/.local/ha-venv/lib/python3.12/site-packages/tibber/home.py", line 460, in _start
    data = _add_extra_data(data)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/ha-venv/lib/python3.12/site-packages/tibber/home.py", line 421, in _add_extra_data
    if live_data.get("powerProduction", 0) > 0 and live_data.get("power") is None:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '>' not supported between instances of 'NoneType' and 'int'

This happens because the powerProduction key in the extra data dict is None. Then the .get(..., 0) won't default to 0 because it's explicitly set to None and not missing.

This is the input I'm getting from the subscription.

{
  "data": {
    "liveMeasurement": {
      "accumulatedConsumption": 12.458026,
      "accumulatedConsumptionLastHour": 0.018409,
      "accumulatedCost": 7.797286,
      "accumulatedProduction": 0.110517,
      "accumulatedProductionLastHour": 0.010517,
      "accumulatedReward": 0.050691,
      "averagePower": 1363.1,
      "currency": "NOK",
      "currentL1": null,
      "currentL2": null,
      "currentL3": null,
      "lastMeterConsumption": 29925.24,
      "lastMeterProduction": 3942.95,
      "maxPower": 4462,
      "minPower": 0,
      "power": 0,
      "powerFactor": null,
      "powerProduction": null,
      "powerReactive": null,
      "signalStrength": null,
      "timestamp": "2024-08-28T09:08:20.000+02:00",
      "voltagePhase1": null,
      "voltagePhase2": null,
      "voltagePhase3": null
    }
  }
}

We just need to explicitly check for None before comparing with 0.

Copy link
Owner

@Danielhiversen Danielhiversen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@Danielhiversen Danielhiversen merged commit 78d1995 into Danielhiversen:master Aug 28, 2024
4 checks passed
@haugene haugene deleted the haugene/fix-type-error branch August 28, 2024 15:42
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.

2 participants