示例#1
0
def test_timer_out_of_sync(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    mock_monotonic.side_effect = 10.2, 11.3, 11.3

    with patch.object(ha, 'callback', mock_callback), \
            patch('homeassistant.core.dt_util.utcnow',
                  return_value=sentinel.mock_date):
        ha._async_create_timer(hass)

        assert len(funcs) == 2
        fire_time_event, stop_timer = funcs

    assert len(hass.loop.call_later.mock_calls) == 1

    slp_seconds, callback, nxt = hass.loop.call_later.mock_calls[0][1]
    assert slp_seconds == 1
    assert callback is fire_time_event
    assert abs(nxt - 12.3) < 0.001
示例#2
0
    def start(self):
        """Start the instance."""
        # Ensure a local API exists to connect with remote
        if 'api' not in self.config.components:
            if not bootstrap.setup_component(self, 'api'):
                raise HomeAssistantError(
                    'Unable to setup local API to receive events')

        self.state = ha.CoreState.starting
        # pylint: disable=protected-access
        ha._async_create_timer(self)

        self.bus.fire(ha.EVENT_HOMEASSISTANT_START,
                      origin=ha.EventOrigin.remote)

        # Ensure local HTTP is started
        self.block_till_done()
        self.state = ha.CoreState.running
        time.sleep(0.05)

        # Setup that events from remote_api get forwarded to local_api
        # Do this after we are running, otherwise HTTP is not started
        # or requests are blocked
        if not connect_remote_events(self.remote_api, self.config.api):
            raise HomeAssistantError(
                ('Could not setup event forwarding from api {} to '
                 'local api {}').format(self.remote_api, self.config.api))
示例#3
0
    def test_create_timer(self, mock_utcnow, mock_event, event_loop):
        """Test create timer fires correctly."""
        hass = MagicMock()
        now = mock_utcnow()
        event = mock_event()
        now.second = 1
        mock_utcnow.reset_mock()

        ha._async_create_timer(hass)
        assert len(hass.bus.async_listen_once.mock_calls) == 2
        start_timer = hass.bus.async_listen_once.mock_calls[1][1][1]

        event_loop.run_until_complete(start_timer(None))
        assert hass.loop.create_task.called

        timer = hass.loop.create_task.mock_calls[0][1][0]
        event.is_set.side_effect = False, False, True
        event_loop.run_until_complete(timer)
        assert len(mock_utcnow.mock_calls) == 1

        assert hass.loop.call_soon.called
        event_type, event_data = hass.loop.call_soon.mock_calls[0][1][1:]

        assert ha.EVENT_TIME_CHANGED == event_type
        assert {ha.ATTR_NOW: now} == event_data

        stop_timer = hass.bus.async_listen_once.mock_calls[0][1][1]
        stop_timer(None)
        assert event.set.called
示例#4
0
    def test_create_timer(self, mock_utcnow, mock_event, event_loop):
        """Test create timer fires correctly."""
        hass = MagicMock()
        now = mock_utcnow()
        event = mock_event()
        now.second = 1
        mock_utcnow.reset_mock()

        ha._async_create_timer(hass)
        assert len(hass.bus.async_listen_once.mock_calls) == 2
        start_timer = hass.bus.async_listen_once.mock_calls[1][1][1]

        event_loop.run_until_complete(start_timer(None))
        assert hass.loop.create_task.called

        timer = hass.loop.create_task.mock_calls[0][1][0]
        event.is_set.side_effect = False, False, True
        event_loop.run_until_complete(timer)
        assert len(mock_utcnow.mock_calls) == 1

        assert hass.loop.call_soon.called
        event_type, event_data = hass.loop.call_soon.mock_calls[0][1][1:]

        assert ha.EVENT_TIME_CHANGED == event_type
        assert {ha.ATTR_NOW: now} == event_data

        stop_timer = hass.bus.async_listen_once.mock_calls[0][1][1]
        stop_timer(None)
        assert event.set.called
示例#5
0
    def start(self):
        """Start the instance."""
        # Ensure a local API exists to connect with remote
        if 'api' not in self.config.components:
            if not setup.setup_component(self, 'api'):
                raise HomeAssistantError(
                    'Unable to setup local API to receive events')

        self.state = ha.CoreState.starting
        # pylint: disable=protected-access
        ha._async_create_timer(self)

        self.bus.fire(ha.EVENT_HOMEASSISTANT_START,
                      origin=ha.EventOrigin.remote)

        # Ensure local HTTP is started
        self.block_till_done()
        self.state = ha.CoreState.running
        time.sleep(0.05)

        # Setup that events from remote_api get forwarded to local_api
        # Do this after we are running, otherwise HTTP is not started
        # or requests are blocked
        if not connect_remote_events(self.remote_api, self.config.api):
            raise HomeAssistantError((
                'Could not setup event forwarding from api {} to '
                'local api {}').format(self.remote_api, self.config.api))
示例#6
0
def test_timer_out_of_sync(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    mock_monotonic.side_effect = 10.2, 11.3, 11.3

    with patch.object(ha, 'callback', mock_callback), \
            patch('homeassistant.core.dt_util.utcnow',
                  return_value=sentinel.mock_date):
        ha._async_create_timer(hass)

        assert len(funcs) == 2
        fire_time_event, stop_timer = funcs

    assert len(hass.loop.call_later.mock_calls) == 1

    slp_seconds, callback, nxt = hass.loop.call_later.mock_calls[0][1]
    assert slp_seconds == 1
    assert callback is fire_time_event
    assert abs(nxt - 12.3) < 0.001
示例#7
0
def test_timer_out_of_sync(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    mock_monotonic.side_effect = 10.2, 13.3, 13.4

    with patch.object(ha, 'callback', mock_callback), \
            patch('homeassistant.core.dt_util.utcnow',
                  return_value=datetime(2018, 12, 31, 3, 4, 5, 333333)):
        ha._async_create_timer(hass)

    delay, callback, target = hass.loop.call_later.mock_calls[0][1]

    with patch.object(ha, '_LOGGER', MagicMock()) as mock_logger, \
            patch('homeassistant.core.dt_util.utcnow',
                  return_value=datetime(2018, 12, 31, 3, 4, 8, 200000)):
        callback(target)

        assert len(mock_logger.error.mock_calls) == 1

        assert len(funcs) == 2
        fire_time_event, stop_timer = funcs

    assert len(hass.loop.call_later.mock_calls) == 2

    delay, callback, target = hass.loop.call_later.mock_calls[1][1]
    assert abs(delay - 0.8) < 0.001
    assert callback is fire_time_event
    assert abs(target - 14.2) < 0.001
def test_timer_out_of_sync(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    mock_monotonic.side_effect = 10.2, 13.3, 13.4

    with patch.object(ha, "callback", mock_callback), patch(
            "homeassistant.core.dt_util.utcnow",
            return_value=datetime(2018, 12, 31, 3, 4, 5, 333333),
    ):
        ha._async_create_timer(hass)

    delay, callback, target = hass.loop.call_later.mock_calls[0][1]

    with patch(
            "homeassistant.core.dt_util.utcnow",
            return_value=datetime(2018, 12, 31, 3, 4, 8, 200000),
    ):
        callback(target)

        _, event_0_args, event_0_kwargs = hass.bus.async_fire.mock_calls[0]
        event_context_0 = event_0_kwargs["context"]

        event_type_0, _ = event_0_args
        assert event_type_0 == EVENT_TIME_CHANGED

        _, event_1_args, event_1_kwargs = hass.bus.async_fire.mock_calls[1]
        event_type_1, event_data_1 = event_1_args
        event_context_1 = event_1_kwargs["context"]

        assert event_type_1 == EVENT_TIMER_OUT_OF_SYNC
        assert abs(event_data_1[ATTR_SECONDS] - 2.433333) < 0.001

        assert event_context_0 == event_context_1

        assert len(funcs) == 2
        fire_time_event, _ = funcs

    assert len(hass.loop.call_later.mock_calls) == 2

    delay, callback, target = hass.loop.call_later.mock_calls[1][1]
    assert abs(delay - 0.8) < 0.001
    assert callback is fire_time_event
    assert abs(target - 14.2) < 0.001
示例#9
0
def test_create_timer(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    mock_monotonic.side_effect = 10.2, 10.8, 11.3

    with patch.object(ha, "callback", mock_callback), patch(
            "homeassistant.core.dt_util.utcnow",
            return_value=datetime(2018, 12, 31, 3, 4, 5, 333333),
    ):
        ha._async_create_timer(hass)

    assert len(funcs) == 2
    fire_time_event, stop_timer = funcs

    assert len(hass.loop.call_later.mock_calls) == 1
    delay, callback, target = hass.loop.call_later.mock_calls[0][1]
    assert abs(delay - 0.666667) < 0.001
    assert callback is fire_time_event
    assert abs(target - 10.866667) < 0.001

    with patch(
            "homeassistant.core.dt_util.utcnow",
            return_value=datetime(2018, 12, 31, 3, 4, 6, 100000),
    ):
        callback(target)

    assert len(hass.bus.async_listen_once.mock_calls) == 1
    assert len(hass.bus.async_fire.mock_calls) == 1
    assert len(hass.loop.call_later.mock_calls) == 2

    event_type, callback = hass.bus.async_listen_once.mock_calls[0][1]
    assert event_type == EVENT_HOMEASSISTANT_STOP
    assert callback is stop_timer

    delay, callback, target = hass.loop.call_later.mock_calls[1][1]
    assert abs(delay - 0.9) < 0.001
    assert callback is fire_time_event
    assert abs(target - 12.2) < 0.001

    event_type, event_data = hass.bus.async_fire.mock_calls[0][1]
    assert event_type == EVENT_TIME_CHANGED
    assert event_data[ATTR_NOW] == datetime(2018, 12, 31, 3, 4, 6, 100000)
示例#10
0
def test_create_timer(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    mock_monotonic.side_effect = 10.2, 10.8, 11.3

    with patch.object(ha, 'callback', mock_callback), \
            patch('homeassistant.core.dt_util.utcnow',
                  return_value=datetime(2018, 12, 31, 3, 4, 5, 333333)):
        ha._async_create_timer(hass)

    assert len(funcs) == 2
    fire_time_event, stop_timer = funcs

    assert len(hass.loop.call_later.mock_calls) == 1
    delay, callback, target = hass.loop.call_later.mock_calls[0][1]
    assert abs(delay - 0.666667) < 0.001
    assert callback is fire_time_event
    assert abs(target - 10.866667) < 0.001

    with patch('homeassistant.core.dt_util.utcnow',
               return_value=datetime(2018, 12, 31, 3, 4, 6, 100000)):
        callback(target)

    assert len(hass.bus.async_listen_once.mock_calls) == 1
    assert len(hass.bus.async_fire.mock_calls) == 1
    assert len(hass.loop.call_later.mock_calls) == 2

    event_type, callback = hass.bus.async_listen_once.mock_calls[0][1]
    assert event_type == EVENT_HOMEASSISTANT_STOP
    assert callback is stop_timer

    delay, callback, target = hass.loop.call_later.mock_calls[1][1]
    assert abs(delay - 0.9) < 0.001
    assert callback is fire_time_event
    assert abs(target - 12.2) < 0.001

    event_type, event_data = hass.bus.async_fire.mock_calls[0][1]
    assert event_type == EVENT_TIME_CHANGED
    assert event_data[ATTR_NOW] == datetime(2018, 12, 31, 3, 4, 6, 100000)
示例#11
0
def test_create_timer(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    with patch.object(ha, 'callback', mock_callback):
        ha._async_create_timer(hass)

        assert len(funcs) == 3
        fire_time_event, start_timer, stop_timer = funcs

    assert len(hass.bus.async_listen_once.mock_calls) == 1
    event_type, callback = hass.bus.async_listen_once.mock_calls[0][1]
    assert event_type == EVENT_HOMEASSISTANT_START
    assert callback is start_timer

    mock_monotonic.side_effect = 10.2, 10.3

    with patch('homeassistant.core.dt_util.utcnow',
               return_value=sentinel.mock_date):
        start_timer(None)

    assert len(hass.bus.async_listen_once.mock_calls) == 2
    assert len(hass.bus.async_fire.mock_calls) == 1
    assert len(hass.loop.call_later.mock_calls) == 1

    event_type, callback = hass.bus.async_listen_once.mock_calls[1][1]
    assert event_type == EVENT_HOMEASSISTANT_STOP
    assert callback is stop_timer

    slp_seconds, callback, nxt = hass.loop.call_later.mock_calls[0][1]
    assert abs(slp_seconds - 0.9) < 0.001
    assert callback is fire_time_event
    assert abs(nxt - 11.2) < 0.001

    event_type, event_data = hass.bus.async_fire.mock_calls[0][1]
    assert event_type == EVENT_TIME_CHANGED
    assert event_data[ATTR_NOW] is sentinel.mock_date
示例#12
0
def test_create_timer(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    with patch.object(ha, 'callback', mock_callback):
        ha._async_create_timer(hass)

        assert len(funcs) == 3
        fire_time_event, start_timer, stop_timer = funcs

    assert len(hass.bus.async_listen_once.mock_calls) == 1
    event_type, callback = hass.bus.async_listen_once.mock_calls[0][1]
    assert event_type == EVENT_HOMEASSISTANT_START
    assert callback is start_timer

    mock_monotonic.side_effect = 10.2, 10.3

    with patch('homeassistant.core.dt_util.utcnow',
               return_value=sentinel.mock_date):
        start_timer(None)

    assert len(hass.bus.async_listen_once.mock_calls) == 2
    assert len(hass.bus.async_fire.mock_calls) == 1
    assert len(hass.loop.call_later.mock_calls) == 1

    event_type, callback = hass.bus.async_listen_once.mock_calls[1][1]
    assert event_type == EVENT_HOMEASSISTANT_STOP
    assert callback is stop_timer

    slp_seconds, callback, nxt = hass.loop.call_later.mock_calls[0][1]
    assert abs(slp_seconds - 0.9) < 0.001
    assert callback is fire_time_event
    assert abs(nxt - 11.2) < 0.001

    event_type, event_data = hass.bus.async_fire.mock_calls[0][1]
    assert event_type == EVENT_TIME_CHANGED
    assert event_data[ATTR_NOW] is sentinel.mock_date
示例#13
0
def test_timer_out_of_sync(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    mock_monotonic.side_effect = 10.2, 13.3, 13.4

    with patch.object(ha, 'callback', mock_callback), \
            patch('homeassistant.core.dt_util.utcnow',
                  return_value=datetime(2018, 12, 31, 3, 4, 5, 333333)):
        ha._async_create_timer(hass)

    delay, callback, target = hass.loop.call_later.mock_calls[0][1]

    with patch('homeassistant.core.dt_util.utcnow',
               return_value=datetime(2018, 12, 31, 3, 4, 8, 200000)):
        callback(target)

        event_type, event_data = hass.bus.async_fire.mock_calls[1][1]
        assert event_type == EVENT_TIMER_OUT_OF_SYNC
        assert abs(event_data[ATTR_SECONDS] - 2.433333) < 0.001

        assert len(funcs) == 2
        fire_time_event, stop_timer = funcs

    assert len(hass.loop.call_later.mock_calls) == 2

    delay, callback, target = hass.loop.call_later.mock_calls[1][1]
    assert abs(delay - 0.8) < 0.001
    assert callback is fire_time_event
    assert abs(target - 14.2) < 0.001
示例#14
0
def test_timer_out_of_sync(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    mock_monotonic.side_effect = 10.2, 13.3, 13.4

    with patch.object(ha, 'callback', mock_callback), \
            patch('homeassistant.core.dt_util.utcnow',
                  return_value=datetime(2018, 12, 31, 3, 4, 5, 333333)):
        ha._async_create_timer(hass)

    delay, callback, target = hass.loop.call_later.mock_calls[0][1]

    with patch('homeassistant.core.dt_util.utcnow',
               return_value=datetime(2018, 12, 31, 3, 4, 8, 200000)):
        callback(target)

        event_type, event_data = hass.bus.async_fire.mock_calls[1][1]
        assert event_type == EVENT_TIMER_OUT_OF_SYNC
        assert abs(event_data[ATTR_SECONDS] - 2.433333) < 0.001

        assert len(funcs) == 2
        fire_time_event, stop_timer = funcs

    assert len(hass.loop.call_later.mock_calls) == 2

    delay, callback, target = hass.loop.call_later.mock_calls[1][1]
    assert abs(delay - 0.8) < 0.001
    assert callback is fire_time_event
    assert abs(target - 14.2) < 0.001