mathoudebine/turing-smart-screen-python

Custom stats do not update visuals if string or numeric value is falsy but not None

uyrussell21 opened this issue · 1 comments

Describe the bug
Custom stats do not update visuals if string or numeric value is falsy (e.g. zero or empty string) but not None

To Reproduce

  1. Create a custom stats class that can return empty string or zero; in this case the numeric value returns 0
class ExampleProgressData(CustomDataSource):
    def as_numeric(self) -> float:
        return 0

    def as_string(self) -> str:
        return "0"
  1. Put class in theme.yaml
STATS:
  CUSTOM:
    INTERVAL: 1
    ExampleProgressData:
      RADIAL:
        SHOW: True
        X: 141
        Y: 275
        RADIUS: 28
        WIDTH: 8
        MIN_VALUE: 0
        MAX_VALUE: 100
        ANGLE_START: 110
        ANGLE_END: 70
        ANGLE_STEPS: 1
        ANGLE_SEP: 25
        CLOCKWISE: True
        BAR_COLOR: 255, 0, 0
        SHOW_TEXT: True
        SHOW_UNIT: True
        FONT: roboto/Roboto-Bold.ttf
        FONT_SIZE: 13
        FONT_COLOR: 200, 200, 200
        # BACKGROUND_COLOR: 0, 0, 0
        BACKGROUND_IMAGE: background.png
  1. Run the program or theme editor
  2. No graph is displayed.

Expected behavior
It should display the string "0" representation as defined in as_string() despite the radial graph being 0 as well

Screenshots / photos of the Turing screen
Current:
image

It should have been like this:
image

Environment:

  • Smart screen model: Turing 3.5”
  • Revision of this project: main branch
  • OS with version: Windows 10
  • Python version: Python 3.11
  • Hardware: AMD Ryzen 7 CPU, Nvidia GPU

Additional context
The problem lies in the Context class using truthy / falsy checks instead of checking if string or numeric value is None

# library/stats.py

class Custom:
    @staticmethod
    def stats():
    ...
-               if not string_value:
+               if string_value is None:
                    string_value = str(numeric_value)

                # Display text
                theme_data = config.THEME_DATA["STATS"]["CUSTOM"][custom_stat].get(
                    "TEXT", None
                )
-               if theme_data and string_value:
+               if theme_data and string_value is not None:
                    display_themed_value(theme_data=theme_data, value=string_value)

                # Display graph from numeric value
                theme_data = config.THEME_DATA["STATS"]["CUSTOM"][custom_stat].get(
                    "GRAPH", None
                )
-               if theme_data and numeric_value:
+               if theme_data and numeric_value is not None:
                    display_themed_progress_bar(
                        theme_data=theme_data, value=numeric_value
                    )

                # Display radial from numeric and text value
                theme_data = config.THEME_DATA["STATS"]["CUSTOM"][custom_stat].get(
                    "RADIAL", None
                )
-               if theme_data and numeric_value and string_value:
+               if theme_data and numeric_value is not None and string_value is not None:
                    display_themed_radial_bar(
                        theme_data=theme_data,
                        value=numeric_value,
                        custom_text=string_value,
                    )

Hi! Thanks for raising this issue, I've made a PR that should fix it in the next version!