cmroche / OctoPrint-HomeAssistant

Home Assistant plugin for OctoPrint, enabling MQTT discovery of you device and sensors
110 stars 18 forks source link

Precise Print Progress Percentage #109

Open mreditor97 opened 1 year ago

mreditor97 commented 1 year ago

In this patch, I have added in a more precise display of the print progress percentage that is displayed within Home Assistant. The suggested_display_precision is set to 2 to allow for two decimal places on the percentage which is probably the sweet spot for accuracy.

Enjoy! :)

cmroche commented 1 year ago

@mreditor97

I'm seeing this error when testing:

Traceback (most recent call last):
  File "/workspaces/core/homeassistant/components/sensor/__init__.py", line 593, in state
    numerical_value = float(value)  # type:ignore[arg-type]
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: 'None'

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

Traceback (most recent call last):
  File "/workspaces/core/homeassistant/components/mqtt/models.py", line 270, in process_write_state_requests
    entity.async_write_ha_state()
  File "/workspaces/core/homeassistant/helpers/entity.py", line 746, in async_write_ha_state
    self._async_write_ha_state()
  File "/workspaces/core/homeassistant/helpers/entity.py", line 808, in _async_write_ha_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspaces/core/homeassistant/helpers/entity.py", line 752, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/workspaces/core/homeassistant/components/sensor/__init__.py", line 595, in state
    raise ValueError(
ValueError: Sensor sensor.octoprint_approximate_total_print_time has device class 'duration', state class 'None' unit 's' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'None' (<class 'str'>)
mreditor97 commented 1 year ago

@cmroche I am thinking that is due to the broken patch from 3.6.5. I'll rebase it when I get chance then it should sort this problem...as that error was fixed in the #108 pull

mreditor97 commented 1 year ago

@cmroche I have rebased with the new master branch, so you shouldn't have any errors now. It should just display a slightly more precise print progress percentage.