Skip to content

Commit

Permalink
Expected state of a float (point zero)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitfin committed Feb 13, 2024
1 parent 5b49ae3 commit 6a8d44d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion custom_components/retry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,15 @@ def _check_state(self, entity: Entity | None) -> bool:
"""Check if the entity's state is expected."""
if not entity or ATTR_EXPECTED_STATE not in self._params.retry_data:
return True
return entity.state in self._params.retry_data[ATTR_EXPECTED_STATE]
for expected in self._params.retry_data[ATTR_EXPECTED_STATE]:
if entity.state == expected:
return True
try:
if float(entity.state) == float(expected):
return True
except ValueError:
pass
return False

def _check_validation(self) -> bool:
"""Check if the validation statement is true."""
Expand Down
28 changes: 28 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,34 @@ async def test_validation_success(
assert len(calls) == 1


@patch("custom_components.retry.asyncio.sleep")
async def test_float_point_zero(
_: AsyncMock,
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test validation of a float with point zero."""
calls = await async_setup(hass, False)
await async_setup_component(
hass,
"input_number",
{
"input_number": {
"test": {"min": 0, "max": 100, "initial": 50},
}
},
)
await async_call(
hass,
{
ATTR_ENTITY_ID: "input_number.test",
ATTR_EXPECTED_STATE: "50",
},
)
await async_shutdown(hass, freezer)
assert len(calls) == 1


@patch("custom_components.retry.asyncio.sleep")
async def test_validation_in_automation(
_: AsyncMock,
Expand Down

0 comments on commit 6a8d44d

Please sign in to comment.