Weissnix4711 / hass-listenbrainz

Integration for ListenBrainz
MIT License
2 stars 0 forks source link

TypeError: unsupported operand type(s) for /: 'NoneType' and 'int' #41

Closed owenvoke closed 13 hours ago

owenvoke commented 3 days ago

System Health details

System Information

version core-2024.7.0
installation_type Home Assistant Container
dev false
hassio false
docker true
user root
virtualenv false
python_version 3.12.4
os_name Linux
os_version 4.4.302+
arch x86_64
timezone Europe/London
config_dir /config
Home Assistant Community Store GitHub API | ok -- | -- GitHub Content | ok GitHub Web | ok GitHub API Calls Remaining | 4946 Installed Version | 1.34.0 Stage | running Available Repositories | 1398 Downloaded Repositories | 28 HACS Data | ok
Home Assistant Cloud logged_in | false -- | -- can_reach_cert_server | ok can_reach_cloud_auth | ok can_reach_cloud | ok
Dashboards dashboards | 3 -- | -- resources | 13 views | 19 mode | storage
Recorder oldest_recorder_run | 26 June 2024 at 09:12 -- | -- current_recorder_run | 4 July 2024 at 09:30 estimated_db_size | 1271.26 MiB database_engine | postgresql database_version | 16.3

Checklist

Describe the issue

When using a service such as Plex to add data to ListenBrainz, it doesn't include a duration_ms in the payload. This results in the following error as it tries to divide None by 1000.

Example response from `get_now_playing()` ```json { "payload": { "count": 1, "listens": [ { "playing_now": true, "track_metadata": { "additional_info": { "listening_from": "Plex", "track_mbid": "a58aa1ab-fff2-42e5-97ae-111347123871" }, "artist_name": "Jeff Lynne\\u2019s ELO", "release_name": "Wembley or Bust", "track_name": "Evil Woman" } } ], "playing_now": true, "user_id": "owenvoke" } } ```

Probably the best solution is to change this line to add support for services that don't submit a duration_ms: https://github.com/Weissnix4711/hass-listenbrainz/blob/e28c1e55f038c4b6beb17247767d5dc352a111cf/custom_components/listenbrainz/api.py#L137

It also might be nice to support duration as well (which is the duration in seconds) that some services provide. 🤔 I'm happy to open a PR if you think this makes sense.


Error

This error originated from a custom integration.

Logger: custom_components.listenbrainz
Source: helpers/update_coordinator.py:312
integration: ListenBrainz (documentation, issues)
First occurred: 09:33:07 (4 occurrences)
Last logged: 09:33:44

Unexpected error fetching ListenBrainz owenvoke data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 312, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/coordinator.py", line 46, in _async_update_data
    return await self.client.async_update_data()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/api.py", line 98, in async_update_data
    playing_now = await self._hass.async_add_executor_job(self._get_playing_now)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/api.py", line 137, in _get_playing_now
    c.duration = round(c.additional_info.get("duration_ms", None) / 1000)
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

Reproduction steps

  1. Submit a ListenBrainz submission without the duration_ms data
  2. Refresh the entity in Home Assistant

Debug logs

