G-Two / subarulink

A python package for interacting with Subaru STARLINK remote vehicle services.
Apache License 2.0
82 stars 14 forks source link

Convert string values to int or float #56

Closed G-Two closed 1 year ago

G-Two commented 1 year ago

Additional processing is required by users of the module to use numerical values presented as strings. This module should convert the text strings to numerical types.

Example from HomeAssistant:

    def get_current_value(self):
        """Get raw value from the coordinator."""
        if isinstance(data := self.coordinator.data, dict):
            value = data.get(self.vin)[VEHICLE_STATUS].get(self.entity_description.key)
            if value in sc.BAD_SENSOR_VALUES:
                value = None
            if isinstance(value, str):
                if "." in value:
                    value = float(value)
                elif value.isdigit():
                    value = int(value)
            _LOGGER.debug("Raw value for %s: %s", self._attr_name, value)
            return value

Ideally, this code should be simplified to without any adverse behavior:

    def get_current_value(self):
        """Get raw value from the coordinator."""
        if isinstance(data := self.coordinator.data, dict):
            value = data.get(self.vin)[VEHICLE_STATUS].get(self.entity_description.key)
            _LOGGER.debug("Raw value for %s: %s", self._attr_name, value)
            return value
G-Two commented 1 year ago

Fixed in v0.6.0