samuelspagl / ha_samsung_soundbar

YASSI (Yet another Samsung Soundbar integration) is a HomeAssistant integration for Samsung Soundbars.
https://ha-samsung-soundbar.vercel.app
MIT License
44 stars 2 forks source link

division by zero #19

Closed tjorim closed 7 months ago

tjorim commented 9 months ago

I tried to set up the integration and filled out the required fields. However, soon I got an error because max_volume was set to 0 (by default). I would recommend setting the default to 100 or leave out the option entirely if possible.

Logger: homeassistant.components.websocket_api.http.connection
Source: components/websocket_api/commands.py:240
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 09:58:34 (2 occurrences)
Last logged: 09:58:35

[546150885312] division by zero
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 240, in handle_call_service
    response = await hass.services.async_call(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2279, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2316, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 899, in entity_service_call
    await entity.async_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 960, in async_update_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1110, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1049, in __async_calculate_state
    attr.update(self.state_attributes or {})
                ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 1118, in state_attributes
    if (value := getattr(self, attr)) is not None:
                 ^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/samsung_soundbar/media_player.py", line 99, in volume_level
    return self.device.volume_level
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/samsung_soundbar/api_extension/SoundbarDevice.py", line 197, in volume_level
    return self.device.status.volume / self.__max_volume
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
This error originated from a custom integration.

Logger: homeassistant
Source: custom_components/samsung_soundbar/api_extension/SoundbarDevice.py:197
Integration: Samsung Soundbar (documentation, issues)
First occurred: 09:58:40 (2 occurrences)
Last logged: 09:58:50

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 911, in _update_entity_states
    await entity.async_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 960, in async_update_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1110, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1049, in __async_calculate_state
    attr.update(self.state_attributes or {})
                ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 1118, in state_attributes
    if (value := getattr(self, attr)) is not None:
                 ^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/samsung_soundbar/media_player.py", line 99, in volume_level
    return self.device.volume_level
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/samsung_soundbar/api_extension/SoundbarDevice.py", line 197, in volume_level
    return self.device.status.volume / self.__max_volume
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
samuelspagl commented 9 months ago

I agree to change the default value for this. Leaving it out is a bit of another discussion, which I am willing to do, but not part of this bugfix.

samuelspagl commented 8 months ago

I added an additional requirement to have the provided number be in the range of 1 to 100 and set the default value to 100.

This should therefore fix the issue.