From 1ef09048e6ab2849fdadeb3cbc81f4c3191be2ca Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Mon, 6 May 2024 20:06:26 +0200 Subject: [PATCH] Allow the rounding to be optional in integral (#116884) --- .../components/integration/config_flow.py | 4 ++-- homeassistant/components/integration/sensor.py | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/integration/config_flow.py b/homeassistant/components/integration/config_flow.py index 318f1355aae..20c1b920ec7 100644 --- a/homeassistant/components/integration/config_flow.py +++ b/homeassistant/components/integration/config_flow.py @@ -48,7 +48,7 @@ INTEGRATION_METHODS = [ OPTIONS_SCHEMA = vol.Schema( { - vol.Required(CONF_ROUND_DIGITS, default=2): selector.NumberSelector( + vol.Optional(CONF_ROUND_DIGITS): selector.NumberSelector( selector.NumberSelectorConfig( min=0, max=6, mode=selector.NumberSelectorMode.BOX ), @@ -69,7 +69,7 @@ CONFIG_SCHEMA = vol.Schema( options=INTEGRATION_METHODS, translation_key=CONF_METHOD ), ), - vol.Required(CONF_ROUND_DIGITS, default=2): selector.NumberSelector( + vol.Optional(CONF_ROUND_DIGITS): selector.NumberSelector( selector.NumberSelectorConfig( min=0, max=6, diff --git a/homeassistant/components/integration/sensor.py b/homeassistant/components/integration/sensor.py index 65e967d2af7..9c2e09559af 100644 --- a/homeassistant/components/integration/sensor.py +++ b/homeassistant/components/integration/sensor.py @@ -81,7 +81,9 @@ PLATFORM_SCHEMA = vol.All( vol.Optional(CONF_NAME): cv.string, vol.Optional(CONF_UNIQUE_ID): cv.string, vol.Required(CONF_SOURCE_SENSOR): cv.entity_id, - vol.Optional(CONF_ROUND_DIGITS, default=DEFAULT_ROUND): vol.Coerce(int), + vol.Optional(CONF_ROUND_DIGITS, default=DEFAULT_ROUND): vol.Any( + None, vol.Coerce(int) + ), vol.Optional(CONF_UNIT_PREFIX): vol.In(UNIT_PREFIXES), vol.Optional(CONF_UNIT_TIME, default=UnitOfTime.HOURS): vol.In(UNIT_TIME), vol.Remove(CONF_UNIT_OF_MEASUREMENT): cv.string, @@ -259,10 +261,14 @@ async def async_setup_entry( # Before we had support for optional selectors, "none" was used for selecting nothing unit_prefix = None + round_digits = config_entry.options.get(CONF_ROUND_DIGITS) + if round_digits: + round_digits = int(round_digits) + integral = IntegrationSensor( integration_method=config_entry.options[CONF_METHOD], name=config_entry.title, - round_digits=int(config_entry.options[CONF_ROUND_DIGITS]), + round_digits=round_digits, source_entity=source_entity_id, unique_id=config_entry.entry_id, unit_prefix=unit_prefix, @@ -283,7 +289,7 @@ async def async_setup_platform( integral = IntegrationSensor( integration_method=config[CONF_METHOD], name=config.get(CONF_NAME), - round_digits=config[CONF_ROUND_DIGITS], + round_digits=config.get(CONF_ROUND_DIGITS), source_entity=config[CONF_SOURCE_SENSOR], unique_id=config.get(CONF_UNIQUE_ID), unit_prefix=config.get(CONF_UNIT_PREFIX), @@ -304,7 +310,7 @@ class IntegrationSensor(RestoreSensor): *, integration_method: str, name: str | None, - round_digits: int, + round_digits: int | None, source_entity: str, unique_id: str | None, unit_prefix: str | None, @@ -328,6 +334,7 @@ class IntegrationSensor(RestoreSensor): self._source_entity: str = source_entity self._last_valid_state: Decimal | None = None self._attr_device_info = device_info + self._attr_suggested_display_precision = round_digits or 2 def _calculate_unit(self, source_unit: str) -> str: """Multiply source_unit with time unit of the integral. @@ -454,7 +461,7 @@ class IntegrationSensor(RestoreSensor): @property def native_value(self) -> Decimal | None: """Return the state of the sensor.""" - if isinstance(self._state, Decimal): + if isinstance(self._state, Decimal) and self._round_digits: return round(self._state, self._round_digits) return self._state