|
1 | 1 | """Test pyscripts test module."""
|
2 | 2 |
|
| 3 | +from datetime import datetime, timezone |
3 | 4 | from unittest.mock import patch
|
4 | 5 |
|
5 | 6 | import pytest
|
6 | 7 |
|
7 | 8 | from custom_components.pyscript.function import Function
|
8 |
| -from custom_components.pyscript.state import State |
| 9 | +from custom_components.pyscript.state import State, StateVal |
| 10 | +from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN |
9 | 11 | from homeassistant.core import Context, ServiceRegistry, StateMachine
|
10 | 12 | from homeassistant.helpers.state import State as HassState
|
11 | 13 |
|
@@ -51,3 +53,58 @@ async def test_service_call(hass):
|
51 | 53 | # Stop all tasks to avoid conflicts with other tests
|
52 | 54 | await Function.waiter_stop()
|
53 | 55 | await Function.reaper_stop()
|
| 56 | + |
| 57 | + |
| 58 | +def test_state_val_conversions(): |
| 59 | + """Test helper conversion methods exposed on StateVal.""" |
| 60 | + float_state = StateVal(HassState("test.float", "123.45")) |
| 61 | + assert float_state.as_float() == pytest.approx(123.45) |
| 62 | + |
| 63 | + int_state = StateVal(HassState("test.int", "42")) |
| 64 | + assert int_state.as_int() == 42 |
| 65 | + |
| 66 | + hex_state = StateVal(HassState("test.hex", "FF")) |
| 67 | + assert hex_state.as_int(base=16) == 255 |
| 68 | + |
| 69 | + bool_state = StateVal(HassState("test.bool", "on")) |
| 70 | + assert bool_state.as_bool() is True |
| 71 | + |
| 72 | + round_state = StateVal(HassState("test.round", "3.1415")) |
| 73 | + assert round_state.as_round(precision=2) == pytest.approx(3.14) |
| 74 | + |
| 75 | + datetime_state = StateVal(HassState("test.datetime", "2024-03-05T06:07:08+00:00")) |
| 76 | + assert datetime_state.as_datetime() == datetime(2024, 3, 5, 6, 7, 8, tzinfo=timezone.utc) |
| 77 | + |
| 78 | + invalid_state = StateVal(HassState("test.invalid", "invalid")) |
| 79 | + with pytest.raises(ValueError): |
| 80 | + invalid_state.as_float() |
| 81 | + with pytest.raises(ValueError): |
| 82 | + invalid_state.as_int() |
| 83 | + with pytest.raises(ValueError): |
| 84 | + invalid_state.as_bool() |
| 85 | + with pytest.raises(ValueError): |
| 86 | + invalid_state.as_round() |
| 87 | + with pytest.raises(ValueError): |
| 88 | + invalid_state.as_datetime() |
| 89 | + |
| 90 | + assert invalid_state.as_bool(default=False) is False |
| 91 | + |
| 92 | + assert invalid_state.as_float(default=1.23) == pytest.approx(1.23) |
| 93 | + |
| 94 | + assert invalid_state.as_round(default=0) == 0 |
| 95 | + |
| 96 | + fallback_datetime = datetime(1999, 1, 2, 3, 4, 5, tzinfo=timezone.utc) |
| 97 | + assert invalid_state.as_datetime(default=fallback_datetime) == fallback_datetime |
| 98 | + |
| 99 | + unknown_state = StateVal(HassState("test.unknown", STATE_UNKNOWN)) |
| 100 | + assert unknown_state.is_unknown() is True |
| 101 | + assert unknown_state.is_unavailable() is False |
| 102 | + assert unknown_state.has_value() is False |
| 103 | + |
| 104 | + unavailable_state = StateVal(HassState("test.unavailable", STATE_UNAVAILABLE)) |
| 105 | + assert unavailable_state.is_unavailable() is True |
| 106 | + assert unavailable_state.is_unknown() is False |
| 107 | + assert unavailable_state.has_value() is False |
| 108 | + |
| 109 | + standard_state = StateVal(HassState("test.standard", "ready")) |
| 110 | + assert standard_state.has_value() is True |
0 commit comments