Trenitalia Viaggiatreno integration - possible issue

Hi there,

apparently I am the sole user of the Viaggiatreno integration, an integration meant to access train status in Italy.

I tried to use that and I am consistently receiving an error:

Logger: homeassistant
Source: components/sensor/__init__.py:664
First occurred: 15:56:54 (56 occurrences)
Last logged: 16:24:25

Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 662, in state
    numerical_value = float(value)  # type:ignore[arg-type]
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: 'Arrived'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 1047, in _async_update_entity_states
    await asyncio.gather(*tasks)
  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 1130, in _async_write_ha_state
    self.__async_calculate_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 664, in state
    raise ValueError(
ValueError: Sensor sensor.train_26397 has device class 'None', state class 'None' unit '' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'Arrived' (<class 'str'>)

It seems a pretty easy fix in the code, since the components successfully reads the train status, “arrived” in this case, but fails since it expects a numeric value.

Should I open a bug and try to fix it? It seems an easy issue in core/homeassistant/components/viaggiatreno/sensor.py around lines 100-120

If you want to give a quick try at the bug just dump

sensor:
  - platform: viaggiatreno
    train_id: 26397
    station_id: S00208

in your config.yaml and reboot.

Yes, you should file a bug and also try to fix it and create a pull request. The integration does not seem to have a maintainer, so it’s up to you I’m afraid

Added an issue here and proposed a solution.
I’ll wait for a feedback before committing