2024-07-04 09:30:08.066 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration listenbrainz which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-04 09:30:50.574 WARNING (MainThread) [homeassistant.helpers.frame] Detected that custom integration 'hacs' calls hass.http.register_static_path which is deprecated because it does blocking I/O in the event loop, instead call `await hass.http.async_register_static_paths([StaticPathConfig("/hacsfiles/frontend", "/config/custom_components/hacs/hacs_frontend_experimental", False)])`; This function will be removed in 2025.7 at custom_components/hacs/frontend.py, line 49: hass.http.register_static_path(, please create a bug report at https://github.com/hacs/integration/issues
2024-07-04 09:30:50.583 WARNING (MainThread) [homeassistant.helpers.frame] Detected that custom integration 'hacs' calls hass.http.register_static_path which is deprecated because it does blocking I/O in the event loop, instead call `await hass.http.async_register_static_paths([StaticPathConfig("/hacsfiles/iconset.js", "/config/custom_components/hacs/iconset.js", True)])`; This function will be removed in 2025.7 at custom_components/hacs/frontend.py, line 57: hass.http.register_static_path(, please create a bug report at https://github.com/hacs/integration/issues
2024-07-04 09:30:50.591 WARNING (MainThread) [homeassistant.helpers.frame] Detected that custom integration 'hacs' accesses hass.components.frontend. This is deprecated and will stop working in Home Assistant 2024.9, it should be updated to import functions used from frontend directly at custom_components/hacs/frontend.py, line 68: hass.components.frontend.async_register_built_in_panel(, please create a bug report at https://github.com/hacs/integration/issues
2024-07-04 09:30:50.659 WARNING (MainThread) [homeassistant.helpers.frame] Detected that custom integration 'hacs' calls hass.http.register_static_path which is deprecated because it does blocking I/O in the event loop, instead call `await hass.http.async_register_static_paths([StaticPathConfig("/hacsfiles", "/config/www/community", True)])`; This function will be removed in 2025.7 at custom_components/hacs/base.py, line 1162: self.hass.http.register_static_path(, please create a bug report at https://github.com/hacs/integration/issues
2024-07-04 09:30:51.764 WARNING (MainThread) [homeassistant.helpers.frame] Detected that custom integration 'thermal_comfort' accesses hass.helpers.service. This is deprecated and will stop working in Home Assistant 2024.11, it should be updated to import functions used from service directly at custom_components/thermal_comfort/__init__.py, line 162: hass.helpers.service.async_register_admin_service(, please create a bug report at https://github.com/dolezsa/thermal_comfort/issues
2024-07-04 09:31:07.479 ERROR (SyncWorker_4) [glocaltokens.client] [!] Could not get access token.
2024-07-04 09:31:09.022 WARNING (MainThread) [homeassistant.bootstrap] Waiting on integrations to complete setup: {('co2signal', '20aea1a82a851e68254be50c63f2faae'): 6197298.305559947, ('hildebrandglow_dcc', 'f169ac4632ee34a22785447058c21a47'): 6197298.472947931, ('tomorrowio', 'bf2f2a4a394ec51f93dbf1b0ff8af5eb'): 6197298.679890791, ('apple_tv', 'e8fad0f78033d3cab29763b40a295a4a'): 6197299.517179315, ('mqtt', '9cad4d752c6d5f3ee34d335aae222ebd'): 6197299.73939352, ('zha', '850a7c3cef3b7bfcc3fb9143bcc2248f'): 6197302.469803977, ('ecologi', '26a8cefbf58d31dab5ed04e0cd200486'): 6197302.572036934, ('waqi', '26e517ceb38408d00ad9921b8af4604c'): 6197302.625580291, ('tier', '1847abf8584856e5b2c2d9b4e8fe9a38'): 6197303.016081439, ('hacs', 'a69a238ce0c28e7c97af4526fbee11dc'): 6197304.955477824, ('openweathermap', '2fae9c961a5965f6af9b7a32b74d3965'): 6197305.751800134, ('adguard', 'c2f2fb2a889eab1d4264281037c0a92b'): 6197305.953130603, ('broadlink', 'a4be2f7e27cf50eb573cabeefee6d81d'): 6197306.285644133, ('battery_notes', 'a8cfeb03ac2347bd26e3fdbe2e6abd85'): 6197306.901572871, ('battery_notes', '2fac28234cc297c6cf9aaef36ea0684d'): 6197306.902377417, ('battery_notes', '9d637b2be227ceeec9260758a44365f3'): 6197306.902931657, ('battery_notes', 'd20d4a8ceac29a9a1588a6b091b6a765'): 6197306.903426385, ('metoffice', 'a828079cb80028fd67b5bdf794512cb9'): 6197306.923633963, ('plex', '3b137583a487cd538cedaba1b738a461'): 6197311.881137268, ('openuv', 'fdb139b7d0a8bad93c4749370b1b56b5'): 6197312.021598762, ('androidtv_remote', '119f544b6900694005221ae36f5a1549'): 6197312.225899143, ('lastfm', '4737c6e399919a3cd483174dcfa057f7'): 6197312.820790477, ('synology_dsm', 'cf239136d49a84d8bfee7a8dd617a1ef'): 6197312.984224672, ('ohdear', '69b88fc0ee7b27e0493309d00073c811'): 6197317.997547035, ('ohdear', 'cee3e55931dff51454b20760057b33fe'): 6197317.997977615, ('ohdear', 'a2389abe729abc0fbc27a8b2339a7e74'): 6197317.998227238, ('qbittorrent', 'ce77d0c4ab1864604a65b08e639c2900'): 6197318.63576899, ('octopus_energy', 'ff62ff3c283ab37c76abc0553ffcfbed'): 6197319.485414032, ('local_calendar', '41b1fce369896acd3166c8bf1591de26'): 6197319.645474449, ('local_calendar', '4de9043dc7c5171410582b0bb111e5cd'): 6197319.645720617, ('local_calendar', '7979661f0238dae7f0ff61242ba6a22d'): 6197319.645880847, ('local_calendar', '9de3216bd3436372a5b7b439cae9876a'): 6197319.646039511, ('local_calendar', '7d9321f10f23703409d075c7b772555c'): 6197319.646176263, ('local_calendar', '5a0c71165be7941e06ad37c5090f1081'): 6197319.646307937, ('local_calendar', 'f8ddcacaad222f49208b00bdf3a90a38'): 6197319.646441242, ('local_calendar', 'ae38c263127618e698f477f3ac96c0d4'): 6197319.646566373, ('unifi', '1b2153e38d12365e2f06511d304ca38f'): 6197320.397649138, ('speedtestdotnet', '97024fa4764f7dcbd7220f5e79eb3684'): 6197321.056525532, ('google_home', '6d8ba36cc0cea3ea5b17da5569e38a9b'): 6197322.254129745, ('gdacs', 'b1931a59e8696a8aac4820497be1b166'): 6197323.054926867, ('wyoming', 'c587210f5e49c356d08f7d4de9245b5c'): 6197323.299041944, ('wyoming', '72c40b04440c12e678654b22ddb6a8e5'): 6197323.300177178, ('wyoming', 'b7f8442229f6b5c9537eb16836280664'): 6197323.3007374, ('geocaching', '5ef2396eea2fe6b5073ce311165a7af0'): 6197323.415498209, ('monzo', 'ae0be4add88240e57bc149a727a48544'): 6197323.582243248}
AttributeError: 'int' object has no attribute 'total_seconds'
2024-07-04 09:31:15.914 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Oh Dear (voke.dev) for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 586, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 130, in async_setup_entry
    return await component.async_setup_entry(entry)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 189, in async_setup_entry
    self._platforms[key] = self._async_init_entity_platform(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 390, in _async_init_entity_platform
    entity_platform = EntityPlatform(
                      ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 137, in __init__
    self.scan_interval_seconds = scan_interval.total_seconds()
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'total_seconds'
2024-07-04 09:31:24.313 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to import_module with args ('dateparser.data.date_translation_data.en',) inside the event loop by integration 'gdacs' at homeassistant/components/gdacs/__init__.py, line 115: await self._feed_manager.update() (offender: /usr/local/lib/python3.12/site-packages/dateparser/languages/loader.py, line 215: import_module("dateparser.data.date_translation_data." + lang),), please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+gdacs%22
For developers, please see https://developers.home-assistant.io/docs/asyncio_blocking_operations/#import_module
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/usr/src/homeassistant/homeassistant/__main__.py", line 223, in <module>
    sys.exit(main())
  File "/usr/src/homeassistant/homeassistant/__main__.py", line 209, in main
    exit_code = runner.run(runtime_conf)
  File "/usr/src/homeassistant/homeassistant/runner.py", line 190, in run
    return loop.run_until_complete(setup_and_run_hass(runtime_config))
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 641, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1990, in _run_once
    handle._run()
  File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/components/gdacs/__init__.py", line 115, in async_update
    await self._feed_manager.update()

2024-07-04 09:31:26.906 WARNING (MainThread) [homeassistant.helpers.frame] Detected that custom integration 'hacs' accesses hass.helpers.event. This is deprecated and will stop working in Home Assistant 2024.11, it should be updated to import functions used from event directly at custom_components/hacs/base.py, line 642: self.hass.helpers.event.async_track_time_interval(, please create a bug report at https://github.com/hacs/integration/issues
2024-07-04 09:33:07.832 ERROR (MainThread) [custom_components.listenbrainz] Unexpected error fetching ListenBrainz owenvoke data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 312, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/coordinator.py", line 46, in _async_update_data
    return await self.client.async_update_data()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/api.py", line 98, in async_update_data
    playing_now = await self._hass.async_add_executor_job(self._get_playing_now)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/api.py", line 137, in _get_playing_now
    c.duration = round(c.additional_info.get("duration_ms", None) / 1000)
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
2024-07-04 09:33:13.499 ERROR (MainThread) [custom_components.listenbrainz] Unexpected error fetching ListenBrainz owenvoke data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 312, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/coordinator.py", line 46, in _async_update_data
    return await self.client.async_update_data()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/api.py", line 98, in async_update_data
    playing_now = await self._hass.async_add_executor_job(self._get_playing_now)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/api.py", line 137, in _get_playing_now
    c.duration = round(c.additional_info.get("duration_ms", None) / 1000)
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
2024-07-04 09:33:23.879 ERROR (MainThread) [custom_components.listenbrainz] Unexpected error fetching ListenBrainz owenvoke data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 312, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/coordinator.py", line 46, in _async_update_data
    return await self.client.async_update_data()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/api.py", line 98, in async_update_data
    playing_now = await self._hass.async_add_executor_job(self._get_playing_now)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/api.py", line 137, in _get_playing_now
    c.duration = round(c.additional_info.get("duration_ms", None) / 1000)
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
2024-07-04 09:33:44.266 ERROR (MainThread) [custom_components.listenbrainz] Unexpected error fetching ListenBrainz owenvoke data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 312, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/coordinator.py", line 46, in _async_update_data
    return await self.client.async_update_data()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/api.py", line 98, in async_update_data
    playing_now = await self._hass.async_add_executor_job(self._get_playing_now)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/listenbrainz/api.py", line 137, in _get_playing_now
    c.duration = round(c.additional_info.get("duration_ms", None) / 1000)
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

Diagnostics dump

No response

Weissnix4711 commented 2 days ago

Hiya, cheers for the bug report.

It also might be nice to support duration as well (which is the duration in seconds) that some services provide.

Sounds good. Was this recently added to the listenbrainz API or did I just manage to completely miss it? lol

I'm happy to open a PR if you think this makes sense.

I'm away at the moment, so if you want to open a PR, go for it. Otherwise you'll have to bear with me a couple days.

owenvoke commented 2 days ago

Hi @Weissnix4711, thanks for the quick reply! I'm happy to wait as it'd be nice to have your input (no rush).

Regarding duration, I'm not entirely sure. It's just mentioned alongside duration_ms in the Additional Info Fields section.