Allow the rounding to be optional in integral (#116884)

This commit is contained in:
Joakim Plate 2024-05-06 20:06:26 +02:00 committed by GitHub
parent 09be56964d
commit 1ef09048e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 7 deletions

View file

@ -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,

View file

@ -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