Skip to content

Fixing TypeError on missing battery data in battery.py#52

Open
loopy321 wants to merge 2 commits intovanstinator:mainfrom
loopy321:main
Open

Fixing TypeError on missing battery data in battery.py#52
loopy321 wants to merge 2 commits intovanstinator:mainfrom
loopy321:main

Conversation

@loopy321
Copy link

Tested and works on my Home Assistant system.

Attempt to address missing data errors like the following:

Unexpected error fetching Melnor Bluetooth data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 392, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/melnor/coordinator.py", line 39, in _async_update_data
    await self._device.fetch_state()
  File "/usr/local/lib/python3.13/site-packages/melnor_bluetooth/device.py", line 349, in fetch_state
    self._battery = parse_battery_value(some_bytes)
                    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/melnor_bluetooth/utils/battery.py", line 4, in parse_battery_value
    if (bytes[0] & 255 == 238) and (bytes[1] & 255 == 238):
        ~~~~~^^^
TypeError: 'NoneType' object is not subscriptable
Unexpected error fetching Melnor Bluetooth data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 392, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/melnor/coordinator.py", line 39, in _async_update_data
    await self._device.fetch_state()
  File "/usr/local/lib/python3.13/site-packages/melnor_bluetooth/device.py", line 353, in fetch_state
    valve.update_state(bytes_array[some_bytes], uuids[i])
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/melnor_bluetooth/device.py", line 69, in update_state
    self._is_watering = struct.unpack_from(">?", raw_bytes, offset)[0]
                        ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'NoneType'

Getting errors when data missing like the following:

ERROR (MainThread) [homeassistant.components.melnor.coordinator] Unexpected error fetching Melnor Bluetooth data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 392, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/melnor/coordinator.py", line 39, in _async_update_data
    await self._device.fetch_state()
  File "/usr/local/lib/python3.13/site-packages/melnor_bluetooth/device.py", line 349, in fetch_state
    self._battery = parse_battery_value(some_bytes)
                    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/melnor_bluetooth/utils/battery.py", line 4, in parse_battery_value
    if (bytes[0] & 255 == 238) and (bytes[1] & 255 == 238):
        ~~~~~^^^
TypeError: 'NoneType' object is not subscriptable
Fix to step error messages like the following:

Unexpected error fetching Melnor Bluetooth data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 392, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/melnor/coordinator.py", line 39, in _async_update_data
    await self._device.fetch_state()
  File "/usr/local/lib/python3.13/site-packages/melnor_bluetooth/device.py", line 353, in fetch_state
    valve.update_state(bytes_array[some_bytes], uuids[i])
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/melnor_bluetooth/device.py", line 69, in update_state
    self._is_watering = struct.unpack_from(">?", raw_bytes, offset)[0]
                        ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'NoneType'
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