def test_last_event(fake_events):

    fake_event = fake_events

    assert Event.last_event() == fake_event
    assert len(Event.query.all()) == 3
    assert Event.last_on() == fake_event
def test_power_off_failure(test_client, fake_users, arduino):
    auth_string = base64.b64encode('test_user:test_password')
    header = {'Authorization': 'Basic ' + auth_string}
    resp = test_client.get('/poweroff', headers=header)
    event = Event.last_event()

    assert resp.status_code == 500
    assert event is None
def test_monitor_ac_usage_turn_off(fake_events, arduino, app):
    old_event = fake_events.timestamp
    monitor_ac_usage(1)
    new_event = Event.last_event()
    old_event = Event.query.filter(Event.timestamp == old_event).first()

    assert naturally_equivalent(old_event, new_event) is False
    assert new_event.event == EventType.off
    assert new_event.event_description == "AutoOff Timer"
def test_monitor_ac_usage_leave_on(fake_events, arduino):
    old_event = fake_events.timestamp
    monitor_ac_usage()
    new_event = Event.last_event()
    old_event = Event.query.filter(Event.timestamp == old_event).first()

    assert naturally_equivalent(old_event, new_event)
    assert new_event.event == EventType.on
    assert new_event.event_description is None
def monitor_ac_usage(minutes=30):
    """ Checks how long AC has been running and shuts it off after max is reached.
    :param minutes: How many minutes to let the AC run continuously.
    :return:
    """
    with app.app_context():
        event = Event.last_event()
        if event and event.event != EventType.off:
            off_time = event.timestamp.replace(minutes=+minutes)
            now = arrow.utcnow()

            if off_time < now:
                if arduino.turn_off():
                    Event.create_event(EventType.off, "AutoOff Timer")
def send_last_event():
    data = Event.last_event()
    return jsonify(results=data.serialize)