Skip to content

Commit

Permalink
rounding numeric value #68
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuen Lee committed Nov 3, 2023
1 parent 8ecc047 commit e3eef36
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion custom_components/alfen_wallbox/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AlfenNumberDescriptionMixin:
state: float
api_param: str
custom_mode: str
round_digits: int | None


@dataclass
Expand All @@ -54,6 +55,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
api_param="2068_0",
round_digits=None,
),
AlfenNumberDescription(
key="main_normal_max_current_socket_1",
Expand All @@ -68,6 +70,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
api_param="2129_0",
round_digits=None,
),
AlfenNumberDescription(
key="max_station_current",
Expand All @@ -82,6 +85,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
api_param="2062_0",
round_digits=None,
),
AlfenNumberDescription(
key="lb_max_smart_meter_current",
Expand All @@ -96,6 +100,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
api_param="2067_0",
round_digits=None,
),
AlfenNumberDescription(
key="lb_solar_charging_green_share",
Expand All @@ -110,6 +115,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=PERCENTAGE,
api_param="3280_2",
round_digits=None,
),
AlfenNumberDescription(
key="lb_solar_charging_comfort_level",
Expand All @@ -124,6 +130,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfPower.WATT,
api_param="3280_3",
round_digits=None,
),
AlfenNumberDescription(
key="dp_light_intensity",
Expand All @@ -138,6 +145,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=PERCENTAGE,
api_param="2061_2",
round_digits=None,
),
AlfenNumberDescription(
key="ps_installation_max_imbalance_current",
Expand All @@ -152,6 +160,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
api_param="2174_0",
round_digits=None,
),
AlfenNumberDescription(
key="lb_Charging_profiles_random_delay",
Expand All @@ -166,6 +175,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
api_param="21B9_0",
round_digits=None,
),
AlfenNumberDescription(
key="auth_re_authorize_after_power_outage",
Expand All @@ -180,6 +190,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfTime.SECONDS,
api_param="2169_0",
round_digits=None,
),
AlfenNumberDescription(
key="auth_connection_timeout",
Expand All @@ -194,6 +205,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfTime.SECONDS,
api_param="2169_0",
round_digits=None,
),
AlfenNumberDescription(
key="ws_wired_socket_timeout",
Expand All @@ -208,6 +220,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfTime.SECONDS,
api_param="208B_1",
round_digits=None,
),
AlfenNumberDescription(
key="ws_mobile_socket_timeout",
Expand All @@ -222,6 +235,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfTime.SECONDS,
api_param="208B_2",
round_digits=None,
),
AlfenNumberDescription(
key="ocpp_wired_ocpp_send_timeout",
Expand All @@ -236,6 +250,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfTime.SECONDS,
api_param="208D_1",
round_digits=None,
),
AlfenNumberDescription(
key="ocpp_mobile_ocpp_send_timeout",
Expand All @@ -250,6 +265,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfTime.SECONDS,
api_param="208D_2",
round_digits=None,
),
AlfenNumberDescription(
key="ocpp_wired_ocpp_reply_timeout",
Expand All @@ -264,6 +280,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfTime.SECONDS,
api_param="208E_1",
round_digits=None,
),
AlfenNumberDescription(
key="ocpp_mobile_ocpp_reply_timeout",
Expand All @@ -278,6 +295,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfTime.SECONDS,
api_param="208E_1",
round_digits=None,
),
AlfenNumberDescription(
key="heartbeat_interval",
Expand All @@ -292,6 +310,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=NumberMode.BOX,
unit_of_measurement=UnitOfTime.SECONDS,
api_param="2086_0",
round_digits=None,
),
AlfenNumberDescription(
key="price_start_tariff",
Expand All @@ -306,6 +325,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=NumberMode.BOX,
unit_of_measurement=CURRENCY_EURO,
api_param="3262_2",
round_digits=2
),
AlfenNumberDescription(
key="price_price_per_kwh",
Expand All @@ -320,6 +340,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=NumberMode.BOX,
unit_of_measurement=CURRENCY_EURO,
api_param="3262_3",
round_digits=2
),
AlfenNumberDescription(
key="price_price_per_minute",
Expand All @@ -334,6 +355,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=NumberMode.BOX,
unit_of_measurement=CURRENCY_EURO,
api_param="3262_4",
round_digits=2
),

)
Expand All @@ -352,6 +374,7 @@ class AlfenNumberDescription(NumberEntityDescription, AlfenNumberDescriptionMixi
custom_mode=None,
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
api_param="3129_0",
round_digits=None,
),
)

Expand Down Expand Up @@ -444,7 +467,10 @@ def native_value(self) -> float | None:

async def async_set_native_value(self, value: float) -> None:
"""Update the current value."""
await self._device.set_value(self.entity_description.api_param, float(value))
if self.entity_description.round_digits is not None:
await self._device.set_value(self.entity_description.api_param, round(float(value), self.entity_description.round_digits))
else:
await self._device.set_value(self.entity_description.api_param, int(value))
self._set_current_option()

def _get_current_option(self) -> str | None:
Expand All @@ -453,6 +479,9 @@ def _get_current_option(self) -> str | None:
if prop[ID] == self.entity_description.api_param:
_LOGGER.debug("%s Value: %s",
self.entity_description.name, prop[VALUE])

if self.entity_description.round_digits is not None:
return round(prop[VALUE], self.entity_description.round_digits)
return prop[VALUE]
return None

Expand Down

0 comments on commit e3eef36

Please sign in to comment.