def user_page(user_id): """Show the vehicle data associated with the user.""" user_id = str(user_id) now = int(time()) user_data = db.get_user(user_id) if not user_data: abort(404) if now < user_data['begins_at']: return render_template("user_wait.html", title=TITLE, begins_at=user_data['begins_at']) if now > user_data['expires_at']: db.delete_user(user_id) abort(404) client = TeslaApiClient(token=user_data['token']) try: # Make sure the token and vehicle_id are valid vehicle = client.get_vehicle(user_data['vehicle_id']) except AuthenticationError: abort(403) # Return template immediately and fill the rest with JSON-requests. if not request.args.get('json'): return render_template("user_page.html", title=TITLE, user_id=user_id, vehicle_name=vehicle.display_name) api_error = False response = {} try: if vehicle.state != "online": _resp = vehicle.wake_up() data = vehicle.get_data() response['battery_level'] = data['charge_state'].get('battery_level') response['charging_state'] = data['charge_state'].get('charging_state') response['is_climate_on'] = data['climate_state'].get('is_climate_on') response['temp_setting'] = data['climate_state'].get( 'driver_temp_setting') response['inside_temp'] = data['climate_state'].get('inside_temp') response['outside_temp'] = data['climate_state'].get('outside_temp') response['gui_temperature_units'] = data['gui_settings'].get( 'gui_temperature_units') response['locked'] = data['vehicle_state'].get('locked') response['vehicle_name'] = vehicle.display_name except ApiError as error_msg: api_error = True LOG.info("Remote API error: %s", error_msg) return jsonify(response=response, api_error=api_error)
def api(): """API to control the vehicle.""" commands = ['start_climate', 'stop_climate'] now = int(time()) try: user_id = request.form['user_id'] command = request.form['command'] except ValueError: abort(400) if not command in commands: abort(400) user_data = db.get_user(user_id) if not user_data: abort(403) if now < user_data['begins_at']: abort(403) if now > user_data['expires_at']: db.delete_user(user_id) abort(403) client = TeslaApiClient(token=user_data['token']) try: # Make sure the token and vehicle_id are valid vehicle = client.get_vehicle(user_data['vehicle_id']) except AuthenticationError: abort(403) try: if command == 'start_climate': data = vehicle.climate.start_climate() if command == 'stop_climate': data = vehicle.climate.stop_climate() except ApiError as error_msg: data = {'result': False} LOG.info("Remote API error: %s", error_msg) return jsonify(data)
def authorize(): """Authorize access and show the URL for user page.""" if not check_csrf_token(request.form['token'], SECRET_KEY, request.form['csrf']): LOG.warning('CSRF failed') abort(403) try: vehicle_id = request.form['vehicle'] begins_at = int(request.form['begins_at']) expires_at = int(request.form['expires_at']) assert vehicle_id.isnumeric() assert 0 <= begins_at <= MAX_ACCESS_DURATION - 1 assert 1 <= expires_at <= MAX_ACCESS_DURATION assert begins_at < expires_at except (ValueError, AssertionError) as _error_msg: abort(400) token = _get_token(refresh_token=request.form['token']) client = TeslaApiClient(token=token) try: # Make sure the token and vehicle_id are valid _data = client.get_vehicle(vehicle_id) except AuthenticationError: abort(403) user_id = str(uuid4()) # Convert submitted times to Unix timestamps now = int(time()) begins_at = now + begins_at * 3600 expires_at = now + expires_at * 3600 data = { "token": client.token, "vehicle_id": vehicle_id, "begins_at": begins_at, "expires_at": expires_at } db.add_user(user_id, data) return render_template("authorize.html", title=TITLE, user_id=user_id)