Skip to content

NetworkReceive/Send sensors crash with TypeError when stats_data "b" key is None #23

@Feriman22

Description

@Feriman22

Describe the bug
The BeszelNetworkReceiveSensor and BeszelNetworkSendSensor crash every
~2 minutes with a TypeError because stats_data.get("b") returns None,
and the code immediately tries to index it with [1] / [0].

Version

  • beszel-ha: 0.5.0 (manifest still says 0.4.0 - version was not bumped)
  • Home Assistant: latest (Docker)
  • Beszel server + agent: latest

Error log

ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved (task: None)
Traceback (most recent call last):
  File "homeassistant/helpers/update_coordinator.py", line 294, in _handle_refresh_interval
  File "homeassistant/helpers/update_coordinator.py", line 533, in _async_refresh
  File "homeassistant/helpers/update_coordinator.py", line 200, in async_update_listeners
  File "homeassistant/helpers/update_coordinator.py", line 613, in _handle_coordinator_update
  File "homeassistant/helpers/entity.py", line 1018, in async_write_ha_state
  File "homeassistant/helpers/entity.py", line 1163, in _async_write_ha_state
  File "homeassistant/helpers/entity.py", line 1070, in __async_calculate_state
  File "homeassistant/helpers/entity.py", line 1024, in _stringify_state
  File "homeassistant/components/sensor/__init__.py", line 569, in state
  File "custom_components/beszel_api/sensor.py", line 241, in native_value
    return self.stats_data.get("b")[1] / 1024 if self.system else None
           ~~~~~~~~~~~~~~~~~~~~~~~~^^^
TypeError: 'NoneType' object is not subscriptable

Root cause
In sensor.py, lines 241 and 270:

# line 241 - BeszelNetworkReceiveSensor
return self.stats_data.get("b")[1] / 1024 if self.system else None

# line 270 - BeszelNetworkSendSensor
return self.stats_data.get("b")[0] / 1024 if self.system else None

stats_data.get("b") can return None when the Beszel API doesn't include
bandwidth data in its response. The code checks if self.system but does not
check whether the "b" key actually exists in stats_data.

Suggested fix

# line 241
b_data = self.stats_data.get("b")
return b_data[1] / 1024 if self.system and b_data else None

# line 270
b_data = self.stats_data.get("b")
return b_data[0] / 1024 if self.system and b_data else None

Additional notes

  • The v0.5.0 release notes mention "Network Send/Receive sensor parsing was
    enhanced", but the actual code in the 0.5.0 tag is identical to 0.4.0 for
    these sensors.
  • The manifest.json version field was not bumped to 0.5.0 either.
  • I applied the fix above locally and it resolves the issue completely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions