async def test_parse_date_time(hass, caplog):
    """Run time parse datetime tests."""

    #
    # Hardcode a location and timezone so we can check sunrise
    # and sunset.
    #
    hass.config.latitude = 38
    hass.config.longitude = -122
    hass.config.elevation = 0
    hass.config.time_zone = "America/Los_Angeles"

    Function.init(hass)
    TrigTime.init(hass)

    #
    # This set of tests assumes it's currently 13:00 on 2019/9/1
    #
    now = dt(2019, 9, 1, 13, 0, 0, 0)

    with patch("homeassistant.helpers.condition.dt_util.utcnow",
               return_value=now), patch("homeassistant.util.dt.utcnow",
                                        return_value=now):
        for test_data in parseDateTimeTests:
            spec, date_offset, expect = test_data
            out = TrigTime.parse_date_time(spec, date_offset, now)
            assert out == expect
def test_timer_active_check(hass, spec, now, expected):
    """Run time active check tests."""

    Function.init(hass)
    TrigTime.init(hass)
    out = TrigTime.timer_active_check(spec, now)
    assert out == expected
Example #3
0
async def test_eval(hass):
    """Test interpreter."""
    hass.data[DOMAIN] = MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: False})
    Function.init(hass)
    State.init(hass)
    State.register_functions()

    for test_data in evalTests:
        await run_one_test(test_data)
Example #4
0
async def test_eval_exceptions(hass):
    """Test interpreter exceptions."""
    hass.data[DOMAIN] = {CONFIG_ENTRY: MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: False})}
    Function.init(hass)
    State.init(hass)
    State.register_functions()
    TrigTime.init(hass)

    for test_data in evalTestsExceptions:
        await run_one_test_exception(test_data)
def test_timer_trigger_next_month_rollover(hass):
    """Run month rollover tests."""

    Function.init(hass)
    TrigTime.init(hass)

    for test_data in timerTriggerNextTestsMonthRollover:
        now = dt(2020, 6, 30, 13, 0, 0, 100000)
        spec, expect_seq = test_data
        for expect in expect_seq:
            t_next = TrigTime.timer_trigger_next(spec, now)
            assert t_next == expect
            now = t_next
Example #6
0
def test_timer_active_check(hass, spec, now, expected):
    """Run time active check tests."""

    #
    # Hardcode a location and timezone so we can check sunrise
    # and sunset.
    #
    hass.config.latitude = 38
    hass.config.longitude = -122
    hass.config.elevation = 0
    hass.config.time_zone = "America/Los_Angeles"

    startup_time = dt(2019, 9, 1, 13, 0, 0, 0)
    Function.init(hass)
    TrigTime.init(hass)
    print(f"calling timer_active_check({spec}, {now}, {startup_time})")
    out = TrigTime.timer_active_check(spec, now, startup_time)
    assert out == expected
def test_timer_trigger_next(hass):
    """Run trigger next tests."""
    #
    # Hardcode a location and timezone so we can check sunrise
    # and sunset.
    #
    hass.config.latitude = 38
    hass.config.longitude = -122
    hass.config.elevation = 0
    hass.config.time_zone = "America/Los_Angeles"

    Function.init(hass)
    TrigTime.init(hass)

    for test_data in timerTriggerNextTests:
        now = dt(2019, 9, 1, 13, 0, 0, 100000)
        spec, expect_seq = test_data
        for expect in expect_seq:
            t_next = TrigTime.timer_trigger_next(spec, now)
            assert t_next == expect
            now = t_next
Example #8
0
def test_timer_trigger_next(hass):
    """Run trigger next tests."""
    #
    # Hardcode a location and timezone so we can check sunrise
    # and sunset.
    #
    hass.config.latitude = 38
    hass.config.longitude = -122
    hass.config.elevation = 0
    hass.config.time_zone = "America/Los_Angeles"

    Function.init(hass)
    TrigTime.init(hass)

    for test_data in timerTriggerNextTests:
        startup_time = now = dt(2019, 9, 1, 13, 0, 0, 100000)
        spec, expect_seq = test_data
        for expect in expect_seq:
            print(f"calling timer_trigger_next({spec}, {now}, {startup_time})")
            t_next = TrigTime.timer_trigger_next(spec, now, startup_time)
            assert t_next == expect
            if t_next is None:
                break
            now = t_next + timedelta(microseconds=1)
Example #9
0
async def test_service_call_params(hass):
    """Test that hass params get set properly on service calls."""
    with patch.object(hass.services, "async_call") as call, patch.object(
            Function, "service_has_service", return_value=True):
        Function.init(hass)
        await Function.service_call("test",
                                    "test",
                                    context=Context(id="test"),
                                    blocking=True,
                                    limit=1,
                                    other_service_data="test")
        assert call.called
        assert call.call_args[0] == ("test", "test", {
            "other_service_data": "test"
        })
        assert call.call_args[1] == {
            "context": Context(id="test"),
            "blocking": True,
            "limit": 1
        }
        call.reset_mock()

        await Function.service_call("test",
                                    "test",
                                    context=Context(id="test"),
                                    blocking=False,
                                    other_service_data="test")
        assert call.called
        assert call.call_args[0] == ("test", "test", {
            "other_service_data": "test"
        })
        assert call.call_args[1] == {
            "context": Context(id="test"),
            "blocking": False
        }
        call.reset_mock()

        await Function.get("test.test")(context=Context(id="test"),
                                        blocking=True,
                                        limit=1,
                                        other_service_data="test")
        assert call.called
        assert call.call_args[0] == ("test", "test", {
            "other_service_data": "test"
        })
        assert call.call_args[1] == {
            "context": Context(id="test"),
            "blocking": True,
            "limit": 1
        }
        call.reset_mock()

        await Function.get("test.test")(context=Context(id="test"),
                                        blocking=False,
                                        other_service_data="test")
        assert call.called
        assert call.call_args[0] == ("test", "test", {
            "other_service_data": "test"
        })
        assert call.call_args[1] == {
            "context": Context(id="test"),
            "blocking": False
        }

    # Stop all tasks to avoid conflicts with other tests
    await Function.reaper_stop()