unixorn / ha-mqtt-discoverable

Python module to create MQTT entities that are automatically discovered by Home Assistant
Apache License 2.0
82 stars 21 forks source link

Add suggested_display_precision to SensorInfo #179

Open peteh opened 5 months ago

peteh commented 5 months ago

Is your feature request related to a problem? Please describe. https://www.home-assistant.io/integrations/sensor.mqtt/ The documentation describes suggested_display_precision however it seems that this is not settable in this library.

It would configure homeassistant to set the correct decimals.

Describe the solution you'd like A clear and concise description of what you want to happen. I'd like to add suggested_display_precision to SensorInfo to give homeassistant this information for showing my numbers.

balance_sensor_info = SensorInfo(name=f"{balance.account_name}", 
                                                 device_class="monetary", 
                                                 unit_of_measurement = "EUR", 
                                                 state_class="measurement",
                                                 #suggested_display_precision=2,
                                                 unique_id=balance_unique_id, 
                                                 device=device_info)
                sensor_settings = Settings(mqtt=self._mqtt_settings, entity=balance_sensor_info)

Describe alternatives you've considered I tried rounding the numbers but for some floats it will still include numerical errors.

Additional context Add any other context or screenshots about the feature request here.

ericyeargan commented 5 months ago

FYI, as a workaround, you can use a custom SensorInfo subclass with the suggested_display_precision property:

class SensorInfoExtra(SensorInfo):
    suggested_display_precision: int

balance_sensor_info = SensorInfoExtra(name=f"{balance.account_name}", 
                                                 device_class="monetary", 
                                                 unit_of_measurement = "EUR", 
                                                 state_class="measurement",
                                                 suggested_display_precision=2,
                                                 unique_id=balance_unique_id, 
                                                 device=device_info)

sensor_settings = Settings(mqtt=self._mqtt_settings, entity=balance_sensor_info)
peteh commented 5 months ago

Thanks, I'm using this workaround for now