Skip to content

Commit a0b9bed

Browse files
frenckatronfrenck
authored andcommitted
Fix number service error messages
1 parent 300965d commit a0b9bed

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

custom_components/spook/ectoplasms/number/services/decrement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def async_handle_service(
3333
if not math.isclose(amount % entity.step, 0, abs_tol=1e-9):
3434
msg = (
3535
f"Amount {amount} not valid for {entity.entity_id}, "
36-
f"it needs to be a multiple of {entity.step}",
36+
f"it needs to be a multiple of {entity.step}"
3737
)
3838
raise ValueError(msg)
3939

custom_components/spook/ectoplasms/number/services/increment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def async_handle_service(
3333
if not math.isclose(amount % entity.step, 0, abs_tol=1e-9):
3434
msg = (
3535
f"Amount {amount} not valid for {entity.entity_id}, "
36-
f"it needs to be a multiple of {entity.step}",
36+
f"it needs to be a multiple of {entity.step}"
3737
)
3838
raise ValueError(msg)
3939

tests/ectoplasms/number/services/test_increment_decrement.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ async def test_number_services_handle_string_native_values(
5252
assert entity.set_value == expected
5353

5454

55+
@pytest.mark.parametrize(
56+
"service_cls",
57+
[increment.SpookService, decrement.SpookService],
58+
)
59+
async def test_number_services_raise_readable_error_for_invalid_amount(
60+
hass: Any,
61+
service_cls: type[increment.SpookService | decrement.SpookService],
62+
) -> None:
63+
"""Test invalid amounts raise a readable error."""
64+
entity = MockNumberEntity(1.5)
65+
call = SimpleNamespace(data={"amount": 0.2})
66+
67+
with pytest.raises(
68+
ValueError,
69+
match=escape(
70+
"Amount 0.2 not valid for number.test, it needs to be a multiple of 0.5"
71+
),
72+
):
73+
await service_cls(hass).async_handle_service(entity, call)
74+
75+
5576
@pytest.mark.parametrize(
5677
"service_cls",
5778
[increment.SpookService, decrement.SpookService],

0 commit comments

Comments
 (0)