mathoudebine / turing-smart-screen-python

Unofficial Python system monitor and library for small IPS USB-C displays like Turing Smart Screen or XuanFang
GNU General Public License v3.0
1.05k stars 174 forks source link

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

Closed uyrussell21 closed 8 months ago

uyrussell21 commented 8 months ago

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:

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,
                    )
mathoudebine commented 8 months ago

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