Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decimal math inaccuracy in increment/decrement services #766

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import math
from typing import TYPE_CHECKING

import voluptuous as vol
Expand Down Expand Up @@ -33,9 +34,8 @@ async def async_handle_service(
) -> None:
"""Handle the service call."""
# pylint: disable=protected-access
if (
amount := call.data.get("amount", entity._step) # noqa: SLF001
) % entity._step != 0: # noqa: SLF001
amount = call.data.get("amount", entity._step) # noqa: SLF001
if not math.isclose(amount % entity._step, 0, abs_tol=1e-9): # noqa: SLF001
msg = (
f"Amount {amount} not valid for {entity.entity_id}, "
f"it needs to be a multiple of {entity._step}", # noqa: SLF001
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import math
from typing import TYPE_CHECKING

import voluptuous as vol
Expand Down Expand Up @@ -33,9 +34,8 @@ async def async_handle_service(
) -> None:
"""Handle the service call."""
# pylint: disable=protected-access
if (
amount := call.data.get("amount", entity._step) # noqa: SLF001
) % entity._step != 0: # noqa: SLF001
amount = call.data.get("amount", entity._step) # noqa: SLF001
if not math.isclose(amount % entity._step, 0, abs_tol=1e-9): # noqa: SLF001
msg = (
f"Amount {amount} not valid for {entity.entity_id}, "
f"it needs to be a multiple of {entity._step}", # noqa: SLF001
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import math
from typing import TYPE_CHECKING

import voluptuous as vol
Expand All @@ -27,7 +28,8 @@ async def async_handle_service(
call: ServiceCall,
) -> None:
"""Handle the service call."""
if (amount := call.data.get("amount", entity.step or 1)) % entity.step != 0:
amount = call.data.get("amount", entity.step or 1)
if not math.isclose(amount % entity.step, 0, abs_tol=1e-9):
msg = (
f"Amount {amount} not valid for {entity.entity_id}, "
f"it needs to be a multiple of {entity.step}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import math
from typing import TYPE_CHECKING

import voluptuous as vol
Expand All @@ -27,7 +28,8 @@ async def async_handle_service(
call: ServiceCall,
) -> None:
"""Handle the service call."""
if (amount := call.data.get("amount", entity.step or 1)) % entity.step != 0:
amount = call.data.get("amount", entity.step or 1)
if not math.isclose(amount % entity.step, 0, abs_tol=1e-9):
msg = (
f"Amount {amount} not valid for {entity.entity_id}, "
f"it needs to be a multiple of {entity.step}",
Expand Down